Skip to content

Commit 824d277

Browse files
committed
修复拍照颜色失真问题
1 parent e07b264 commit 824d277

File tree

8 files changed

+173
-71
lines changed

8 files changed

+173
-71
lines changed

app/src/main/java/com/jiangdg/usbcamera/view/USBCameraActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,10 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
139139
mUVCCameraView = (CameraViewInterface) mTextureView;
140140
mUVCCameraView.setCallback(this);
141141
mCameraHelper = UVCCameraHelper.getInstance();
142+
mCameraHelper.setDefaultPreviewSize(320,240);
142143
mCameraHelper.initUSBMonitor(this, mUVCCameraView, listener);
143144

145+
144146
mCameraHelper.setOnPreviewFrameListener(new AbstractUVCCameraHandler.OnPreViewResultListener() {
145147
@Override
146148
public void onPreviewResult(byte[] nv21Yuv) {
@@ -232,6 +234,7 @@ public void onCaptureResult(String path) {
232234
Log.i(TAG,"save path:" + path);
233235
}
234236
});
237+
235238
break;
236239
case R.id.menu_recording:
237240
if (mCameraHelper == null || !mCameraHelper.isCameraOpened()) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
android:id="@+id/camera_view"
2222
android:layout_below="@id/toolbar"
2323
android:layout_width="match_parent"
24-
android:layout_height="match_parent"
24+
android:layout_height="wrap_content"
25+
android:layout_centerInParent="true"
2526
android:layout_centerHorizontal="true"
2627
android:layout_centerVertical="true" />
2728

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public class UVCCameraHelper {
3434
private int previewHeight = 480;
3535
public static int MODE_BRIGHTNESS = UVCCamera.PU_BRIGHTNESS;
3636
public static int MODE_CONTRAST = UVCCamera.PU_CONTRAST;
37-
//0-YUYV
38-
private static final int PREVIEW_FORMAT = 0;
3937

4038
private static UVCCameraHelper mCameraHelper;
4139
// USB Manager
@@ -135,9 +133,9 @@ public void createUVCCamera() {
135133
mCameraHandler = null;
136134
}
137135
// initialize camera handler
138-
// cameraView.setAspectRatio(previewWidth / (float)previewHeight);
136+
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
139137
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2,
140-
previewWidth, previewHeight, PREVIEW_FORMAT);
138+
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV);
141139
}
142140

143141
public void updateResolution(int width, int height) {
@@ -150,9 +148,9 @@ public void updateResolution(int width, int height) {
150148
mCameraHandler.release();
151149
mCameraHandler = null;
152150
}
153-
// cameraView.setAspectRatio(previewWidth / (float)previewHeight);
151+
// mCamViewWrf.get().setAspectRatio(previewWidth / (float)previewHeight);
154152
mCameraHandler = UVCCameraHandler.createHandler(mActivityWrf.get(), mCamViewWrf.get(), 2,
155-
previewWidth, previewHeight, PREVIEW_FORMAT);
153+
previewWidth, previewHeight, UVCCamera.FRAME_FORMAT_YUYV);
156154
openCamera(mCtrlBlock);
157155
startPreview(mCamViewWrf.get());
158156
}
@@ -299,4 +297,20 @@ public List<Size> getSupportedPreviewSizes() {
299297
return null;
300298
return mCameraHandler.getSupportedPreviewSizes();
301299
}
300+
301+
public void setDefaultPreviewSize(int defaultWidth,int defaultHeight) {
302+
if(mUSBMonitor != null) {
303+
throw new IllegalStateException("setDefaultPreviewSize should be call before initMonitor");
304+
}
305+
this.previewWidth = defaultWidth;
306+
this.previewHeight = defaultHeight;
307+
}
308+
309+
public int getPreviewWidth() {
310+
return previewWidth;
311+
}
312+
313+
public int getPreviewHeight() {
314+
return previewHeight;
315+
}
302316
}

