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 c360d81..bb5ba33 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -139,7 +139,6 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog mUVCCameraView = (CameraViewInterface) mTextureView; mUVCCameraView.setCallback(this); mCameraHelper = UVCCameraHelper.getInstance(); - mCameraHelper.setDefaultPreviewSize(320,240); mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener); diff --git a/app/src/main/res/layout/activity_usbcamera.xml b/app/src/main/res/layout/activity_usbcamera.xml index 1d7e7b4..ada9f5b 100644 --- a/app/src/main/res/layout/activity_usbcamera.xml +++ b/app/src/main/res/layout/activity_usbcamera.xml @@ -17,39 +17,42 @@ app:navigationIcon="@null" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> - + android:layout_height="match_parent" + android:layout_below="@id/toolbar"> + + + + android:text="对比度(contrast)" + android:textColor="@color/colorWhite" /> + android:layout_height="match_parent" + android:layout_weight="2" /> + android:layout_height="match_parent" + android:layout_weight="2" /> + android:text="closeVoice" + android:textColor="@color/colorWhite" /> \ No newline at end of file diff --git a/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java b/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java index 44ca652..194ff46 100644 --- a/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java +++ b/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java @@ -32,6 +32,10 @@ public class UVCCameraHelper { private static final String TAG = "UVCCameraHelper"; private int previewWidth = 640; private int previewHeight = 480; + // 高分辨率YUV格式帧率较低 + private static int FRAME_FORMAT_YUYV = UVCCamera.FRAME_FORMAT_YUYV; + // 默认使用MJPEG + private static int FRAME_FORMAT_MJPEG = UVCCamera.FRAME_FORMAT_MJPEG; public static int MODE_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS; public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST; @@ -42,8 +46,8 @@ public class UVCCameraHelper { private UVCCameraHandler mCameraHandler; private USBMonitor.UsbControlBlock mCtrlBlock; - private WeakReference mActivityWrf; - private WeakReference mCamViewWrf; + private Activity mActivity; + private CameraViewInterface mCamView; private UVCCameraHelper() { } @@ -72,8 +76,8 @@ public class UVCCameraHelper { } public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) { - this.mActivityWrf = new WeakReference<>(activity); - this.mCamViewWrf = new WeakReference<>(cameraView); + this.mActivity = activity; + this.mCamView = cameraView; mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { // called by checking usb device @@ -100,7 +104,19 @@ public class UVCCameraHelper { public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) { mCtrlBlock = ctrlBlock; openCamera(ctrlBlock); - startPreview(mCamViewWrf.get()); + new Thread(new Runnable() { + @Override + public void run() { + // 休眠500ms,等待Camera创建完毕 + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + // 开启预览 + startPreview(mCamView); + } + }).start(); if(listener != null) { listener.onConnectDev(device,true); } @@ -124,7 +140,7 @@ public class UVCCameraHelper { } public void createUVCCamera() { - if (mCamViewWrf.get() == null) + if (mCamView == null) throw new NullPointerException("CameraViewInterface cannot be null!"); // release resources for initializing camera handler @@ -133,9 +149,9 @@ public class UVCCameraHelper { mCameraHandler = null; } // initialize camera handler -// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight); - mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, - previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); + mCamView.setAspectRatio(previewWidth / (float)previewHeight); + mCameraHandler = UVCCameraHandler.createHandler(mActivity, mCamView, 2, + previewWidth, previewHeight, FRAME_FORMAT_MJPEG); } public void updateResolution(int width, int height) { @@ -148,11 +164,23 @@ public class UVCCameraHelper { mCameraHandler.release(); mCameraHandler = null; } -// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight); - mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, - previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); + mCamView.setAspectRatio(previewWidth / (float)previewHeight); + mCameraHandler = UVCCameraHandler.createHandler(mActivity,mCamView, 2, + previewWidth, previewHeight, FRAME_FORMAT_MJPEG); openCamera(mCtrlBlock); - startPreview(mCamViewWrf.get()); + new Thread(new Runnable() { + @Override + public void run() { + // 休眠500ms,等待Camera创建完毕 + try { + Thread.sleep(500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + // 开启预览 + startPreview(mCamView); + } + }).start(); } public void registerUSB() { @@ -206,7 +234,7 @@ public class UVCCameraHelper { public List getUsbDeviceList() { List deviceFilters = DeviceFilter - .getDeviceFilters(mActivityWrf.get().getApplicationContext(), R.xml.device_filter); + .getDeviceFilters(mActivity.getApplicationContext(), R.xml.device_filter); if (mUSBMonitor == null || deviceFilters == null) return null; return mUSBMonitor.getDeviceList(deviceFilters.get(0)); @@ -245,8 +273,6 @@ public class UVCCameraHelper { } public void release() { - mCamViewWrf.clear(); - mActivityWrf.clear(); if (mCameraHandler != null) { mCameraHandler.release(); mCameraHandler = null; diff --git a/libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java b/libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java index 2c0e935..15a4180 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java @@ -48,7 +48,7 @@ public class UVCCamera { public static final int DEFAULT_PREVIEW_HEIGHT = 480; public static final int DEFAULT_PREVIEW_MODE = 0; public static final int DEFAULT_PREVIEW_MIN_FPS = 1; - public static final int DEFAULT_PREVIEW_MAX_FPS = 30; + public static final int DEFAULT_PREVIEW_MAX_FPS = 31; public static final float DEFAULT_BANDWIDTH = 1.0f; public static final int FRAME_FORMAT_YUYV = 0; @@ -127,7 +127,7 @@ public class UVCCamera { private UsbControlBlock mCtrlBlock; protected long mControlSupports; // カメラコントロールでサポートしている機能フラグ protected long mProcSupports; // プロセッシングユニットでサポートしている機能フラグ - protected int mCurrentFrameFormat = FRAME_FORMAT_YUYV; + protected int mCurrentFrameFormat = FRAME_FORMAT_MJPEG; protected int mCurrentWidth = DEFAULT_PREVIEW_WIDTH, mCurrentHeight = DEFAULT_PREVIEW_HEIGHT; protected float mCurrentBandwidthFactor = DEFAULT_BANDWIDTH; protected String mSupportedSize; 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 5286b42..b05a9ac 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java @@ -534,13 +534,13 @@ public abstract class AbstractUVCCameraHandler extends Handler { // mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_NV21); mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_YUV420SP); } catch (final IllegalArgumentException e) { -// try { -// // fallback to YUV mode -// mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, UVCCamera.DEFAULT_PREVIEW_MODE, mBandwidthFactor); -// } catch (final IllegalArgumentException e1) { -// callOnError(e1); -// return; -// } + try { + // fallback to YUV mode + mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, UVCCamera.DEFAULT_PREVIEW_MODE, mBandwidthFactor); + } catch (final IllegalArgumentException e1) { + callOnError(e1); + return; + } } if (surface instanceof SurfaceHolder) { mUVCCamera.setPreviewDisplay((SurfaceHolder)surface); 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 4403ee8..2d01f1c 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java @@ -42,7 +42,7 @@ public class UVCCameraHandler extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int width, final int height) { - return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH); + return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH); } /** @@ -58,7 +58,7 @@ public class UVCCameraHandler extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int width, final int height, final float bandwidthFactor) { - return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, bandwidthFactor); + return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, bandwidthFactor); } /** @@ -74,7 +74,7 @@ public class UVCCameraHandler extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int encoderType, final int width, final int height) { - return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH); + return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH); } /** diff --git a/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java b/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java index 428908c..b85aaa0 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java @@ -43,7 +43,7 @@ public class UVCCameraHandlerMultiSurface extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int width, final int height) { - return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH); + return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH); } /** @@ -59,7 +59,7 @@ public class UVCCameraHandlerMultiSurface extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int width, final int height, final float bandwidthFactor) { - return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, bandwidthFactor); + return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, bandwidthFactor); } /** @@ -75,7 +75,7 @@ public class UVCCameraHandlerMultiSurface extends AbstractUVCCameraHandler { final Activity parent, final CameraViewInterface cameraView, final int encoderType, final int width, final int height) { - return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH); + return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH); } /**