@@ -41,6 +41,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
41
41
protected Camera mCamera ;
42
42
protected JavaCameraFrame [] mCameraFrame ;
43
43
private SurfaceTexture mSurfaceTexture ;
44
+ private int mPreviewFormat = ImageFormat .NV21 ;
44
45
45
46
public static class JavaCameraSizeAccessor implements ListItemAccessor {
46
47
@@ -145,7 +146,14 @@ protected boolean initializeCamera(int width, int height) {
145
146
/* Select the size that fits surface considering maximum size allowed */
146
147
Size frameSize = calculateCameraFrameSize (sizes , new JavaCameraSizeAccessor (), width , height );
147
148
148
- params .setPreviewFormat (ImageFormat .NV21 );
149
+ /* Image format NV21 causes issues in the Android emulators */
150
+ if (Build .BRAND .equalsIgnoreCase ("generic" ) || Build .BRAND .equalsIgnoreCase ("Android" ))
151
+ params .setPreviewFormat (ImageFormat .YV12 ); // "generic" or "android" = android emulator
152
+ else
153
+ params .setPreviewFormat (ImageFormat .NV21 );
154
+
155
+ mPreviewFormat = params .getPreviewFormat ();
156
+
149
157
Log .d (TAG , "Set preview size to " + Integer .valueOf ((int )frameSize .width ) + "x" + Integer .valueOf ((int )frameSize .height ));
150
158
params .setPreviewSize ((int )frameSize .width , (int )frameSize .height );
151
159
@@ -303,7 +311,13 @@ public Mat gray() {
303
311
304
312
@ Override
305
313
public Mat rgba () {
306
- Imgproc .cvtColor (mYuvFrameData , mRgba , Imgproc .COLOR_YUV2RGBA_NV21 , 4 );
314
+ if (mPreviewFormat == ImageFormat .NV21 )
315
+ Imgproc .cvtColor (mYuvFrameData , mRgba , Imgproc .COLOR_YUV2RGBA_NV21 , 4 );
316
+ else if (mPreviewFormat == ImageFormat .YV12 )
317
+ Imgproc .cvtColor (mYuvFrameData , mRgba , Imgproc .COLOR_YUV2RGB_I420 , 4 ); // COLOR_YUV2RGBA_YV12 produces inverted colors
318
+ else
319
+ throw new IllegalArgumentException ("Preview Format can be NV21 or YV12" );
320
+
307
321
return mRgba ;
308
322
}
309
323
0 commit comments