Browse Source

新增获取支持的分辨率,自动对焦功能

main
jiangdongguo 7 years ago
parent
commit
daf9d7b5ae
  1. 23
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 39
      libusbcamera/src/main/java/com/jiangdg/usbcamera/USBCameraManager.java
  3. 31
      libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java
  4. 5
      libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java

23
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.R;
import com.jiangdg.usbcamera.USBCameraManager; import com.jiangdg.usbcamera.USBCameraManager;
import com.serenegiant.usb.CameraDialog; import com.serenegiant.usb.CameraDialog;
import com.serenegiant.usb.Size;
import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.common.AbstractUVCCameraHandler; import com.serenegiant.usb.common.AbstractUVCCameraHandler;
import com.serenegiant.usb.encoder.RecordParams; import com.serenegiant.usb.encoder.RecordParams;
@ -26,6 +27,7 @@ import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
@ -147,16 +149,19 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog
} }
}); });
break; break;
// 开启或关闭Camera // 点击后自动对焦
case R.id.camera_view: case R.id.camera_view:
// if(mUSBManager != null){ if(mUSBManager == null)
// boolean isOpened = mUSBManager.isCameraOpened(); return;
// if(! isOpened){ // mUSBManager.startCameraFoucs();
// CameraDialog.showDialog(USBCameraActivity.this); // showShortMsg("对焦相机");
// }else { List<Size> list = mUSBManager.getSupportedPreviewSizes();
// mUSBManager.closeCamera(); StringBuilder sb = new StringBuilder();
// } for(Size size:list){
// } sb.append(size.width+"x"+size.height);
sb.append("\n");
}
showShortMsg(sb.toString());
break; break;
case R.id.btn_capture_pic: case R.id.btn_capture_pic:
if(mUSBManager == null || ! mUSBManager.isCameraOpened()){ if(mUSBManager == null || ! mUSBManager.isCameraOpened()){

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

@ -8,6 +8,7 @@ import android.os.Environment;
import com.jiangdg.libusbcamera.R; import com.jiangdg.libusbcamera.R;
import com.serenegiant.usb.DeviceFilter; import com.serenegiant.usb.DeviceFilter;
import com.serenegiant.usb.Size;
import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.common.AbstractUVCCameraHandler; import com.serenegiant.usb.common.AbstractUVCCameraHandler;
import com.serenegiant.usb.common.UVCCameraHandler; import com.serenegiant.usb.common.UVCCameraHandler;
@ -194,9 +195,7 @@ public class USBCameraManager{
} }
} }
/** // 返回USB设备列表数量
* 返回
* */
public int getUsbDeviceCount(){ public int getUsbDeviceCount(){
List<UsbDevice> devList = getUsbDeviceList(); List<UsbDevice> devList = getUsbDeviceList();
if(devList==null || devList.size() ==0){ if(devList==null || devList.size() ==0){
@ -205,6 +204,7 @@ public class USBCameraManager{
return devList.size(); return devList.size();
} }
// 返回USB设备列表
private List<UsbDevice> getUsbDeviceList(){ private List<UsbDevice> getUsbDeviceList(){
List<DeviceFilter> deviceFilters = DeviceFilter.getDeviceFilters(mContext, R.xml.device_filter); List<DeviceFilter> deviceFilters = DeviceFilter.getDeviceFilters(mContext, R.xml.device_filter);
if(mUSBMonitor == null || deviceFilters == null) if(mUSBMonitor == null || deviceFilters == null)
@ -212,28 +212,28 @@ public class USBCameraManager{
return mUSBMonitor.getDeviceList(deviceFilters.get(0)); return mUSBMonitor.getDeviceList(deviceFilters.get(0));
} }
/** // 拍照
* 抓拍照片
* */
public void capturePicture(String savePath){ public void capturePicture(String savePath){
if(mCameraHandler != null && mCameraHandler.isOpened()){ if(mCameraHandler != null && mCameraHandler.isOpened()){
mCameraHandler.captureStill(savePath); mCameraHandler.captureStill(savePath);
} }
} }
// 开始录像
public void startRecording(RecordParams params, AbstractUVCCameraHandler.OnEncodeResultListener listener){ public void startRecording(RecordParams params, AbstractUVCCameraHandler.OnEncodeResultListener listener){
if(mCameraHandler != null && ! isRecording()){ if(mCameraHandler != null && ! isRecording()){
mCameraHandler.startRecording(params,listener); mCameraHandler.startRecording(params,listener);
} }
} }
// 停止录像
public void stopRecording(){ public void stopRecording(){
if(mCameraHandler != null && isRecording()){ if(mCameraHandler != null && isRecording()){
mCameraHandler.stopRecording(); mCameraHandler.stopRecording();
} }
} }
// 是否正在录像
public boolean isRecording(){ public boolean isRecording(){
if(mCameraHandler != null){ if(mCameraHandler != null){
return mCameraHandler.isRecording(); return mCameraHandler.isRecording();
@ -241,6 +241,7 @@ public class USBCameraManager{
return false; return false;
} }
// 是否打开Camera
public boolean isCameraOpened(){ public boolean isCameraOpened(){
if(mCameraHandler != null){ if(mCameraHandler != null){
return mCameraHandler.isOpened(); return mCameraHandler.isOpened();
@ -248,11 +249,7 @@ public class USBCameraManager{
return false; return false;
} }
// 释放资源
/**
* 释放资源
* */
public void release(){ public void release(){
// 关闭摄像头 // 关闭摄像头
closeCamera(); closeCamera();
@ -272,23 +269,39 @@ public class USBCameraManager{
return mUSBMonitor; return mUSBMonitor;
} }
// 关闭Camera
public void closeCamera() { public void closeCamera() {
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.close(); mCameraHandler.close();
} }
} }
// 打开Camera
private void openCamera(USBMonitor.UsbControlBlock ctrlBlock) { private void openCamera(USBMonitor.UsbControlBlock ctrlBlock) {
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.open(ctrlBlock); mCameraHandler.open(ctrlBlock);
} }
} }
// 开始预览
public void startPreview(CameraViewInterface cameraView,AbstractUVCCameraHandler.OnPreViewResultListener mPreviewListener) { public void startPreview(CameraViewInterface cameraView,AbstractUVCCameraHandler.OnPreViewResultListener mPreviewListener) {
SurfaceTexture st = cameraView.getSurfaceTexture(); SurfaceTexture st = cameraView.getSurfaceTexture();
if(mCameraHandler != null){ if(mCameraHandler != null){
mCameraHandler.startPreview(st,mPreviewListener); mCameraHandler.startPreview(st,mPreviewListener);
} }
} }
// Camera对焦
public void startCameraFoucs(){
if(mCameraHandler != null){
mCameraHandler.startCameraFoucs();
}
}
// 获取Camera支持的所有分辨率参数
public List<Size> getSupportedPreviewSizes(){
if(mCameraHandler == null)
return null;
return mCameraHandler.getSupportedPreviewSizes();
}
} }

31
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.jiangdg.usbcamera.FileUtils;
import com.serenegiant.usb.IFrameCallback; import com.serenegiant.usb.IFrameCallback;
import com.serenegiant.usb.Size;
import com.serenegiant.usb.USBMonitor; import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.UVCCamera; import com.serenegiant.usb.UVCCamera;
import com.serenegiant.usb.encoder.MediaAudioEncoder; import com.serenegiant.usb.encoder.MediaAudioEncoder;
@ -42,6 +43,7 @@ import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet; 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 boolean DEBUG = true; // TODO set false on release
private static final String TAG = "AbsUVCCameraHandler"; private static final String TAG = "AbsUVCCameraHandler";
// 对外回调接口 // 对外回调接口
public interface CameraCallback { public interface CameraCallback {
public void onOpen(); 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_CAPTURE_STOP = 6;
private static final int MSG_MEDIA_UPDATE = 7; private static final int MSG_MEDIA_UPDATE = 7;
private static final int MSG_RELEASE = 9; 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_START = 10;
// private static final int MSG_AUDIO_STOP = 11; // private static final int MSG_AUDIO_STOP = 11;
@ -225,6 +229,14 @@ public abstract class AbstractUVCCameraHandler extends Handler {
sendEmptyMessage(MSG_CAPTURE_STOP); sendEmptyMessage(MSG_CAPTURE_STOP);
} }
public void startCameraFoucs(){
sendEmptyMessage(MSG_CAMERA_FOUCS);
}
public List<Size> getSupportedPreviewSizes(){
return mWeakThread.get().getSupportedSizes();
}
// // 启动音频线程 // // 启动音频线程
// public void startAudioThread(){ // public void startAudioThread(){
// sendEmptyMessage(MSG_AUDIO_START); // sendEmptyMessage(MSG_AUDIO_START);
@ -350,6 +362,10 @@ public abstract class AbstractUVCCameraHandler extends Handler {
case MSG_RELEASE: case MSG_RELEASE:
thread.handleRelease(); thread.handleRelease();
break; break;
// 自动对焦
case MSG_CAMERA_FOUCS:
thread.handleCameraFoucs();
break;
// 音频线程 // 音频线程
// case MSG_AUDIO_START: // case MSG_AUDIO_START:
// thread.startAudioRecord(); // thread.startAudioRecord();
@ -820,6 +836,21 @@ public abstract class AbstractUVCCameraHandler extends Handler {
if (DEBUG) Log.v(TAG_THREAD, "handleRelease:finished"); 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<Size> getSupportedSizes() {
if ((mUVCCamera == null) || !mIsPreviewing)
return null;
return mUVCCamera.getSupportedSizeList();
}
private final MediaEncoder.MediaEncoderListener mMediaEncoderListener = new MediaEncoder.MediaEncoderListener() { private final MediaEncoder.MediaEncoderListener mMediaEncoderListener = new MediaEncoder.MediaEncoderListener() {
@Override @Override
public void onPrepared(final MediaEncoder encoder) { public void onPrepared(final MediaEncoder encoder) {

5
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) { public void captureStill(final String path) {
super.captureStill(path); super.captureStill(path);
} }
@Override
public void startCameraFoucs() {
super.startCameraFoucs();
}
} }

Loading…
Cancel
Save