|
@ -32,6 +32,10 @@ public class UVCCameraHelper { |
|
|
private static final String TAG = "UVCCameraHelper"; |
|
|
private static final String TAG = "UVCCameraHelper"; |
|
|
private int previewWidth = 640; |
|
|
private int previewWidth = 640; |
|
|
private int previewHeight = 480; |
|
|
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_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS; |
|
|
public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST; |
|
|
public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST; |
|
|
|
|
|
|
|
@ -42,8 +46,8 @@ public class UVCCameraHelper { |
|
|
private UVCCameraHandler mCameraHandler; |
|
|
private UVCCameraHandler mCameraHandler; |
|
|
private USBMonitor.UsbControlBlock mCtrlBlock; |
|
|
private USBMonitor.UsbControlBlock mCtrlBlock; |
|
|
|
|
|
|
|
|
private WeakReference<Activity> mActivityWrf; |
|
|
private Activity mActivity; |
|
|
private WeakReference<CameraViewInterface> mCamViewWrf; |
|
|
private CameraViewInterface mCamView; |
|
|
|
|
|
|
|
|
private UVCCameraHelper() { |
|
|
private UVCCameraHelper() { |
|
|
} |
|
|
} |
|
@ -72,8 +76,8 @@ public class UVCCameraHelper { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) { |
|
|
public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) { |
|
|
this.mActivityWrf = new WeakReference<>(activity); |
|
|
this.mActivity = activity; |
|
|
this.mCamViewWrf = new WeakReference<>(cameraView); |
|
|
this.mCamView = cameraView; |
|
|
mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { |
|
|
mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { |
|
|
|
|
|
|
|
|
// called by checking usb device
|
|
|
// called by checking usb device
|
|
@ -100,7 +104,19 @@ public class UVCCameraHelper { |
|
|
public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) { |
|
|
public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) { |
|
|
mCtrlBlock = ctrlBlock; |
|
|
mCtrlBlock = ctrlBlock; |
|
|
openCamera(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) { |
|
|
if(listener != null) { |
|
|
listener.onConnectDev(device,true); |
|
|
listener.onConnectDev(device,true); |
|
|
} |
|
|
} |
|
@ -124,7 +140,7 @@ public class UVCCameraHelper { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void createUVCCamera() { |
|
|
public void createUVCCamera() { |
|
|
if (mCamViewWrf.get() == null) |
|
|
if (mCamView == null) |
|
|
throw new NullPointerException("CameraViewInterface cannot be null!"); |
|
|
throw new NullPointerException("CameraViewInterface cannot be null!"); |
|
|
|
|
|
|
|
|
// release resources for initializing camera handler
|
|
|
// release resources for initializing camera handler
|
|
@ -133,9 +149,9 @@ public class UVCCameraHelper { |
|
|
mCameraHandler = null; |
|
|
mCameraHandler = null; |
|
|
} |
|
|
} |
|
|
// initialize camera handler
|
|
|
// initialize camera handler
|
|
|
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
|
|
|
mCamView.setAspectRatio(previewWidth / (float)previewHeight); |
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, |
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivity, mCamView, 2, |
|
|
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); |
|
|
previewWidth, previewHeight, FRAME_FORMAT_MJPEG); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void updateResolution(int width, int height) { |
|
|
public void updateResolution(int width, int height) { |
|
@ -148,11 +164,23 @@ public class UVCCameraHelper { |
|
|
mCameraHandler.release(); |
|
|
mCameraHandler.release(); |
|
|
mCameraHandler = null; |
|
|
mCameraHandler = null; |
|
|
} |
|
|
} |
|
|
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
|
|
|
mCamView.setAspectRatio(previewWidth / (float)previewHeight); |
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, |
|
|
mCameraHandler = UVCCameraHandler.createHandler(mActivity,mCamView, 2, |
|
|
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); |
|
|
previewWidth, previewHeight, FRAME_FORMAT_MJPEG); |
|
|
openCamera(mCtrlBlock); |
|
|
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() { |
|
|
public void registerUSB() { |
|
@ -206,7 +234,7 @@ public class UVCCameraHelper { |
|
|
|
|
|
|
|
|
public List<UsbDevice> getUsbDeviceList() { |
|
|
public List<UsbDevice> getUsbDeviceList() { |
|
|
List<DeviceFilter> deviceFilters = DeviceFilter |
|
|
List<DeviceFilter> deviceFilters = DeviceFilter |
|
|
.getDeviceFilters(mActivityWrf.get().getApplicationContext(), R.xml.device_filter); |
|
|
.getDeviceFilters(mActivity.getApplicationContext(), R.xml.device_filter); |
|
|
if (mUSBMonitor == null || deviceFilters == null) |
|
|
if (mUSBMonitor == null || deviceFilters == null) |
|
|
return null; |
|
|
return null; |
|
|
return mUSBMonitor.getDeviceList(deviceFilters.get(0)); |
|
|
return mUSBMonitor.getDeviceList(deviceFilters.get(0)); |
|
@ -245,8 +273,6 @@ public class UVCCameraHelper { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public void release() { |
|
|
public void release() { |
|
|
mCamViewWrf.clear(); |
|
|
|
|
|
mActivityWrf.clear(); |
|
|
|
|
|
if (mCameraHandler != null) { |
|
|
if (mCameraHandler != null) { |
|
|
mCameraHandler.release(); |
|
|
mCameraHandler.release(); |
|
|
mCameraHandler = null; |
|
|
mCameraHandler = null; |
|
|