From 6e8f5ae574a0ea27f5218685eb69a1ec4fc175d3 Mon Sep 17 00:00:00 2001 From: Alexander Smorkalov Date: Wed, 10 Apr 2013 16:57:34 +0400 Subject: [PATCH] Front/back camera constants semantic for Android fixed (Bug #2839). Front/Back camera constants' values changed; Additional camera facing check added for JavaCameraView. --- modules/java/android_lib/res/values/attrs.xml | 4 +-- .../src/java/android+CameraBridgeViewBase.java | 6 +++- .../generator/src/java/android+JavaCameraView.java | 41 ++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/modules/java/android_lib/res/values/attrs.xml b/modules/java/android_lib/res/values/attrs.xml index 0cdf109..6902621 100644 --- a/modules/java/android_lib/res/values/attrs.xml +++ b/modules/java/android_lib/res/values/attrs.xml @@ -4,8 +4,8 @@ - - + + diff --git a/modules/java/generator/src/java/android+CameraBridgeViewBase.java b/modules/java/generator/src/java/android+CameraBridgeViewBase.java index 36417c5..42a199a 100644 --- a/modules/java/generator/src/java/android+CameraBridgeViewBase.java +++ b/modules/java/generator/src/java/android+CameraBridgeViewBase.java @@ -47,10 +47,14 @@ public abstract class CameraBridgeViewBase extends SurfaceView implements Surfac protected int mMaxWidth; protected float mScale = 0; protected int mPreviewFormat = Highgui.CV_CAP_ANDROID_COLOR_FRAME_RGBA; - protected int mCameraIndex = -1; + protected int mCameraIndex = CAMERA_ID_ANY; protected boolean mEnabled; protected FpsMeter mFpsMeter = null; + public static final int CAMERA_ID_ANY = -1; + public static final int CAMERA_ID_BACK = 99; + public static final int CAMERA_ID_FRONT = 98; + public CameraBridgeViewBase(Context context, int cameraId) { super(context); mCameraIndex = cameraId; diff --git a/modules/java/generator/src/java/android+JavaCameraView.java b/modules/java/generator/src/java/android+JavaCameraView.java index f07b7d2..c7f3558 100644 --- a/modules/java/generator/src/java/android+JavaCameraView.java +++ b/modules/java/generator/src/java/android+JavaCameraView.java @@ -6,6 +6,7 @@ import android.content.Context; import android.graphics.ImageFormat; import android.graphics.SurfaceTexture; import android.hardware.Camera; +import android.hardware.Camera.CameraInfo; import android.hardware.Camera.PreviewCallback; import android.os.Build; import android.util.AttributeSet; @@ -68,7 +69,7 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb synchronized (this) { mCamera = null; - if (mCameraIndex == -1) { + if (mCameraIndex == CAMERA_ID_ANY) { Log.d(TAG, "Trying to open camera with old open()"); try { mCamera = Camera.open(); @@ -92,11 +93,39 @@ public class JavaCameraView extends CameraBridgeViewBase implements PreviewCallb } } else { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { - Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(mCameraIndex) + ")"); - try { - mCamera = Camera.open(mCameraIndex); - } catch (RuntimeException e) { - Log.e(TAG, "Camera #" + mCameraIndex + "failed to open: " + e.getLocalizedMessage()); + int localCameraIndex = mCameraIndex; + if (mCameraIndex == CAMERA_ID_BACK) { + Log.i(TAG, "Trying to open back camera"); + Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); + for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { + Camera.getCameraInfo( camIdx, cameraInfo ); + if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) { + localCameraIndex = camIdx; + break; + } + } + } else if (mCameraIndex == CAMERA_ID_FRONT) { + Log.i(TAG, "Trying to open front camera"); + Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); + for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { + Camera.getCameraInfo( camIdx, cameraInfo ); + if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + localCameraIndex = camIdx; + break; + } + } + } + if (localCameraIndex == CAMERA_ID_BACK) { + Log.e(TAG, "Back camera not found!"); + } else if (localCameraIndex == CAMERA_ID_FRONT) { + Log.e(TAG, "Front camera not found!"); + } else { + Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(localCameraIndex) + ")"); + try { + mCamera = Camera.open(localCameraIndex); + } catch (RuntimeException e) { + Log.e(TAG, "Camera #" + localCameraIndex + "failed to open: " + e.getLocalizedMessage()); + } } } } -- 2.7.4