From b8a135e08180bf4ef37b4768d7b856829df37752 Mon Sep 17 00:00:00 2001
From: jiangdongguo <765067602@qq.com>
Date: Mon, 13 Nov 2017 17:06:46 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E6=AC=A1=E6=8F=92?=
=?UTF-8?q?=E6=8B=94=E8=AE=BE=E5=A4=87=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../usbcamera/view/USBCameraActivity.java | 44 +++++++++-----
.../main/res/layout/activity_usbcamera.xml | 59 +++++++++++--------
app/src/main/res/values/colors.xml | 1 +
.../jiangdg/usbcamera/USBCameraManager.java | 15 ++---
.../usb/common/AbstractUVCCameraHandler.java | 3 +-
5 files changed, 74 insertions(+), 48 deletions(-)
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 9ea4ceb..af66e9c 100644
--- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
+++ b/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();
+ }
}
diff --git a/app/src/main/res/layout/activity_usbcamera.xml b/app/src/main/res/layout/activity_usbcamera.xml
index 16c2ab1..3d5a5e6 100644
--- a/app/src/main/res/layout/activity_usbcamera.xml
+++ b/app/src/main/res/layout/activity_usbcamera.xml
@@ -1,25 +1,34 @@
+
+
+
+
-
-
+ android:layout_centerHorizontal="true"
+ android:layout_centerVertical="true" />
+ android:layout_marginRight="10dp"
+ android:text="抓拍"
+ android:textSize="16sp" />
+ android:layout_marginRight="10dp"
+ android:text="开始录制"
+ android:textSize="16sp" />
+ android:layout_marginRight="10dp"
+ android:text="切换分辨率为320x240"
+ android:textSize="16sp" />
+ android:layout_marginRight="10dp"
+ android:text="重启摄像头"
+ android:textSize="16sp" />
\ No newline at end of file
diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml
index 3ab3e9c..ed05faa 100644
--- a/app/src/main/res/values/colors.xml
+++ b/app/src/main/res/values/colors.xml
@@ -3,4 +3,5 @@
#3F51B5
#303F9F
#FF4081
+ #000000
diff --git a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java b/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java
index 8bc1d6f..fc26587 100644
--- a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java
+++ b/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) {
diff --git a/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java b/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java
index 0fbfb11..75de3fc 100644
--- a/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java
+++ b/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);
}
}
};