libusbcamera/src/main/java/com/serenegiant/usb/UVCCamera.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ public class UVCCamera {
5858
public static final int PIXEL_FORMAT_YUV = 1;
5959
public static final int PIXEL_FORMAT_RGB565 = 2;
6060
public static final int PIXEL_FORMAT_RGBX = 3;
61-
public static final int PIXEL_FORMAT_YUV420SP = 4;
62-
public static final int PIXEL_FORMAT_NV21 = 5; // = YVU420SemiPlanar
61+
public static final int PIXEL_FORMAT_YUV420SP = 4; // NV12
62+
public static final int PIXEL_FORMAT_NV21 = 5; // = YVU420SemiPlanar,NV21,但是保存到jpg颜色失真
6363

6464
//--------------------------------------------------------------------------------
6565
public static final int CTRL_SCANNING = 0x00000001; // D0: Scanning Mode
@@ -127,7 +127,7 @@ public class UVCCamera {
127127
private UsbControlBlock mCtrlBlock;
128128
protected long mControlSupports; // カメラコントロールでサポートしている機能フラグ
129129
protected long mProcSupports; // プロセッシングユニットでサポートしている機能フラグ
130-
protected int mCurrentFrameFormat = FRAME_FORMAT_MJPEG;
130+
protected int mCurrentFrameFormat = FRAME_FORMAT_YUYV;
131131
protected int mCurrentWidth = DEFAULT_PREVIEW_WIDTH, mCurrentHeight = DEFAULT_PREVIEW_HEIGHT;
132132
protected float mCurrentBandwidthFactor = DEFAULT_BANDWIDTH;
133133
protected String mSupportedSize;

libusbcamera/src/main/java/com/serenegiant/usb/common/AbstractUVCCameraHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,9 @@ public void handleStartPreview(final Object surface) {
530530
if ((mUVCCamera == null) || mIsPreviewing) return;
531531
try {
532532
mUVCCamera.setPreviewSize(mWidth, mHeight, 1, 31, mPreviewMode, mBandwidthFactor);
533-
// 获取USB Camera预览数据
534-
mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_NV21);
535-
533+
// 获取USB Camera预览数据,使用NV21颜色会失真
534+
// mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_NV21);
535+
mUVCCamera.setFrameCallback(mIFrameCallback, UVCCamera.PIXEL_FORMAT_YUV420SP);
536536
} catch (final IllegalArgumentException e) {
537537
// try {
538538
// // fallback to YUV mode

libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static final UVCCameraHandler createHandler(
4242
final Activity parent, final CameraViewInterface cameraView,
4343
final int width, final int height) {
4444

45-
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH);
45+
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH);
4646
}
4747

4848
/**
@@ -58,7 +58,7 @@ public static final UVCCameraHandler createHandler(
5858
final Activity parent, final CameraViewInterface cameraView,
5959
final int width, final int height, final float bandwidthFactor) {
6060

61-
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, bandwidthFactor);
61+
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, bandwidthFactor);
6262
}
6363

6464
/**
@@ -74,7 +74,7 @@ public static final UVCCameraHandler createHandler(
7474
final Activity parent, final CameraViewInterface cameraView,
7575
final int encoderType, final int width, final int height) {
7676

77-
return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH);
77+
return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH);
7878
}
7979

8080
/**

libusbcamera/src/main/java/com/serenegiant/usb/common/UVCCameraHandlerMultiSurface.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static final UVCCameraHandlerMultiSurface createHandler(
4343
final Activity parent, final CameraViewInterface cameraView,
4444
final int width, final int height) {
4545

46-
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH);
46+
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH);
4747
}
4848

4949
/**
@@ -59,7 +59,7 @@ public static final UVCCameraHandlerMultiSurface createHandler(
5959
final Activity parent, final CameraViewInterface cameraView,
6060
final int width, final int height, final float bandwidthFactor) {
6161

62-
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_MJPEG, bandwidthFactor);
62+
return createHandler(parent, cameraView, 1, width, height, UVCCamera.FRAME_FORMAT_YUYV, bandwidthFactor);
6363
}
6464

6565
/**
@@ -75,7 +75,7 @@ public static final UVCCameraHandlerMultiSurface createHandler(
7575
final Activity parent, final CameraViewInterface cameraView,
7676
final int encoderType, final int width, final int height) {
7777

78-
return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_MJPEG, UVCCamera.DEFAULT_BANDWIDTH);
78+
return createHandler(parent, cameraView, encoderType, width, height, UVCCamera.FRAME_FORMAT_YUYV, UVCCamera.DEFAULT_BANDWIDTH);
7979
}
8080

8181
/**

0 commit comments

Comments
 (0)