Browse Source

修复高分辨率切换失败问题;解决Camera预览不流畅问题

main
jiangdongguo 7 years ago
parent
commit
3fba248207
  1. 1
      app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java
  2. 33
      app/src/main/res/layout/activity_usbcamera.xml
  3. 58
      libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java
  4. 4
      libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java
  5. 14
      libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java
  6. 6
      libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java
  7. 6
      libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java

1
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 = (CameraViewInterface) mTextureView;
mUVCCameraView.setCallback(this); mUVCCameraView.setCallback(this);
mCameraHelper = UVCCameraHelper.getInstance(); mCameraHelper = UVCCameraHelper.getInstance();
mCameraHelper.setDefaultPreviewSize(320,240);
mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener); mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);

33
app/src/main/res/layout/activity_usbcamera.xml

@ -17,39 +17,42 @@
app:navigationIcon="@null" app:navigationIcon="@null"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/toolbar">
<com.serenegiant.usb.widget.UVCCameraTextureView <com.serenegiant.usb.widget.UVCCameraTextureView
android:id="@+id/camera_view" android:id="@+id/camera_view"
android:layout_below="@id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerInParent="true" android:layout_gravity="center"/>
android:layout_centerHorizontal="true" </FrameLayout>
android:layout_centerVertical="true" />
<LinearLayout <LinearLayout
android:id="@+id/llayout_contrast" android:id="@+id/llayout_contrast"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp" android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal"> android:orientation="horizontal">
<TextView <TextView
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="7dp" android:paddingBottom="7dp"
android:paddingTop="7dp" android:paddingTop="7dp"
android:layout_weight="1" android:text="对比度(contrast)"
android:textColor="@color/colorWhite" android:textColor="@color/colorWhite" />
android:text="对比度(contrast)" />
<SeekBar <SeekBar
android:id="@+id/seekbar_contrast" android:id="@+id/seekbar_contrast"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="2" android:layout_height="match_parent"
android:layout_height="match_parent" /> android:layout_weight="2" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
@ -65,17 +68,17 @@
<TextView <TextView
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="7dp" android:paddingBottom="7dp"
android:paddingTop="7dp" android:paddingTop="7dp"
android:layout_weight="1"
android:text="亮度(brightness)" android:text="亮度(brightness)"
android:textColor="@color/colorWhite" /> android:textColor="@color/colorWhite" />
<SeekBar <SeekBar
android:id="@+id/seekbar_brightness" android:id="@+id/seekbar_brightness"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_weight="2" android:layout_height="match_parent"
android:layout_height="match_parent" /> android:layout_weight="2" />
</LinearLayout> </LinearLayout>
<Switch <Switch
@ -89,7 +92,7 @@
android:layout_marginStart="13dp" android:layout_marginStart="13dp"
android:layout_marginTop="27dp" android:layout_marginTop="27dp"
android:checked="true" android:checked="true"
android:textColor="@color/colorWhite" android:text="closeVoice"
android:text="closeVoice" /> android:textColor="@color/colorWhite" />
</RelativeLayout> </RelativeLayout>

58
libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java

