Browse Source

修复设置不同CameraViewInterface,重启Camera失败Bug

main v1.3.2
jiangdongguo 7 years ago
parent
commit
1bd2eb24ee
  1. 4
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 7
      app/src/main/res/layout/activity_usbcamera.xml
  3. 21
      libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java

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

@ -42,6 +42,8 @@ import butterknife.OnClick;
public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent{ public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent{
@BindView(R.id.camera_view) @BindView(R.id.camera_view)
public View mTextureView; public View mTextureView;
@BindView(R.id.camera_view2)
public View mTextureView2;
@BindView(R.id.btn_capture_pic) @BindView(R.id.btn_capture_pic)
public Button mBtnCapture; public Button mBtnCapture;
@BindView(R.id.btn_rec_video) @BindView(R.id.btn_rec_video)
@ -140,7 +142,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(mUVCCameraView,new USBCameraManager.OnPreviewListener() { mUSBManager.restartUSBCamera((CameraViewInterface) mTextureView2,new USBCameraManager.OnPreviewListener() {
@Override @Override
public void onPreviewResult(boolean isSuccess) { public void onPreviewResult(boolean isSuccess) {
if(isSuccess) { if(isSuccess) {

7
app/src/main/res/layout/activity_usbcamera.xml

@ -14,6 +14,13 @@
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/> android:layout_centerHorizontal="true"/>
<com.serenegiant.usb.widget.UVCCameraTextureView
android:id="@+id/camera_view2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<Button <Button
android:id="@+id/btn_capture_pic" android:id="@+id/btn_capture_pic"
android:layout_width="match_parent" android:layout_width="match_parent"

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

@ -130,15 +130,13 @@ public class USBCameraManager{
public void createUVCCamera(CameraViewInterface cameraView) { public void createUVCCamera(CameraViewInterface cameraView) {
if(cameraView == null) if(cameraView == null)
throw new NullPointerException("CameraViewInterface cannot be null!"); throw new NullPointerException("CameraViewInterface cannot be null!");
this.cameraView = cameraView;
// 关闭摄像头
closeCamera();
// 释放CameraHandler占用的相关资源 // 释放CameraHandler占用的相关资源
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.release(); mCameraHandler.release();
mCameraHandler = null; mCameraHandler = null;
} }
// 重新初始化mCameraHandler // 重新初始化mCameraHandler
this.cameraView = cameraView;
cameraView.setAspectRatio(previewWidth / (float)previewHeight); cameraView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE, mCameraHandler = UVCCameraHandler.createHandler(mActivity,cameraView,ENCODER_TYPE,
previewWidth,previewHeight,PREVIEW_FORMAT); previewWidth,previewHeight,PREVIEW_FORMAT);
@ -152,8 +150,6 @@ public class USBCameraManager{
} }
this.previewWidth = width; this.previewWidth = width;
this.previewHeight = height; this.previewHeight = height;
// 关闭摄像头
closeCamera();
// 释放CameraHandler占用的相关资源 // 释放CameraHandler占用的相关资源
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.release(); mCameraHandler.release();
@ -176,17 +172,6 @@ public class USBCameraManager{
} }
public void restartUSBCamera(CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){ 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管理线程 // 创建Camera管理线程
createUVCCamera(cameraView); createUVCCamera(cameraView);
// 创建Camera // 创建Camera
@ -293,8 +278,6 @@ public class USBCameraManager{
// 释放资源 // 释放资源
public void release(){ public void release(){
// 关闭摄像头
closeCamera();
//释放CameraHandler占用的相关资源 //释放CameraHandler占用的相关资源
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.release(); mCameraHandler.release();
@ -314,7 +297,7 @@ public class USBCameraManager{
// 关闭Camera // 关闭Camera
public void closeCamera() { public void closeCamera() {
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.close(); mCameraHandler.release();
} }
} }

Loading…
Cancel
Save