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 bb5ba33..16587da 100644 --- a/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java +++ b/app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java @@ -139,6 +139,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog mUVCCameraView = (CameraViewInterface) mTextureView; mUVCCameraView.setCallback(this); mCameraHelper = UVCCameraHelper.getInstance(); + mCameraHelper.setDefaultFrameFormat(UVCCameraHelper.FRAME_FORMAT_YUYV); mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener); @@ -242,7 +243,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog } if (!mCameraHelper.isRecording()) { String videoPath = UVCCameraHelper.ROOT_PATH + System.currentTimeMillis(); -// FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264"); + FileUtils.createfile(FileUtils.ROOT_PATH + "test666.h264"); RecordParams params = new RecordParams(); params.setRecordPath(videoPath); params.setRecordDuration(0); // 设置为0,不分割保存 @@ -252,7 +253,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) { // type = 1,h264 video stream if (type == 1) { -// FileUtils.putFileStream(data, offset, length); + FileUtils.putFileStream(data, offset, length); } // type = 0,aac audio stream if(type == 0) { @@ -268,7 +269,7 @@ public class USBCameraActivity extends AppCompatActivity implements CameraDialog showShortMsg("start record..."); mSwitchVoice.setEnabled(false); } else { -// FileUtils.releaseFile(); + FileUtils.releaseFile(); mCameraHelper.stopRecording(); showShortMsg("stop record..."); mSwitchVoice.setEnabled(true); diff --git a/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java b/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java index 194ff46..d03d6ab 100644 --- a/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java +++ b/libusbcamera/src/main/java/com/jiangdg/usbcamera/UVCCameraHelper.java @@ -33,11 +33,12 @@ public class UVCCameraHelper { private int previewWidth = 640; private int previewHeight = 480; // 高分辨率YUV格式帧率较低 - private static int FRAME_FORMAT_YUYV = UVCCamera.FRAME_FORMAT_YUYV; + public static final 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; + public static final int FRAME_FORMAT_MJPEG = UVCCamera.FRAME_FORMAT_MJPEG; + public static final int MODE_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS; + public static final int MODE_CONTRAST = UVCCamera.PU_CONTRAST; + private int mFrameFormat = FRAME_FORMAT_MJPEG; private static UVCCameraHelper mCameraHelper; // USB Manager @@ -151,7 +152,7 @@ public class UVCCameraHelper { // initialize camera handler mCamView.setAspectRatio(previewWidth / (float)previewHeight); mCameraHandler = UVCCameraHandler.createHandler(mActivity, mCamView, 2, - previewWidth, previewHeight, FRAME_FORMAT_MJPEG); + previewWidth, previewHeight, mFrameFormat); } public void updateResolution(int width, int height) { @@ -166,7 +167,7 @@ public class UVCCameraHelper { } mCamView.setAspectRatio(previewWidth / (float)previewHeight); mCameraHandler = UVCCameraHandler.createHandler(mActivity,mCamView, 2, - previewWidth, previewHeight, FRAME_FORMAT_MJPEG); + previewWidth, previewHeight, mFrameFormat); openCamera(mCtrlBlock); new Thread(new Runnable() { @Override @@ -332,6 +333,13 @@ public class UVCCameraHelper { this.previewHeight = defaultHeight; } + public void setDefaultFrameFormat(int format) { + if(mUSBMonitor != null) { + throw new IllegalStateException("setDefaultFrameFormat should be call before initMonitor"); + } + this.mFrameFormat = format; + } + public int getPreviewWidth() { return previewWidth; } 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 b05a9ac..fd7f7f9 100644 --- a/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java +++ b/libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java @@ -531,6 +531,7 @@ public abstract class AbstractUVCCameraHandler extends Handler { try { mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, mPreviewMode, mBandwidthFactor); // 获取USB Camera预览数据,使用NV21颜色会失真 + // 无论使用YUV还是MPEG,setFrameCallback的设置效果一致 // mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_NV21); mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_YUV420SP); } catch (final IllegalArgumentException e) {