Browse Source

优化Camera创建预览流程,使Camera创建使用更加灵活

main v1.3.0
jiangdongguo 7 years ago
parent
commit
82b2e8f68b
  1. 10
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 6
      libusbcamera/libusbcamera.iml
  3. 64
      libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java

10
app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java

@ -93,7 +93,6 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
if(! isConnected) { if(! isConnected) {
showShortMsg("连接失败,请检查分辨率参数是否正确"); showShortMsg("连接失败,请检查分辨率参数是否正确");
} }
showShortMsg("连接成功");
} }
// 与USB设备断开连接 // 与USB设备断开连接
@ -108,10 +107,11 @@ 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);
mUVCCameraView = (CameraViewInterface) mTextureView;
// 初始化引擎 // 初始化引擎
mUSBManager = USBCameraManager.getInstance(); mUSBManager = USBCameraManager.getInstance();
mUVCCameraView = (CameraViewInterface) mTextureView; mUSBManager.initUSBMonitor(this,listener);
mUSBManager.init(this, mUVCCameraView, listener); mUSBManager.createUVCCamera(mUVCCameraView);
} }
@Override @Override
@ -140,7 +140,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
case R.id.btn_restart_camera: case R.id.btn_restart_camera:
if(mUSBManager == null) if(mUSBManager == null)
return; return;
mUSBManager.restartUSBCamera(USBCameraActivity.this, mUVCCameraView, new USBCameraManager.OnPreviewListener() { mUSBManager.restartUSBCamera(mUVCCameraView,new USBCameraManager.OnPreviewListener() {
@Override @Override
public void onPreviewResult(boolean isSuccess) { public void onPreviewResult(boolean isSuccess) {
if(isSuccess) { if(isSuccess) {
@ -153,7 +153,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
case R.id.btn_update_resolution: case R.id.btn_update_resolution:
if(mUSBManager == null) if(mUSBManager == null)
return; return;
mUSBManager.updateResolution(this, mUVCCameraView, 320, 240, new USBCameraManager.OnPreviewListener() { mUSBManager.updateResolution(320, 240, new USBCameraManager.OnPreviewListener() {
@Override @Override
public void onPreviewResult(boolean isSuccess) { public void onPreviewResult(boolean isSuccess) {
if(! isSuccess) { if(! isSuccess) {

6
libusbcamera/libusbcamera.iml

@ -83,16 +83,22 @@
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/jni" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/rs" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" /> <sourceFolder url="file://$MODULE_DIR$/src/androidTest/shaders" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/annotations" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/blame" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental-safeguard" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/res" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/shaders" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" /> <excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/transforms" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" /> <excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content> </content>
<orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 25 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

64
libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java

@ -41,9 +41,11 @@ public class USBCameraManager{
private USBMonitor mUSBMonitor; private USBMonitor mUSBMonitor;
// Camera业务逻辑处理 // Camera业务逻辑处理
private UVCCameraHandler mCameraHandler; private UVCCameraHandler mCameraHandler;
// 上下文
private Activity mActivity;
private Context mContext;
private USBMonitor.UsbControlBlock mCtrlBlock; private USBMonitor.UsbControlBlock mCtrlBlock;
private CameraViewInterface cameraView;
private USBCameraManager(){} private USBCameraManager(){}
@ -72,10 +74,8 @@ public class USBCameraManager{
* cameraView Camera要渲染的Surface * cameraView Camera要渲染的Surface
* listener USB设备检测与连接状态事件监听器 * listener USB设备检测与连接状态事件监听器
* */ * */
public void init(Activity activity, final CameraViewInterface cameraView, final OnMyDevConnectListener listener){ public void initUSBMonitor(Activity activity,final OnMyDevConnectListener listener){
if(cameraView == null) this.mActivity = activity;
throw new NullPointerException("CameraViewInterface cannot be null!");
mContext = activity.getApplicationContext();
mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() {
@ -105,9 +105,7 @@ public class USBCameraManager{
startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
@Override @Override
public void onPreviewResult(boolean isConnected) { 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 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); cameraView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE,
previewWidth,previewHeight,PREVIEW_FORMAT); 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 // 如果分辨率无变化,则无需重启Camera
if(previewWidth == width && previewHeight == height){ if(previewWidth == width && previewHeight == height){
return; return;
@ -146,7 +159,7 @@ public class USBCameraManager{
} }
// 重新初始化mCameraHandler // 重新初始化mCameraHandler
cameraView.setAspectRatio(previewWidth / (float)previewHeight); cameraView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE,
previewWidth,previewHeight,PREVIEW_FORMAT); previewWidth,previewHeight,PREVIEW_FORMAT);
openCamera(mCtrlBlock); openCamera(mCtrlBlock);
// 开始预览 // 开始预览
@ -160,18 +173,21 @@ public class USBCameraManager{
}); });
} }
public void restartUSBCamera(Activity activity, CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){ public void restartUSBCamera(CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){
// 关闭摄像头 // // 关闭摄像头
closeCamera(); // closeCamera();
// 释放CameraHandler占用的相关资源 // // 释放CameraHandler占用的相关资源
if(mCameraHandler != null){ // if(mCameraHandler != null){
mCameraHandler.release(); // mCameraHandler.release();
mCameraHandler = null; // mCameraHandler = null;
} // }
// 重新初始化mCameraHandler // // 重新初始化mCameraHandler
cameraView.setAspectRatio(previewWidth / (float)previewHeight); // cameraView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE, // mCameraHandler = UVCCameraHandler.createHandler(activity,cameraView,ENCODER_TYPE,
previewWidth,previewHeight,PREVIEW_FORMAT); // previewWidth,previewHeight,PREVIEW_FORMAT);
// 创建Camera管理线程
createUVCCamera(cameraView);
// 创建Camera
openCamera(mCtrlBlock); openCamera(mCtrlBlock);
// 开始预览 // 开始预览
startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() { startPreview(cameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
@ -230,7 +246,7 @@ public class USBCameraManager{
// 返回USB设备列表 // 返回USB设备列表
private List<UsbDevice> getUsbDeviceList(){ private List<UsbDevice> getUsbDeviceList(){
List<DeviceFilter> deviceFilters = DeviceFilter.getDeviceFilters(mContext, R.xml.device_filter); List<DeviceFilter> deviceFilters = DeviceFilter.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));

Loading…
Cancel
Save