diff --git a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java index 2c2ee4b..ca809a4 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -93,7 +93,6 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog if(! isConnected) { showShortMsg("连接失败,请检查分辨率参数是否正确"); } - showShortMsg("连接成功"); } // 与USB设备断开连接 @@ -108,10 +107,11 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog super.onCreate(savedInstanceState); setContentView(R.layout.activity_usbcamera); ButterKnife.bind(this); + mUVCCameraView = (CameraViewInterface) mTextureView; // 初始化引擎 mUSBManager = USBCameraManager.getInstance(); - mUVCCameraView = (CameraViewInterface) mTextureView; - mUSBManager.init(this, mUVCCameraView, listener); + mUSBManager.initUSBMonitor(this,listener); + mUSBManager.createUVCCamera(mUVCCameraView); } @Override @@ -140,7 +140,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog case R.id.btn_restart_camera: if(mUSBManager == null) return; - mUSBManager.restartUSBCamera(USBCameraActivity.this, mUVCCameraView, new USBCameraManager.OnPreviewListener() { + mUSBManager.restartUSBCamera(mUVCCameraView,new USBCameraManager.OnPreviewListener() { @Override public void onPreviewResult(boolean isSuccess) { if(isSuccess) { @@ -153,7 +153,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog case R.id.btn_update_resolution: if(mUSBManager == null) return; - mUSBManager.updateResolution(this, mUVCCameraView, 320, 240, new USBCameraManager.OnPreviewListener() { + mUSBManager.updateResolution(320, 240, new USBCameraManager.OnPreviewListener() { @Override public void onPreviewResult(boolean isSuccess) { if(! isSuccess) { diff --git a/libusbcamera/libusbcamera.iml b/libusbcamera/libusbcamera.iml index a7b8a41..013d57c 100644 --- a/libusbcamera/libusbcamera.iml +++ b/libusbcamera/libusbcamera.iml @@ -83,16 +83,22 @@ + + + + + + diff --git a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java b/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java index 064df07..484bd2c 100644 --- a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java +++ b/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java @@ -41,9 +41,11 @@ public class USBCameraManager{ private USBMonitor mUSBMonitor; // Camera业务逻辑处理 private UVCCameraHandler mCameraHandler; + // 上下文 + private Activity mActivity; - private Context mContext; private USBMonitor.UsbControlBlock mCtrlBlock; + private CameraViewInterface cameraView; private USBCameraManager(){} @@ -72,10 +74,8 @@ public class USBCameraManager{ * cameraView Camera要渲染的Surface * listener USB设备检测与连接状态事件监听器 * */ - public void init(Activity activity, final CameraViewInterface cameraView, final OnMyDevConnectListener listener){ - if(cameraView == null) - throw new NullPointerException("CameraViewInterface cannot be null!"); - mContext = activity.getApplicationContext(); + public void initUSBMonitor(Activity activity,final OnMyDevConnectListener listener){ + this.mActivity = activity; mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { @@ -105,9 +105,7 @@ public class USBCameraManager{ startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { @Override public void onPreviewResult(boolean isConnected) { - if(listener != null){ - listener.onConnectDev(device,isConnected); - } + } }); } @@ -124,13 +122,28 @@ public class USBCameraManager{ public void onCancel(UsbDevice device) { } }); + + } + + public void createUVCCamera(CameraViewInterface cameraView) { + if(cameraView == null) + throw new NullPointerException("CameraViewInterface cannot be null!"); + this.cameraView = cameraView; + // 关闭摄像头 + closeCamera(); + // 释放CameraHandler占用的相关资源 + if(mCameraHandler != null){ + mCameraHandler.release(); + mCameraHandler = null; + } + // 重新初始化mCameraHandler cameraView.setAspectRatio(previewWidth / (float)previewHeight); - mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, + mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE, previewWidth,previewHeight,PREVIEW_FORMAT); } // 切换分辨率 - public void updateResolution(Activity activity, CameraViewInterface cameraView, int width, int height, final OnPreviewListener mPreviewListener){ + public void updateResolution(int width, int height, final OnPreviewListener mPreviewListener){ // 如果分辨率无变化,则无需重启Camera if(previewWidth == width && previewHeight == height){ return; @@ -146,7 +159,7 @@ public class USBCameraManager{ } // 重新初始化mCameraHandler cameraView.setAspectRatio(previewWidth / (float)previewHeight); - mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, + mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE, previewWidth,previewHeight,PREVIEW_FORMAT); openCamera(mCtrlBlock); // 开始预览 @@ -160,18 +173,21 @@ public class USBCameraManager{ }); } - public void restartUSBCamera(Activity activity, CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){ - // 关闭摄像头 - closeCamera(); - // 释放CameraHandler占用的相关资源 - if(mCameraHandler != null){ - mCameraHandler.release(); - mCameraHandler = null; - } - // 重新初始化mCameraHandler - cameraView.setAspectRatio(previewWidth / (float)previewHeight); - mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, - previewWidth,previewHeight,PREVIEW_FORMAT); + public void restartUSBCamera(CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){ +// // 关闭摄像头 +// closeCamera(); +// // 释放CameraHandler占用的相关资源 +// if(mCameraHandler != null){ +// mCameraHandler.release(); +// mCameraHandler = null; +// } +// // 重新初始化mCameraHandler +// cameraView.setAspectRatio(previewWidth / (float)previewHeight); +// mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, +// previewWidth,previewHeight,PREVIEW_FORMAT); + // 创建Camera管理线程 + createUVCCamera(cameraView); + // 创建Camera openCamera(mCtrlBlock); // 开始预览 startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { @@ -230,7 +246,7 @@ public class USBCameraManager{ // 返回USB设备列表 private List getUsbDeviceList(){ - List deviceFilters = DeviceFilter.getDeviceFilters(mContext, R.xml.device_filter); + List deviceFilters = DeviceFilter.getDeviceFilters(mActivity.getApplicationContext(), R.xml.device_filter); if(mUSBMonitor == null || deviceFilters == null) return null; return mUSBMonitor.getDeviceList(deviceFilters.get(0));