|
|
@ -32,6 +32,10 @@ public class UVCCameraHelper { |
|
|
|
private static final String TAG = "UVCCameraHelper"; |
|
|
|
private int previewWidth = 640; |
|
|
|
private int previewHeight = 480; |
|
|
|
// 高分辨率YUV格式帧率较低
|
|
|
|
private static int FRAME_FORMAT_YUYV = UVCCamera.FRAME_FORMAT_YUYV; |
|
|
|
// 默认使用MJPEG
|
|
|
|
private static int FRAME_FORMAT_MJPEG = UVCCamera.FRAME_FORMAT_MJPEG; |
|
|
|
public static int MODE_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS; |
|
|
|
public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST; |
|
|
|
|
|
|
@ -42,8 +46,8 @@ public class UVCCameraHelper { |
|
|
|
private UVCCameraHandler mCameraHandler; |
|
|
|
private USBMonitor.UsbControlBlock mCtrlBlock; |
|
|
|
|
|
|
|
private WeakReference<Activity> mActivityWrf; |
|
|
|
private WeakReference<CameraViewInterface> mCamViewWrf; |
|
|
|
private Activity mActivity; |
|
|
|
private CameraViewInterface mCamView; |
|
|
|
|
|
|
|
private UVCCameraHelper() { |
|
|
|
} |
|
|
@ -72,8 +76,8 @@ public class UVCCameraHelper { |
|
|
|
} |
|
|
|
|
|
|
|
public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) { |
|
|
|
this.mActivityWrf = new WeakReference<>(activity); |
|
|
|
this.mCamViewWrf = new WeakReference<>(cameraView); |
|
|
|
this.mActivity = activity; |
|
|
|
this.mCamView = cameraView; |
|
|
|
mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { |
|
|
|
|
|
|
|
// called by checking usb device
|
|
|
@ -100,7 +104,19 @@ public class UVCCameraHelper { |
|
|
|
public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) { |
|
|
|
mCtrlBlock = ctrlBlock; |
|
|
|
openCamera(ctrlBlock); |
|
|
|
startPreview(mCamViewWrf.get()); |
|
|
|
new Thread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
// 休眠500ms,等待Camera创建完毕
|
|
|
|
try { |
|
|
|
Thread.sleep(500); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
// 开启预览
|
|
|
|
startPreview(mCamView); |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
if(listener != null) { |
|
|
|
listener.onConnectDev(device,true); |
|
|
|
} |
|
|
@ -124,7 +140,7 @@ public class UVCCameraHelper { |
|
|
|
} |
|
|
|
|
|
|
|
public void createUVCCamera() { |
|
|
|
if (mCamViewWrf.get() == null) |
|
|
|
if (mCamView == null) |
|
|
|
throw new NullPointerException("CameraViewInterface cannot be null!"); |
|
|
|
|
|
|
|
// release resources for initializing camera handler
|
|
|
@ -133,9 +149,9 @@ public class UVCCameraHelper { |
|
|
|
mCameraHandler = null; |
|
|
|
} |
|
|
|
// initialize camera handler
|
|
|
|
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
|
|
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, |
|
|
|
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); |
|
|
|
mCamView.setAspectRatio(previewWidth / (float)previewHeight); |
|
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivity, mCamView, 2, |
|
|
|
previewWidth, previewHeight, FRAME_FORMAT_MJPEG); |
|
|
|
} |
|
|
|
|
|
|
|
public void updateResolution(int width, int height) { |
|
|
@ -148,11 +164,23 @@ public class UVCCameraHelper { |
|
|
|
mCameraHandler.release(); |
|
|
|
mCameraHandler = null; |
|
|
|
} |
|
|
|
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
|
|
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, |
|
|
|
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); |
|
|
|
mCamView.setAspectRatio(previewWidth / (float)previewHeight); |
|
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivity,mCamView, 2, |
|
|
|
previewWidth, previewHeight, FRAME_FORMAT_MJPEG); |
|
|
|
openCamera(mCtrlBlock); |
|
|
|
startPreview(mCamViewWrf.get()); |
|
|
|
new Thread(new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
// 休眠500ms,等待Camera创建完毕
|
|
|
|
try { |
|
|
|
Thread.sleep(500); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
// 开启预览
|
|
|
|
startPreview(mCamView); |
|
|
|
} |
|
|
|
}).start(); |
|
|
|
} |
|
|
|
|
|
|
|
public void registerUSB() { |
|
|
@ -206,7 +234,7 @@ public class UVCCameraHelper { |
|
|
|
|
|
|
|
public List<UsbDevice> getUsbDeviceList() { |
|
|
|
List<DeviceFilter> deviceFilters = DeviceFilter |
|
|
|
.getDeviceFilters(mActivityWrf.get().getApplicationContext(), R.xml.device_filter); |
|
|
|
.getDeviceFilters(mActivity.getApplicationContext(), R.xml.device_filter); |
|
|
|
if (mUSBMonitor == null || deviceFilters == null) |
|
|
|
return null; |
|
|
|
return mUSBMonitor.getDeviceList(deviceFilters.get(0)); |
|
|
@ -245,8 +273,6 @@ public class UVCCameraHelper { |
|
|
|
} |
|
|
|
|
|
|
|
public void release() { |
|
|
|
mCamViewWrf.clear(); |
|
|
|
mActivityWrf.clear(); |
|
|
|
if (mCameraHandler != null) { |
|
|
|
mCameraHandler.release(); |
|
|
|
mCameraHandler = null; |
|
|
|