|
@ -1,17 +1,29 @@ |
|
|
package com.jiangdg.usbcamera.view; |
|
|
package com.jiangdg.usbcamera.view; |
|
|
|
|
|
|
|
|
|
|
|
import android.content.Intent; |
|
|
import android.hardware.usb.UsbDevice; |
|
|
import android.hardware.usb.UsbDevice; |
|
|
import android.os.Bundle; |
|
|
import android.os.Bundle; |
|
|
|
|
|
import android.os.Looper; |
|
|
import android.support.annotation.Nullable; |
|
|
import android.support.annotation.Nullable; |
|
|
|
|
|
import android.support.v7.app.ActionBar; |
|
|
|
|
|
import android.support.v7.app.AlertDialog; |
|
|
import android.support.v7.app.AppCompatActivity; |
|
|
import android.support.v7.app.AppCompatActivity; |
|
|
|
|
|
import android.support.v7.widget.Toolbar; |
|
|
|
|
|
import android.view.LayoutInflater; |
|
|
|
|
|
import android.view.Menu; |
|
|
|
|
|
import android.view.MenuItem; |
|
|
import android.view.Surface; |
|
|
import android.view.Surface; |
|
|
import android.view.View; |
|
|
import android.view.View; |
|
|
import android.widget.Button; |
|
|
import android.widget.AdapterView; |
|
|
|
|
|
import android.widget.ArrayAdapter; |
|
|
|
|
|
import android.widget.ListView; |
|
|
|
|
|
import android.widget.SeekBar; |
|
|
|
|
|
import android.widget.SimpleAdapter; |
|
|
import android.widget.Toast; |
|
|
import android.widget.Toast; |
|
|
|
|
|
|
|
|
import com.jiangdg.usbcamera.utils.FileUtils; |
|
|
|
|
|
import com.jiangdg.usbcamera.R; |
|
|
import com.jiangdg.usbcamera.R; |
|
|
import com.jiangdg.usbcamera.USBCameraManager; |
|
|
import com.jiangdg.usbcamera.UVCCameraHelper; |
|
|
|
|
|
import com.jiangdg.usbcamera.utils.FileUtils; |
|
|
import com.serenegiant.usb.CameraDialog; |
|
|
import com.serenegiant.usb.CameraDialog; |
|
|
import com.serenegiant.usb.Size; |
|
|
import com.serenegiant.usb.Size; |
|
|
import com.serenegiant.usb.USBMonitor; |
|
|
import com.serenegiant.usb.USBMonitor; |
|
@ -19,87 +31,92 @@ import com.serenegiant.usb.common.AbstractUVCCameraHandler; |
|
|
import com.serenegiant.usb.encoder.RecordParams; |
|
|
import com.serenegiant.usb.encoder.RecordParams; |
|
|
import com.serenegiant.usb.widget.CameraViewInterface; |
|
|
import com.serenegiant.usb.widget.CameraViewInterface; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
import butterknife.BindView; |
|
|
import butterknife.BindView; |
|
|
import butterknife.ButterKnife; |
|
|
import butterknife.ButterKnife; |
|
|
import butterknife.OnClick; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* AndroidUSBCamera引擎使用Demo |
|
|
* UVCCamera use demo |
|
|
* |
|
|
* <p> |
|
|
* Created by jiangdongguo on 2017/9/30. |
|
|
* Created by jiangdongguo on 2017/9/30. |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent{ |
|
|
public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent, CameraViewInterface.Callback { |
|
|
@BindView(R.id.camera_view) |
|
|
@BindView(R.id.camera_view) |
|
|
public View mTextureView; |
|
|
public View mTextureView; |
|
|
@BindView(R.id.btn_capture_pic) |
|
|
@BindView(R.id.toolbar) |
|
|
public Button mBtnCapture; |
|
|
public Toolbar mToolbar; |
|
|
@BindView(R.id.btn_rec_video) |
|
|
@BindView(R.id.seekbar_brightness) |
|
|
public Button mBtnRecord; |
|
|
public SeekBar mSeekBrightness; |
|
|
@BindView(R.id.btn_update_resolution) |
|
|
@BindView(R.id.seekbar_contrast) |
|
|
public Button mBtnUpdateResultion; |
|
|
public SeekBar mSeekContrast; |
|
|
@BindView(R.id.btn_restart_camera) |
|
|
|
|
|
Button mBtnRestartCamera; |
|
|
private UVCCameraHelper mCameraHelper; |
|
|
@BindView(R.id.btn_contrast) |
|
|
|
|
|
Button mBtnContrast; |
|
|
|
|
|
@BindView(R.id.btn_brightness) |
|
|
|
|
|
Button mBtnBrightness; |
|
|
|
|
|
|
|
|
|
|
|
private USBCameraManager mUSBManager; |
|
|
|
|
|
|
|
|
|
|
|
private CameraViewInterface mUVCCameraView; |
|
|
private CameraViewInterface mUVCCameraView; |
|
|
|
|
|
private AlertDialog mDialog; |
|
|
|
|
|
|
|
|
private boolean isRequest; |
|
|
private boolean isRequest; |
|
|
private boolean isPreview; |
|
|
private boolean isPreview; |
|
|
|
|
|
|
|
|
/** |
|
|
private UVCCameraHelper.OnMyDevConnectListener listener = new UVCCameraHelper.OnMyDevConnectListener() { |
|
|
* USB设备事件监听器 |
|
|
|
|
|
* */ |
|
|
|
|
|
private USBCameraManager.OnMyDevConnectListener listener = new USBCameraManager.OnMyDevConnectListener() { |
|
|
|
|
|
// 插入USB设备
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onAttachDev(UsbDevice device) { |
|
|
public void onAttachDev(UsbDevice device) { |
|
|
if(mUSBManager == null || mUSBManager.getUsbDeviceCount() == 0){ |
|
|
if (mCameraHelper == null || mCameraHelper.getUsbDeviceCount() == 0) { |
|
|
showShortMsg("未检测到USB摄像头设备"); |
|
|
showShortMsg("check no usb camera"); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
// 请求打开摄像头
|
|
|
// request open permission
|
|
|
if (!isRequest) { |
|
|
if (!isRequest) { |
|
|
isRequest = true; |
|
|
isRequest = true; |
|
|
if(mUSBManager != null){ |
|
|
if (mCameraHelper != null) { |
|
|
mUSBManager.requestPermission(0); |
|
|
mCameraHelper.requestPermission(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 拔出USB设备
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onDettachDev(UsbDevice device) { |
|
|
public void onDettachDev(UsbDevice device) { |
|
|
|
|
|
// close camera
|
|
|
if (isRequest) { |
|
|
if (isRequest) { |
|
|
// 关闭摄像头
|
|
|
|
|
|
isRequest = false; |
|
|
isRequest = false; |
|
|
mUSBManager.closeCamera(); |
|
|
mCameraHelper.closeCamera(); |
|
|
showShortMsg(device.getDeviceName()+"已拨出"); |
|
|
showShortMsg(device.getDeviceName() + " is out"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 连接USB设备成功
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onConnectDev(UsbDevice device, boolean isConnected) { |
|
|
public void onConnectDev(UsbDevice device, boolean isConnected) { |
|
|
if (!isConnected) { |
|
|
if (!isConnected) { |
|
|
showShortMsg("连接失败,请检查分辨率参数是否正确"); |
|
|
showShortMsg("fail to connect,please check resolution params"); |
|
|
isPreview = false; |
|
|
isPreview = false; |
|
|
} else { |
|
|
} else { |
|
|
isPreview = true; |
|
|
isPreview = true; |
|
|
|
|
|
showShortMsg("connecting"); |
|
|
|
|
|
// initialize seekbar
|
|
|
|
|
|
// need to wait UVCCamera initialize over
|
|
|
|
|
|
new Thread(new Runnable() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void run() { |
|
|
|
|
|
try { |
|
|
|
|
|
Thread.sleep(1500); |
|
|
|
|
|
} catch (InterruptedException e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
Looper.prepare(); |
|
|
|
|
|
mSeekBrightness.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_BRIGHTNESS)); |
|
|
|
|
|
mSeekContrast.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_CONTRAST)); |
|
|
|
|
|
Looper.loop(); |
|
|
|
|
|
} |
|
|
|
|
|
}).start(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 与USB设备断开连接
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onDisConnectDev(UsbDevice device) { |
|
|
public void onDisConnectDev(UsbDevice device) { |
|
|
showShortMsg("连接失败"); |
|
|
showShortMsg("disconnecting"); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
@ -108,148 +125,119 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog |
|
|
super.onCreate(savedInstanceState); |
|
|
super.onCreate(savedInstanceState); |
|
|
setContentView(R.layout.activity_usbcamera); |
|
|
setContentView(R.layout.activity_usbcamera); |
|
|
ButterKnife.bind(this); |
|
|
ButterKnife.bind(this); |
|
|
|
|
|
initView(); |
|
|
|
|
|
// step.1 initialize UVCCameraHelper
|
|
|
mUVCCameraView = (CameraViewInterface) mTextureView; |
|
|
mUVCCameraView = (CameraViewInterface) mTextureView; |
|
|
mUVCCameraView.setCallback(new CameraViewInterface.Callback() { |
|
|
mUVCCameraView.setCallback(this); |
|
|
|
|
|
mCameraHelper = UVCCameraHelper.getInstance(); |
|
|
|
|
|
mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void initView() { |
|
|
|
|
|
setSupportActionBar(mToolbar); |
|
|
|
|
|
ActionBar actionBar = getSupportActionBar(); |
|
|
|
|
|
if (actionBar != null) { |
|
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true); |
|
|
|
|
|
} |
|
|
|
|
|
mSeekBrightness.setMax(100); |
|
|
|
|
|
mSeekBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void onSurfaceCreated(CameraViewInterface view, Surface surface) { |
|
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
|
|
if(!isPreview && mUSBManager.isCameraOpened()) { |
|
|
if(mCameraHelper != null && mCameraHelper.isCameraOpened()) { |
|
|
mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { |
|
|
mCameraHelper.setModelValue(UVCCameraHelper.MODE_BRIGHTNESS,progress); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onPreviewResult(boolean result) { |
|
|
public void onStartTrackingTouch(SeekBar seekBar) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onStopTrackingTouch(SeekBar seekBar) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
isPreview = true; |
|
|
mSeekContrast.setMax(100); |
|
|
|
|
|
mSeekContrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
|
|
|
|
|
if(mCameraHelper != null && mCameraHelper.isCameraOpened()) { |
|
|
|
|
|
mCameraHelper.setModelValue(UVCCameraHelper.MODE_CONTRAST,progress); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) { |
|
|
public void onStartTrackingTouch(SeekBar seekBar) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onSurfaceDestroy(CameraViewInterface view, Surface surface) { |
|
|
public void onStopTrackingTouch(SeekBar seekBar) { |
|
|
if(isPreview && mUSBManager.isCameraOpened()) { |
|
|
|
|
|
mUSBManager.stopPreview(); |
|
|
|
|
|
isPreview = false; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
// 初始化引擎
|
|
|
|
|
|
mUSBManager = USBCameraManager.getInstance(); |
|
|
|
|
|
mUSBManager.initUSBMonitor(this,listener); |
|
|
|
|
|
mUSBManager.createUVCCamera(mUVCCameraView); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onStart() { |
|
|
protected void onStart() { |
|
|
super.onStart(); |
|
|
super.onStart(); |
|
|
if(mUSBManager == null) |
|
|
// step.2 register USB event broadcast
|
|
|
return; |
|
|
if (mCameraHelper != null) { |
|
|
// 注册USB事件广播监听器
|
|
|
mCameraHelper.registerUSB(); |
|
|
mUSBManager.registerUSB(); |
|
|
} |
|
|
mUVCCameraView.onResume(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onStop() { |
|
|
protected void onStop() { |
|
|
super.onStop(); |
|
|
super.onStop(); |
|
|
// 注销USB事件广播监听器
|
|
|
// step.3 unregister USB event broadcast
|
|
|
if(mUSBManager != null){ |
|
|
if (mCameraHelper != null) { |
|
|
mUSBManager.unregisterUSB(); |
|
|
mCameraHelper.unregisterUSB(); |
|
|
} |
|
|
} |
|
|
mUVCCameraView.onPause(); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@OnClick({ R.id.btn_contrast,R.id.btn_brightness,R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution,R.id.btn_restart_camera}) |
|
|
|
|
|
public void onViewClick(View view) { |
|
|
|
|
|
int vId = view.getId(); |
|
|
|
|
|
switch (vId) { |
|
|
|
|
|
// 对比度
|
|
|
|
|
|
case R.id.btn_contrast: |
|
|
|
|
|
if(mUSBManager == null || !mUSBManager.isCameraOpened()) |
|
|
|
|
|
return; |
|
|
|
|
|
int contrast = mUSBManager.getModelValue(USBCameraManager.MODE_CONTRAST); |
|
|
|
|
|
mUSBManager.setModelValue(USBCameraManager.MODE_CONTRAST,contrast++); |
|
|
|
|
|
break; |
|
|
|
|
|
// 亮度
|
|
|
|
|
|
case R.id.btn_brightness: |
|
|
|
|
|
if(mUSBManager == null || !mUSBManager.isCameraOpened()) |
|
|
|
|
|
return; |
|
|
|
|
|
int brightness = mUSBManager.getModelValue(USBCameraManager.MODE_BRIGHTNESS); |
|
|
|
|
|
mUSBManager.setModelValue(USBCameraManager.MODE_BRIGHTNESS,brightness++); |
|
|
|
|
|
break; |
|
|
|
|
|
// 重启Camera
|
|
|
|
|
|
case R.id.btn_restart_camera: |
|
|
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
|
// 切换分辨率
|
|
|
|
|
|
case R.id.btn_update_resolution: |
|
|
|
|
|
if(mUSBManager == null || !mUSBManager.isCameraOpened()) |
|
|
|
|
|
return; |
|
|
|
|
|
mUSBManager.updateResolution(320, 240, new USBCameraManager.OnPreviewListener() { |
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onPreviewResult(boolean isSuccess) { |
|
|
public boolean onCreateOptionsMenu(Menu menu) { |
|
|
if(! isSuccess) { |
|
|
getMenuInflater().inflate(R.menu.main_toobar, menu); |
|
|
showShortMsg("预览失败,不支持该分辨率"); |
|
|
return true; |
|
|
}else { |
|
|
|
|
|
showShortMsg("以切换到分辨率为320x240"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
break; |
|
|
|
|
|
// 点击后自动对焦
|
|
|
|
|
|
case R.id.camera_view: |
|
|
|
|
|
if(mUSBManager == null) |
|
|
|
|
|
return; |
|
|
|
|
|
// mUSBManager.startCameraFoucs();
|
|
|
|
|
|
// showShortMsg("对焦相机");
|
|
|
|
|
|
List<Size> list = mUSBManager.getSupportedPreviewSizes(); |
|
|
|
|
|
if(list == null) { |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
@Override |
|
|
for(Size size:list){ |
|
|
public boolean onOptionsItemSelected(MenuItem item) { |
|
|
sb.append(size.width+"x"+size.height); |
|
|
switch (item.getItemId()) { |
|
|
sb.append("\n"); |
|
|
case R.id.menu_takepic: |
|
|
} |
|
|
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) { |
|
|
showShortMsg(sb.toString()); |
|
|
showShortMsg("sorry,camera open failed"); |
|
|
break; |
|
|
return super.onOptionsItemSelected(item); |
|
|
case R.id.btn_capture_pic: |
|
|
} |
|
|
if(mUSBManager == null || ! mUSBManager.isCameraOpened()){ |
|
|
String picPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis() |
|
|
showShortMsg("抓拍异常,摄像头未开启"); |
|
|
+ UVCCameraHelper.SUFFIX_PNG; |
|
|
return; |
|
|
mCameraHelper.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() { |
|
|
} |
|
|
|
|
|
String picPath = USBCameraManager.ROOT_PATH+System.currentTimeMillis() |
|
|
|
|
|
+USBCameraManager.SUFFIX_PNG; |
|
|
|
|
|
mUSBManager.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() { |
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onCaptureResult(String path) { |
|
|
public void onCaptureResult(String path) { |
|
|
showShortMsg("保存路径:"+path); |
|
|
showShortMsg("save path:" + path); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
break; |
|
|
break; |
|
|
case R.id.btn_rec_video: |
|
|
case R.id.menu_recording: |
|
|
if(mUSBManager == null || ! mUSBManager.isCameraOpened()){ |
|
|
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) { |
|
|
showShortMsg("录制异常,摄像头未开启"); |
|
|
showShortMsg("sorry,camera open failed"); |
|
|
return; |
|
|
return super.onOptionsItemSelected(item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(! mUSBManager.isRecording()){ |
|
|
if (!mCameraHelper.isRecording()) { |
|
|
String videoPath = USBCameraManager.ROOT_PATH+System.currentTimeMillis(); |
|
|
String videoPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis(); |
|
|
FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264"); |
|
|
FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264"); |
|
|
RecordParams params = new RecordParams(); |
|
|
RecordParams params = new RecordParams(); |
|
|
params.setRecordPath(videoPath); |
|
|
params.setRecordPath(videoPath); |
|
|
params.setRecordDuration(0); // 设置为0,不分割保存
|
|
|
params.setRecordDuration(0); // 设置为0,不分割保存
|
|
|
params.setVoiceClose(false); // 不屏蔽声音
|
|
|
params.setVoiceClose(false); // 不屏蔽声音
|
|
|
mUSBManager.startRecording(params, new AbstractUVCCameraHandler.OnEncodeResultListener() { |
|
|
mCameraHelper.startRecording(params, new AbstractUVCCameraHandler.OnEncodeResultListener() { |
|
|
@Override |
|
|
@Override |
|
|
public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) { |
|
|
public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) { |
|
|
// type = 0,aac格式音频流
|
|
|
// type = 0,aac stream
|
|
|
// type = 1,h264格式视频流
|
|
|
// type = 1,h264 stream
|
|
|
if (type == 1) { |
|
|
if (type == 1) { |
|
|
FileUtils.putFileStream(data, offset, length); |
|
|
FileUtils.putFileStream(data, offset, length); |
|
|
} |
|
|
} |
|
@ -260,22 +248,171 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog |
|
|
showShortMsg(videoPath); |
|
|
showShortMsg(videoPath); |
|
|
} |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
showShortMsg("start record..."); |
|
|
mBtnRecord.setText("正在录制"); |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
FileUtils.releaseFile(); |
|
|
FileUtils.releaseFile(); |
|
|
mUSBManager.stopRecording(); |
|
|
mCameraHelper.stopRecording(); |
|
|
mBtnRecord.setText("开始录制"); |
|
|
showShortMsg("stop record..."); |
|
|
|
|
|
} |
|
|
|
|
|
break; |
|
|
|
|
|
case R.id.menu_resolution: |
|
|
|
|
|
showResolutionListDialog(); |
|
|
|
|
|
break; |
|
|
|
|
|
case R.id.menu_focus: |
|
|
|
|
|
if (mCameraHelper != null) { |
|
|
|
|
|
mCameraHelper.startCameraFoucs(); |
|
|
} |
|
|
} |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
return super.onOptionsItemSelected(item); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void showResolutionListDialog() { |
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(USBCameraActivity.this); |
|
|
|
|
|
View rootView = LayoutInflater.from(USBCameraActivity.this).inflate(R.layout.layout_dialog_list, null); |
|
|
|
|
|
ListView listView = (ListView) rootView.findViewById(R.id.listview_dialog); |
|
|
|
|
|
ArrayAdapter<String> adapter = new ArrayAdapter<String>(USBCameraActivity.this, android.R.layout.simple_list_item_1, getResolutionList()); |
|
|
|
|
|
if (adapter != null) { |
|
|
|
|
|
listView.setAdapter(adapter); |
|
|
|
|
|
} |
|
|
|
|
|
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { |
|
|
|
|
|
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) |
|
|
|
|
|
return; |
|
|
|
|
|
final String resolution = (String) adapterView.getItemAtPosition(position); |
|
|
|
|
|
String[] tmp = resolution.split("x"); |
|
|
|
|
|
if (tmp != null && tmp.length >= 2) { |
|
|
|
|
|
int widht = Integer.valueOf(tmp[0]); |
|
|
|
|
|
int height = Integer.valueOf(tmp[1]); |
|
|
|
|
|
mCameraHelper.updateResolution(widht, height, new UVCCameraHelper.OnPreviewListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onPreviewResult(boolean isSuccess) { |
|
|
|
|
|
if (isSuccess) { |
|
|
|
|
|
showShortMsg("update resolution to " + resolution + " success"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
mDialog.dismiss(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
builder.setView(rootView); |
|
|
|
|
|
mDialog = builder.create(); |
|
|
|
|
|
mDialog.show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// example: {640x480,320x240,etc}
|
|
|
|
|
|
private List<String> getResolutionList() { |
|
|
|
|
|
List<Size> list = mCameraHelper.getSupportedPreviewSizes(); |
|
|
|
|
|
List<String> resolutions = null; |
|
|
|
|
|
if (list != null && list.size() != 0) { |
|
|
|
|
|
resolutions = new ArrayList<>(); |
|
|
|
|
|
for (Size size : list) { |
|
|
|
|
|
if (size != null) { |
|
|
|
|
|
resolutions.add(size.width + "x" + size.height); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return resolutions; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// @OnClick({R.id.btn_contrast, R.id.btn_brightness, R.id.btn_capture_pic, R.id.btn_rec_video, R.id.btn_update_resolution, R.id.btn_restart_camera})
|
|
|
|
|
|
// public void onViewClick(View view) {
|
|
|
|
|
|
// int vId = view.getId();
|
|
|
|
|
|
// switch (vId) {
|
|
|
|
|
|
// case R.id.btn_contrast:
|
|
|
|
|
|
// if (mUSBManager == null || !mUSBManager.isCameraOpened())
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// int contrast = mUSBManager.getModelValue(UVCCameraHelper.MODE_CONTRAST);
|
|
|
|
|
|
// mUSBManager.setModelValue(UVCCameraHelper.MODE_CONTRAST, contrast++);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case R.id.btn_brightness:
|
|
|
|
|
|
// if (mUSBManager == null || !mUSBManager.isCameraOpened())
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// int brightness = mUSBManager.getModelValue(UVCCameraHelper.MODE_BRIGHTNESS);
|
|
|
|
|
|
// mUSBManager.setModelValue(UVCCameraHelper.MODE_BRIGHTNESS, brightness++);
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case R.id.btn_update_resolution:
|
|
|
|
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case R.id.camera_view:
|
|
|
|
|
|
// if (mUSBManager == null)
|
|
|
|
|
|
// return;
|
|
|
|
|
|
//// mUSBManager.startCameraFoucs();
|
|
|
|
|
|
//// showShortMsg("对焦相机");
|
|
|
|
|
|
// List<Size> list = mUSBManager.getSupportedPreviewSizes();
|
|
|
|
|
|
// if (list == null) {
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
// for (Size size : list) {
|
|
|
|
|
|
// sb.append(size.width + "x" + size.height);
|
|
|
|
|
|
// sb.append("\n");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// showShortMsg(sb.toString());
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case R.id.btn_capture_pic:
|
|
|
|
|
|
// if (mUSBManager == null || !mUSBManager.isCameraOpened()) {
|
|
|
|
|
|
// showShortMsg("抓拍异常,摄像头未开启");
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// String picPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis()
|
|
|
|
|
|
// + UVCCameraHelper.SUFFIX_PNG;
|
|
|
|
|
|
// mUSBManager.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() {
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public void onCaptureResult(String path) {
|
|
|
|
|
|
// showShortMsg("保存路径:" + path);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// case R.id.btn_rec_video:
|
|
|
|
|
|
// if (mUSBManager == null || !mUSBManager.isCameraOpened()) {
|
|
|
|
|
|
// showShortMsg("录制异常,摄像头未开启");
|
|
|
|
|
|
// return;
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// if (!mUSBManager.isRecording()) {
|
|
|
|
|
|
// String videoPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis();
|
|
|
|
|
|
// FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264");
|
|
|
|
|
|
// RecordParams params = new RecordParams();
|
|
|
|
|
|
// params.setRecordPath(videoPath);
|
|
|
|
|
|
// params.setRecordDuration(0); // 设置为0,不分割保存
|
|
|
|
|
|
// params.setVoiceClose(false); // 不屏蔽声音
|
|
|
|
|
|
// mUSBManager.startRecording(params, new AbstractUVCCameraHandler.OnEncodeResultListener() {
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
|
|
|
|
|
|
// // type = 0,aac格式音频流
|
|
|
|
|
|
// // type = 1,h264格式视频流
|
|
|
|
|
|
// if (type == 1) {
|
|
|
|
|
|
// FileUtils.putFileStream(data, offset, length);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
//
|
|
|
|
|
|
// @Override
|
|
|
|
|
|
// public void onRecordResult(String videoPath) {
|
|
|
|
|
|
// showShortMsg(videoPath);
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
//
|
|
|
|
|
|
// mBtnRecord.setText("正在录制");
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// FileUtils.releaseFile();
|
|
|
|
|
|
// mUSBManager.stopRecording();
|
|
|
|
|
|
// mBtnRecord.setText("开始录制");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// break;
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
protected void onDestroy() { |
|
|
protected void onDestroy() { |
|
|
super.onDestroy(); |
|
|
super.onDestroy(); |
|
|
if(mUSBManager != null){ |
|
|
// step.4 release uvc camera resources
|
|
|
mUSBManager.release(); |
|
|
if (mCameraHelper != null) { |
|
|
|
|
|
mCameraHelper.release(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -285,7 +422,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public USBMonitor getUSBMonitor() { |
|
|
public USBMonitor getUSBMonitor() { |
|
|
return mUSBManager.getUSBMonitor(); |
|
|
return mCameraHelper.getUSBMonitor(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
@ -296,6 +433,32 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public boolean isCameraOpened() { |
|
|
public boolean isCameraOpened() { |
|
|
return mUSBManager.isCameraOpened(); |
|
|
return mCameraHelper.isCameraOpened(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onSurfaceCreated(CameraViewInterface view, Surface surface) { |
|
|
|
|
|
if (!isPreview && mCameraHelper.isCameraOpened()) { |
|
|
|
|
|
mCameraHelper.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { |
|
|
|
|
|
@Override |
|
|
|
|
|
public void onPreviewResult(boolean result) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
isPreview = true; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public void onSurfaceDestroy(CameraViewInterface view, Surface surface) { |
|
|
|
|
|
if (isPreview && mCameraHelper.isCameraOpened()) { |
|
|
|
|
|
mCameraHelper.stopPreview(); |
|
|
|
|
|
isPreview = false; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|