From daf9d7b5ae28b5fe54a19d814e66b39a24298ec7 Mon Sep 17 00:00:00 2001 From: jiangdongguo <765067602@qq.com> Date: Mon, 30 Oct 2017 11:07:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=8E=B7=E5=8F=96=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=9A=84=E5=88=86=E8=BE=A8=E7=8E=87=EF=BC=8C=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E5=AF=B9=E7=84=A6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../usbcamera/view/USBCameraActivity.java | 23 ++++++----- .../jiangdg/usbcamera/USBCameraManager.java | 39 ++++++++++++------- .../usb/common/AbstractUVCCameraHandler.java | 31 +++++++++++++++ .../usb/common/UVCCameraHandler.java | 5 +++ 4 files changed, 76 insertions(+), 22 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 1f28e2d..6873691 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -16,6 +16,7 @@ import com.jiangdg.usbcamera.FileUtils; import com.jiangdg.usbcamera.R; import com.jiangdg.usbcamera.USBCameraManager; import com.serenegiant.usb.CameraDialog; +import com.serenegiant.usb.Size; import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.common.AbstractUVCCameraHandler; import com.serenegiant.usb.encoder.RecordParams; @@ -26,6 +27,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; @@ -147,16 +149,19 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog } }); break; - // 开启或关闭Camera + // 点击后自动对焦 case R.id.camera_view: -// if(mUSBManager != null){ -// boolean isOpened = mUSBManager.isCameraOpened(); -// if(! isOpened){ -// CameraDialog.showDialog(USBCameraActivity.this); -// }else { -// mUSBManager.closeCamera(); -// } -// } + if(mUSBManager == null) + return; +// mUSBManager.startCameraFoucs(); +// showShortMsg("对焦相机"); + List list = mUSBManager.getSupportedPreviewSizes(); + StringBuilder sb = new StringBuilder(); + for(Size size:list){ + sb.append(size.width+"x"+size.height); + sb.append("\n"); + } + showShortMsg(sb.toString()); break; case R.id.btn_capture_pic: if(mUSBManager == null || ! mUSBManager.isCameraOpened()){ diff --git a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java b/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java index 9c72938..4fd4a05 100644 --- a/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java +++ b/libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java @@ -8,6 +8,7 @@ import android.os.Environment; import com.jiangdg.libusbcamera.R; import com.serenegiant.usb.DeviceFilter; +import com.serenegiant.usb.Size; import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.common.AbstractUVCCameraHandler; import com.serenegiant.usb.common.UVCCameraHandler; @@ -194,9 +195,7 @@ public class USBCameraManager{ } } - /** - * 返回 - * */ + // 返回USB设备列表数量 public int getUsbDeviceCount(){ List devList = getUsbDeviceList(); if(devList==null || devList.size() ==0){ @@ -205,6 +204,7 @@ public class USBCameraManager{ return devList.size(); } + // 返回USB设备列表 private List getUsbDeviceList(){ List deviceFilters = DeviceFilter.getDeviceFilters(mContext, R.xml.device_filter); if(mUSBMonitor == null || deviceFilters == null) @@ -212,28 +212,28 @@ public class USBCameraManager{ return mUSBMonitor.getDeviceList(deviceFilters.get(0)); } - /** - * 抓拍照片 - * */ + // 拍照 public void capturePicture(String savePath){ if(mCameraHandler != null && mCameraHandler.isOpened()){ mCameraHandler.captureStill(savePath); } } + // 开始录像 public void startRecording(RecordParams params, AbstractUVCCameraHandler.OnEncodeResultListener listener){ if(mCameraHandler != null && ! isRecording()){ mCameraHandler.startRecording(params,listener); } } - + // 停止录像 public void stopRecording(){ if(mCameraHandler != null && isRecording()){ mCameraHandler.stopRecording(); } } + // 是否正在录像 public boolean isRecording(){ if(mCameraHandler != null){ return mCameraHandler.isRecording(); @@ -241,6 +241,7 @@ public class USBCameraManager{ return false; } + // 是否打开Camera public boolean isCameraOpened(){ if(mCameraHandler != null){ return mCameraHandler.isOpened(); @@ -248,11 +249,7 @@ public class USBCameraManager{ return false; } - - - /** - * 释放资源 - * */ + // 释放资源 public void release(){ // 关闭摄像头 closeCamera(); @@ -272,23 +269,39 @@ public class USBCameraManager{ return mUSBMonitor; } - + // 关闭Camera public void closeCamera() { if(mCameraHandler != null){ mCameraHandler.close(); } } + // 打开Camera private void openCamera(USBMonitor.UsbControlBlock ctrlBlock) { if(mCameraHandler != null){ mCameraHandler.open(ctrlBlock); } } + // 开始预览 public void startPreview(CameraViewInterface cameraView,AbstractUVCCameraHandler.OnPreViewResultListener mPreviewListener) { SurfaceTexture st = cameraView.getSurfaceTexture(); if(mCameraHandler != null){ mCameraHandler.startPreview(st,mPreviewListener); } } + + // Camera对焦 + public void startCameraFoucs(){ + if(mCameraHandler != null){ + mCameraHandler.startCameraFoucs(); + } + } + + // 获取Camera支持的所有分辨率参数 + public List getSupportedPreviewSizes(){ + if(mCameraHandler == null) + return null; + return mCameraHandler.getSupportedPreviewSizes(); + } } 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 d9bacab..8bbd649 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java @@ -18,6 +18,7 @@ import android.view.SurfaceHolder; import com.jiangdg.usbcamera.FileUtils; import com.serenegiant.usb.IFrameCallback; +import com.serenegiant.usb.Size; import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.UVCCamera; import com.serenegiant.usb.encoder.MediaAudioEncoder; @@ -42,6 +43,7 @@ import java.lang.reflect.InvocationTargetException; import java.nio.ByteBuffer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.List; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; @@ -54,6 +56,7 @@ public abstract class AbstractUVCCameraHandler extends Handler { private static final boolean DEBUG = true; // TODO set false on release private static final String TAG = "AbsUVCCameraHandler"; + // 对外回调接口 public interface CameraCallback { public void onOpen(); @@ -85,6 +88,7 @@ public abstract class AbstractUVCCameraHandler extends Handler { private static final int MSG_CAPTURE_STOP = 6; private static final int MSG_MEDIA_UPDATE = 7; private static final int MSG_RELEASE = 9; + private static final int MSG_CAMERA_FOUCS = 10; // 音频线程 // private static final int MSG_AUDIO_START = 10; // private static final int MSG_AUDIO_STOP = 11; @@ -225,6 +229,14 @@ public abstract class AbstractUVCCameraHandler extends Handler { sendEmptyMessage(MSG_CAPTURE_STOP); } + public void startCameraFoucs(){ + sendEmptyMessage(MSG_CAMERA_FOUCS); + } + + public List getSupportedPreviewSizes(){ + return mWeakThread.get().getSupportedSizes(); + } + // // 启动音频线程 // public void startAudioThread(){ // sendEmptyMessage(MSG_AUDIO_START); @@ -350,6 +362,10 @@ public abstract class AbstractUVCCameraHandler extends Handler { case MSG_RELEASE: thread.handleRelease(); break; + // 自动对焦 + case MSG_CAMERA_FOUCS: + thread.handleCameraFoucs(); + break; // 音频线程 // case MSG_AUDIO_START: // thread.startAudioRecord(); @@ -820,6 +836,21 @@ public abstract class AbstractUVCCameraHandler extends Handler { if (DEBUG) Log.v(TAG_THREAD, "handleRelease:finished"); } + // 自动对焦 + public void handleCameraFoucs() { + if (DEBUG) Log.v(TAG_THREAD, "handleStartPreview:"); + if ((mUVCCamera == null) || !mIsPreviewing) + return; + mUVCCamera.setAutoFocus(true); + } + + // 获取支持的分辨率 + public List getSupportedSizes() { + if ((mUVCCamera == null) || !mIsPreviewing) + return null; + return mUVCCamera.getSupportedSizeList(); + } + private final MediaEncoder.MediaEncoderListener mMediaEncoderListener = new MediaEncoder.MediaEncoderListener() { @Override public void onPrepared(final MediaEncoder encoder) { diff --git a/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java b/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java index 7617b60..55f9121 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java @@ -132,4 +132,9 @@ public class UVCCameraHandler extends AbstractUVCCameraHandler { public void captureStill(final String path) { super.captureStill(path); } + + @Override + public void startCameraFoucs() { + super.startCameraFoucs(); + } }