From ebaab6480991b7e73bd9e4abb45c8249beddc8fc Mon Sep 17 00:00:00 2001 From: jiangdongguo <765067602@qq.com> Date: Wed, 28 Feb 2018 16:12:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE=EF=BC=8C?= =?UTF-8?q?=E5=8C=85=E6=8B=AC=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E4=BC=98?= =?UTF-8?q?=E5=8C=96=EF=BC=8C=E5=88=86=E8=BE=A8=E7=8E=87=E5=88=87=E6=8D=A2?= =?UTF-8?q?=EF=BC=8C=E7=94=BB=E9=9D=A2=E5=B0=BA=E5=AF=B8=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 4 +- .../usbcamera/view/USBCameraActivity.java | 477 ++++++++++++------ .../main/res/layout/activity_usbcamera.xml | 92 ++-- .../main/res/layout/layout_dialog_list.xml | 9 + app/src/main/res/menu/main_toobar.xml | 20 + app/src/main/res/values/colors.xml | 3 +- app/src/main/res/values/strings.xml | 2 +- app/src/main/res/values/styles.xml | 2 +- build.gradle | 2 +- libusbcamera/build.gradle | 2 +- .../jiangdg/usbcamera/USBCameraManager.java | 360 ------------- .../jiangdg/usbcamera/UVCCameraHelper.java | 311 ++++++++++++ 12 files changed, 717 insertions(+), 567 deletions(-) create mode 100644 app/src/main/res/layout/layout_dialog_list.xml create mode 100644 app/src/main/res/menu/main_toobar.xml delete mode 100644 libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java create mode 100644 libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java diff --git a/app/build.gradle b/app/build.gradle index b873162..015f4f1 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,7 +2,7 @@ apply plugin: 'com.android.application' android { compileSdkVersion 25 - buildToolsVersion "26.0.1" + buildToolsVersion '26.0.2' defaultConfig { applicationId "com.jiangdg.usbcamera" minSdkVersion 16 @@ -28,5 +28,5 @@ dependencies { testCompile 'junit:junit:4.12' compile project(':libusbcamera') compile 'com.jakewharton:butterknife:8.8.1' - compile 'com.jakewharton:butterknife-compiler:8.8.1' + annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' } 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 7a71ae4..ef24337 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -1,17 +1,29 @@ package com.jiangdg.usbcamera.view; +import android.content.Intent; import android.hardware.usb.UsbDevice; import android.os.Bundle; +import android.os.Looper; import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; import android.view.Surface; import android.view.View; -import android.widget.Button; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.SeekBar; +import android.widget.SimpleAdapter; import android.widget.Toast; -import com.jiangdg.usbcamera.utils.FileUtils; import com.jiangdg.usbcamera.R; -import com.jiangdg.usbcamera.USBCameraManager; +import com.jiangdg.usbcamera.UVCCameraHelper; +import com.jiangdg.usbcamera.utils.FileUtils; import com.serenegiant.usb.CameraDialog; import com.serenegiant.usb.Size; import com.serenegiant.usb.USBMonitor; @@ -19,87 +31,92 @@ import com.serenegiant.usb.common.AbstractUVCCameraHandler; import com.serenegiant.usb.encoder.RecordParams; import com.serenegiant.usb.widget.CameraViewInterface; +import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; -import butterknife.OnClick; /** - * AndroidUSBCamera引擎使用Demo - * + * UVCCamera use demo + *
* Created by jiangdongguo on 2017/9/30.
*/
-public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent{
+public class USBCameraActivity extends AppCompatActivity implements CameraDialog.CameraDialogParent, CameraViewInterface.Callback {
@BindView(R.id.camera_view)
public View mTextureView;
- @BindView(R.id.btn_capture_pic)
- public Button mBtnCapture;
- @BindView(R.id.btn_rec_video)
- public Button mBtnRecord;
- @BindView(R.id.btn_update_resolution)
- 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;
-
+ @BindView(R.id.toolbar)
+ public Toolbar mToolbar;
+ @BindView(R.id.seekbar_brightness)
+ public SeekBar mSeekBrightness;
+ @BindView(R.id.seekbar_contrast)
+ public SeekBar mSeekContrast;
+
+ private UVCCameraHelper mCameraHelper;
private CameraViewInterface mUVCCameraView;
+ private AlertDialog mDialog;
private boolean isRequest;
private boolean isPreview;
- /**
- * USB设备事件监听器
- * */
- private USBCameraManager.OnMyDevConnectListener listener = new USBCameraManager.OnMyDevConnectListener() {
- // 插入USB设备
+ private UVCCameraHelper.OnMyDevConnectListener listener = new UVCCameraHelper.OnMyDevConnectListener() {
+
@Override
public void onAttachDev(UsbDevice device) {
- if(mUSBManager == null || mUSBManager.getUsbDeviceCount() == 0){
- showShortMsg("未检测到USB摄像头设备");
+ if (mCameraHelper == null || mCameraHelper.getUsbDeviceCount() == 0) {
+ showShortMsg("check no usb camera");
return;
}
- // 请求打开摄像头
- if(! isRequest){
+ // request open permission
+ if (!isRequest) {
isRequest = true;
- if(mUSBManager != null){
- mUSBManager.requestPermission(0);
+ if (mCameraHelper != null) {
+ mCameraHelper.requestPermission(0);
}
}
}
- // 拔出USB设备
@Override
public void onDettachDev(UsbDevice device) {
- if(isRequest){
- // 关闭摄像头
+ // close camera
+ if (isRequest) {
isRequest = false;
- mUSBManager.closeCamera();
- showShortMsg(device.getDeviceName()+"已拨出");
+ mCameraHelper.closeCamera();
+ showShortMsg(device.getDeviceName() + " is out");
}
}
- // 连接USB设备成功
@Override
- public void onConnectDev(UsbDevice device,boolean isConnected) {
- if(! isConnected) {
- showShortMsg("连接失败,请检查分辨率参数是否正确");
+ public void onConnectDev(UsbDevice device, boolean isConnected) {
+ if (!isConnected) {
+ showShortMsg("fail to connect,please check resolution params");
isPreview = false;
- }else{
+ } else {
isPreview = true;
+ showShortMsg("connecting");
+ // initialize seekbar
+ // need to wait UVCCamera initialize over
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ try {
+ Thread.sleep(1500);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ Looper.prepare();
+ mSeekBrightness.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_BRIGHTNESS));
+ mSeekContrast.setProgress(mCameraHelper.getModelValue(UVCCameraHelper.MODE_CONTRAST));
+ Looper.loop();
+ }
+ }).start();
}
}
- // 与USB设备断开连接
@Override
public void onDisConnectDev(UsbDevice device) {
- showShortMsg("连接失败");
+ showShortMsg("disconnecting");
}
};
@@ -108,150 +125,121 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_usbcamera);
ButterKnife.bind(this);
+ initView();
+ // step.1 initialize UVCCameraHelper
mUVCCameraView = (CameraViewInterface) mTextureView;
- mUVCCameraView.setCallback(new CameraViewInterface.Callback() {
- @Override
- public void onSurfaceCreated(CameraViewInterface view, Surface surface) {
- if(!isPreview && mUSBManager.isCameraOpened()) {
- mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
- @Override
- public void onPreviewResult(boolean result) {
+ mUVCCameraView.setCallback(this);
+ mCameraHelper = UVCCameraHelper.getInstance();
+ mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);
+ }
- }
- });
- isPreview = true;
+ private void initView() {
+ setSupportActionBar(mToolbar);
+ ActionBar actionBar = getSupportActionBar();
+ if (actionBar != null) {
+ actionBar.setDisplayHomeAsUpEnabled(true);
+ }
+ mSeekBrightness.setMax(100);
+ mSeekBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+ @Override
+ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+ if(mCameraHelper != null && mCameraHelper.isCameraOpened()) {
+ mCameraHelper.setModelValue(UVCCameraHelper.MODE_BRIGHTNESS,progress);
}
}
@Override
- public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) {
+ public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
- public void onSurfaceDestroy(CameraViewInterface view, Surface surface) {
- if(isPreview && mUSBManager.isCameraOpened()) {
- mUSBManager.stopPreview();
- isPreview = false;
- }
+ public void onStopTrackingTouch(SeekBar seekBar) {
+
}
});
- // 初始化引擎
- mUSBManager = USBCameraManager.getInstance();
- mUSBManager.initUSBMonitor(this,listener);
- mUSBManager.createUVCCamera(mUVCCameraView);
+ mSeekContrast.setMax(100);
+ mSeekContrast.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
+ @Override
+ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
+ if(mCameraHelper != null && mCameraHelper.isCameraOpened()) {
+ mCameraHelper.setModelValue(UVCCameraHelper.MODE_CONTRAST,progress);
+ }
+ }
+
+ @Override
+ public void onStartTrackingTouch(SeekBar seekBar) {
+
+ }
+
+ @Override
+ public void onStopTrackingTouch(SeekBar seekBar) {
+ }
+ });
}
@Override
protected void onStart() {
super.onStart();
- if(mUSBManager == null)
- return;
- // 注册USB事件广播监听器
- mUSBManager.registerUSB();
- mUVCCameraView.onResume();
+ // step.2 register USB event broadcast
+ if (mCameraHelper != null) {
+ mCameraHelper.registerUSB();
+ }
}
@Override
protected void onStop() {
super.onStop();
- // 注销USB事件广播监听器
- if(mUSBManager != null){
- mUSBManager.unregisterUSB();
+ // step.3 unregister USB event broadcast
+ if (mCameraHelper != null) {
+ mCameraHelper.unregisterUSB();
}
- mUVCCameraView.onPause();
}
- @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:
-
- break;
- // 切换分辨率
- case R.id.btn_update_resolution:
- if(mUSBManager == null || !mUSBManager.isCameraOpened())
- return;
- mUSBManager.updateResolution(320, 240, new USBCameraManager.OnPreviewListener() {
- @Override
- public void onPreviewResult(boolean isSuccess) {
- if(! isSuccess) {
- showShortMsg("预览失败,不支持该分辨率");
- }else {
- showShortMsg("以切换到分辨率为320x240");
- }
- }
- });
- break;
- // 点击后自动对焦
- case R.id.camera_view:
- if(mUSBManager == null)
- return;
-// mUSBManager.startCameraFoucs();
-// showShortMsg("对焦相机");
- List