@ -32,6 +32,10 @@ public class UVCCameraHelper {
private static final String TAG = "UVCCameraHelper"; private static final String TAG = "UVCCameraHelper";
private int previewWidth = 640; private int previewWidth = 640;
private int previewHeight = 480; 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_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS;
public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST; public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST;
@ -42,8 +46,8 @@ public class UVCCameraHelper {
private UVCCameraHandler mCameraHandler; private UVCCameraHandler mCameraHandler;
private USBMonitor.UsbControlBlock mCtrlBlock; private USBMonitor.UsbControlBlock mCtrlBlock;
private WeakReference<Activity> mActivityWrf; private Activity mActivity;
private WeakReference<CameraViewInterface> mCamViewWrf; private CameraViewInterface mCamView;
private UVCCameraHelper() { private UVCCameraHelper() {
} }
@ -72,8 +76,8 @@ public class UVCCameraHelper {
} }
public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) { public void initUSBMonitor(Activity activity, CameraViewInterface cameraView, final OnMyDevConnectListener listener) {
this.mActivityWrf = new WeakReference<>(activity); this.mActivity = activity;
this.mCamViewWrf = new WeakReference<>(cameraView); this.mCamView = cameraView;
mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() { mUSBMonitor = new USBMonitor(activity.getApplicationContext(), new USBMonitor.OnDeviceConnectListener() {
// called by checking usb device // called by checking usb device
@ -100,7 +104,19 @@ public class UVCCameraHelper {
public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) { public void onConnect(final UsbDevice device, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) {
mCtrlBlock = ctrlBlock; mCtrlBlock = ctrlBlock;
openCamera(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) { if(listener != null) {
listener.onConnectDev(device,true); listener.onConnectDev(device,true);
} }
@ -124,7 +140,7 @@ public class UVCCameraHelper {
} }
public void createUVCCamera() { public void createUVCCamera() {
if (mCamViewWrf.get() == null) if (mCamView == null)
throw new NullPointerException("CameraViewInterface cannot be null!"); throw new NullPointerException("CameraViewInterface cannot be null!");
// release resources for initializing camera handler // release resources for initializing camera handler
@ -133,9 +149,9 @@ public class UVCCameraHelper {
mCameraHandler = null; mCameraHandler = null;
} }
// initialize camera handler // initialize camera handler
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight); mCamView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, mCameraHandler = UVCCameraHandler.createHandler(mActivity, mCamView, 2,
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); previewWidth, previewHeight, FRAME_FORMAT_MJPEG);
} }
public void updateResolution(int width, int height) { public void updateResolution(int width, int height) {
@ -148,11 +164,23 @@ public class UVCCameraHelper {
mCameraHandler.release(); mCameraHandler.release();
mCameraHandler = null; mCameraHandler = null;
} }
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight); mCamView.setAspectRatio(previewWidth / (float)previewHeight);
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2, mCameraHandler = UVCCameraHandler.createHandler(mActivity,mCamView, 2,
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV); previewWidth, previewHeight, FRAME_FORMAT_MJPEG);
openCamera(mCtrlBlock); 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() { public void registerUSB() {
@ -206,7 +234,7 @@ public class UVCCameraHelper {
public List<UsbDevice> getUsbDeviceList() { public List<UsbDevice> getUsbDeviceList() {
List<DeviceFilter> deviceFilters = DeviceFilter List<DeviceFilter> deviceFilters = DeviceFilter
.getDeviceFilters(mActivityWrf.get().getApplicationContext(), R.xml.device_filter); .getDeviceFilters(mActivity.getApplicationContext(), R.xml.device_filter);
if (mUSBMonitor == null || deviceFilters == null) if (mUSBMonitor == null || deviceFilters == null)
return null; return null;
return mUSBMonitor.getDeviceList(deviceFilters.get(0)); return mUSBMonitor.getDeviceList(deviceFilters.get(0));
@ -245,8 +273,6 @@ public class UVCCameraHelper {
} }
public void release() { public void release() {
mCamViewWrf.clear();
mActivityWrf.clear();
if (mCameraHandler != null) { if (mCameraHandler != null) {
mCameraHandler.release(); mCameraHandler.release();
mCameraHandler = null; mCameraHandler = null;

4
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_HEIGHT = 480;
public static final int DEFAULT_PREVIEW_MODE = 0; public static final int DEFAULT_PREVIEW_MODE = 0;
public static final int DEFAULT_PREVIEW_MIN_FPS = 1; 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 float DEFAULT_BANDWIDTH = 1.0f;
public static final int FRAME_FORMAT_YUYV = 0; public static final int FRAME_FORMAT_YUYV = 0;
@ -127,7 +127,7 @@ public class UVCCamera {
private UsbControlBlock mCtrlBlock; private UsbControlBlock mCtrlBlock;
protected long mControlSupports; // カメラコントロールでサポートしている機能フラグ protected long mControlSupports; // カメラコントロールでサポートしている機能フラグ
protected long mProcSupports; // プロセッシングユニットでサポートしている機能フラグ 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 int mCurrentWidth = DEFAULT_PREVIEW_WIDTH, mCurrentHeight = DEFAULT_PREVIEW_HEIGHT;
protected float mCurrentBandwidthFactor = DEFAULT_BANDWIDTH; protected float mCurrentBandwidthFactor = DEFAULT_BANDWIDTH;
protected String mSupportedSize; protected String mSupportedSize;

14
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_NV21);
mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_YUV420SP); mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_YUV420SP);
} catch (final IllegalArgumentException e) { } catch (final IllegalArgumentException e) {
// try { try {
// // fallback to YUV mode // fallback to YUV mode
// mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, UVCCamera.DEFAULT_PREVIEW_MODE, mBandwidthFactor); mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, UVCCamera.DEFAULT_PREVIEW_MODE, mBandwidthFactor);
// } catch (final IllegalArgumentException e1) { } catch (final IllegalArgumentException e1) {
// callOnError(e1); callOnError(e1);
// return; return;
// } }
} }
if (surface instanceof SurfaceHolder) { if (surface instanceof SurfaceHolder) {
mUVCCamera.setPreviewDisplay((SurfaceHolder)surface); mUVCCamera.setPreviewDisplay((SurfaceHolder)surface);

6
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 Activity parent, final CameraViewInterface cameraView,
final int width, final int height) { 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 Activity parent, final CameraViewInterface cameraView,
final int width, final int height, final float bandwidthFactor) { 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 Activity parent, final CameraViewInterface cameraView,
final int encoderType, final int width, final int height) { 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);
} }
/** /**

6
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 Activity parent, final CameraViewInterface cameraView,
final int width, final int height) { 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 Activity parent, final CameraViewInterface cameraView,
final int width, final int height, final float bandwidthFactor) { 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 Activity parent, final CameraViewInterface cameraView,
final int encoderType, final int width, final int height) { 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);
} }
/** /**

Loading…
Cancel
Save