Browse Source

修复多次插拔设备报错问题

main 1.3.5
jiangdongguo 7 years ago
parent
commit
b8a135e081
  1. 44
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 59
      app/src/main/res/layout/activity_usbcamera.xml
  3. 1
      app/src/main/res/values/colors.xml
  4. 15
      libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java
  5. 3
      libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java

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

@ -6,6 +6,7 @@ import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
@ -27,6 +28,7 @@ import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
@ -42,8 +44,6 @@ import butterknife.OnClick;
public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent{
@BindView(R.id.camera_view)
public View mTextureView;
@BindView(R.id.camera_view2)
public View mTextureView2;
@BindView(R.id.btn_capture_pic)
public Button mBtnCapture;
@BindView(R.id.btn_rec_video)
@ -52,8 +52,13 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
public Button mBtnUpdateResultion;
@BindView(R.id.btn_restart_camera)
Button mBtnRestartCamera;
@BindView(R.id.btn_contrast)
Button mBtnContrast;
@BindView(R.id.btn_brightness)
Button mBtnBrightness;
private USBCameraManager mUSBManager;
private CameraViewInterface mUVCCameraView;
private boolean isRequest;
@ -116,6 +121,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
mUSBManager.createUVCCamera(mUVCCameraView);
}
@Override
protected void onStart() {
super.onStart();
@ -134,26 +140,31 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
}
}
@OnClick({R.id.camera_view, R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution,R.id.btn_restart_camera})
@OnClick({ R.id.btn_contrast,R.id.btn_brightness,R.id.btn_capture_pic, R.id.btn_rec_video,R.id.btn_update_resolution,R.id.btn_restart_camera})
public void onViewClick(View view) {
int vId = view.getId();
switch (vId) {
// 对比度
case R.id.btn_contrast:
if(mUSBManager == null || !mUSBManager.isCameraOpened())
return;
int contrast = mUSBManager.getModelValue(USBCameraManager.MODE_CONTRAST);
mUSBManager.setModelValue(USBCameraManager.MODE_CONTRAST,contrast++);
break;
// 亮度
case R.id.btn_brightness:
if(mUSBManager == null || !mUSBManager.isCameraOpened())
return;
int brightness = mUSBManager.getModelValue(USBCameraManager.MODE_BRIGHTNESS);
mUSBManager.setModelValue(USBCameraManager.MODE_BRIGHTNESS,brightness++);
break;
// 重启Camera
case R.id.btn_restart_camera:
if(mUSBManager == null)
return;
mUSBManager.restartUSBCamera((CameraViewInterface) mTextureView2,new USBCameraManager.OnPreviewListener() {
@Override
public void onPreviewResult(boolean isSuccess) {
if(isSuccess) {
showShortMsg("重启成功");
}
}
});
break;
// 切换分辨率
case R.id.btn_update_resolution:
if(mUSBManager == null)
if(mUSBManager == null || !mUSBManager.isCameraOpened())
return;
mUSBManager.updateResolution(320, 240, new USBCameraManager.OnPreviewListener() {
@Override
@ -240,7 +251,6 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
@Override
protected void onDestroy() {
super.onDestroy();
// 释放资源
if(mUSBManager != null){
mUSBManager.release();
}
@ -261,4 +271,8 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
showShortMsg("取消操作");
}
}
public boolean isCameraOpened() {
return mUSBManager.isCameraOpened();
}
}

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

@ -1,25 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="#ff000000"
tools:context=".view.USBCameraActivity"
tools:ignore="MergeRootFrame">
<Button
android:id="@+id/btn_contrast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="对比度调整" />
<Button
android:id="@+id/btn_brightness"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="亮度调整" />
<com.serenegiant.usb.widget.UVCCameraTextureView
android:id="@+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="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"/>
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<Button
android:id="@+id/btn_capture_pic"
@ -27,39 +36,39 @@
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="抓拍"/>
android:layout_marginRight="10dp"
android:text="抓拍"
android:textSize="16sp" />
<Button
android:layout_above="@id/btn_capture_pic"
android:id="@+id/btn_rec_video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_above="@id/btn_capture_pic"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="开始录制"/>
android:layout_marginRight="10dp"
android:text="开始录制"
android:textSize="16sp" />
<Button
android:layout_above="@id/btn_rec_video"
android:id="@+id/btn_update_resolution"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_above="@id/btn_rec_video"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="切换分辨率为320x240"/>
android:layout_marginRight="10dp"
android:text="切换分辨率为320x240"
android:textSize="16sp" />
<Button
android:layout_above="@id/btn_update_resolution"
android:id="@+id/btn_restart_camera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_above="@id/btn_update_resolution"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:text="重启摄像头"/>
android:layout_marginRight="10dp"
android:text="重启摄像头"
android:textSize="16sp" />
</RelativeLayout>

1
app/src/main/res/values/colors.xml

@ -3,4 +3,5 @@
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorBlack">#000000</color>
</resources>

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

@ -59,6 +59,12 @@ public class USBCameraManager{
return mUsbCamManager;
}
public void closeCamera() {
if(mCameraHandler != null){
mCameraHandler.close();
}
}
public interface OnMyDevConnectListener{
void onAttachDev(UsbDevice device);
void onDettachDev(UsbDevice device);
@ -127,12 +133,12 @@ public class USBCameraManager{
public void onCancel(UsbDevice device) {
}
});
}
public void createUVCCamera(CameraViewInterface cameraView) {
if(cameraView == null)
throw new NullPointerException("CameraViewInterface cannot be null!");
// 释放CameraHandler占用的相关资源
if(mCameraHandler != null){
mCameraHandler.release();
@ -177,6 +183,7 @@ public class USBCameraManager{
public void restartUSBCamera(CameraViewInterface cameraView,final OnPreviewListener mPreviewListener){
if(mCtrlBlock == null || cameraView == null)
throw new NullPointerException("mCtrlBlock or cameraView is null,please connected to camera");
// 创建Camera管理线程
createUVCCamera(cameraView);
// 创建Camera
@ -315,12 +322,6 @@ public class USBCameraManager{
return mUSBMonitor;
}
// 关闭Camera
public void closeCamera() {
if(mCameraHandler != null){
mCameraHandler.release();
}
}
// 打开Camera
private void openCamera(USBMonitor.UsbControlBlock ctrlBlock) {

3
libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java

@ -814,7 +814,8 @@ public abstract class AbstractUVCCameraHandler extends Handler {
byte[] yuv = new byte[len];
frame.get(yuv);
if(mH264Consumer != null){
mH264Consumer.setRawYuv(yuv,640,480);
// 修改分辨率参数
mH264Consumer.setRawYuv(yuv,mWidth,mHeight);
}
}
};

Loading…
Cancel
Save