Skip to content

Commit 6f39f9a

Browse files
jmgomezpovedaalalek
authored andcommitted
Merge pull request opencv#8168 from jmgomezpoveda:issue_8166
* Use the YV12 format in the Android emulator to avoid image issues * Removed trailing spaces * Added exception in else case * Removed tab
1 parent 9a92777 commit 6f39f9a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

modules/java/generator/src/java/android+JavaCameraView.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb
4141
protected Camera mCamera;
4242
protected JavaCameraFrame[] mCameraFrame;
4343
private SurfaceTexture mSurfaceTexture;
44+
private int mPreviewFormat = ImageFormat.NV21;
4445

4546
public static class JavaCameraSizeAccessor implements ListItemAccessor {
4647

@@ -145,7 +146,14 @@ protected boolean initializeCamera(int width, int height) {
145146
/* Select the size that fits surface considering maximum size allowed */
146147
Size frameSize = calculateCameraFrameSize(sizes, new JavaCameraSizeAccessor(), width, height);
147148

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+
149157
Log.d(TAG, "Set preview size to " + Integer.valueOf((int)frameSize.width) + "x" + Integer.valueOf((int)frameSize.height));
150158
params.setPreviewSize((int)frameSize.width, (int)frameSize.height);
151159

@@ -303,7 +311,13 @@ public Mat gray() {
303311

304312
@Override
305313
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+
307321
return mRgba;
308322
}
309323

0 commit comments

Comments
 (0)