android Camera 如何判断当前使用的摄像头是前置还是后置
2014-04-16 16:33
615 查看
现在 android 平台的智能手机一般都标配有两颗摄像头。在 Camera 中都存在摄像头切换的功能。
并且有一些功能前后置摄像头上会有所不同。譬如人脸检测,人脸识别,自动对焦,闪光灯等功能,
如果前置摄像头的像素太低,不支持该功能的话,就需要在前置摄像头上关掉该 feature.
那么是如何判断并切换前后置摄像头的呢?
我们先来看下 CameraInfo 这个类,
[java] view
plaincopy
/**
* Information about a camera
*/
public static class CameraInfo {
/**
* The facing of the camera is opposite to that of the screen.
*/
public static final int CAMERA_FACING_BACK = 0;
/**
* The facing of the camera is the same as that of the screen.
*/
public static final int CAMERA_FACING_FRONT = 1;
/**
* The direction that the camera faces. It should be
* CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
*/
public int facing;
/**
* <p>The orientation of the camera image. The value is the angle that the
* camera image needs to be rotated clockwise so it shows correctly on
* the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
*
* <p>For example, suppose a device has a naturally tall screen. The
* back-facing camera sensor is mounted in landscape. You are looking at
* the screen. If the top side of the camera sensor is aligned with the
* right edge of the screen in natural orientation, the value should be
* 90. If the top side of a front-facing camera sensor is aligned with
* the right of the screen, the value should be 270.</p>
*
* @see #setDisplayOrientation(int)
* @see Parameters#setRotation(int)
* @see Parameters#setPreviewSize(int, int)
* @see Parameters#setPictureSize(int, int)
* @see Parameters#setJpegThumbnailSize(int, int)
*/
public int orientation;
};
见名知义,它就是一个 Camera 信息类。它是通过与屏幕的方向是否一致来定义前后置摄像头的。
与屏幕方向相反即为 BACK_FACING_CAMERA
与屏幕方向一致即为 FRONT_FACING_CAMERA
那么在代码中我们是如何获取当前使用的 CamerInfo 呢
[java] view
plaincopy
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
当然,使用该代码的前提是要 import android.hardware.Camera.CameraInfo;
判断使用是前置还是后置摄像头,可以通过if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 来判断。
当Camera 的实例已经创建了的情况下,则需要通过如下方式来判断。
[java] view
plaincopy
CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
//stopFaceDetection();
}
也可以通过 if(mCameraId == CameraInfo.CAMERA_FACING_FRONT) 来判断。
其中 mCameraId 是当前使用的 CameraId, 一般前置为1, 后置为 0。
并且有一些功能前后置摄像头上会有所不同。譬如人脸检测,人脸识别,自动对焦,闪光灯等功能,
如果前置摄像头的像素太低,不支持该功能的话,就需要在前置摄像头上关掉该 feature.
那么是如何判断并切换前后置摄像头的呢?
我们先来看下 CameraInfo 这个类,
[java] view
plaincopy
/**
* Information about a camera
*/
public static class CameraInfo {
/**
* The facing of the camera is opposite to that of the screen.
*/
public static final int CAMERA_FACING_BACK = 0;
/**
* The facing of the camera is the same as that of the screen.
*/
public static final int CAMERA_FACING_FRONT = 1;
/**
* The direction that the camera faces. It should be
* CAMERA_FACING_BACK or CAMERA_FACING_FRONT.
*/
public int facing;
/**
* <p>The orientation of the camera image. The value is the angle that the
* camera image needs to be rotated clockwise so it shows correctly on
* the display in its natural orientation. It should be 0, 90, 180, or 270.</p>
*
* <p>For example, suppose a device has a naturally tall screen. The
* back-facing camera sensor is mounted in landscape. You are looking at
* the screen. If the top side of the camera sensor is aligned with the
* right edge of the screen in natural orientation, the value should be
* 90. If the top side of a front-facing camera sensor is aligned with
* the right of the screen, the value should be 270.</p>
*
* @see #setDisplayOrientation(int)
* @see Parameters#setRotation(int)
* @see Parameters#setPreviewSize(int, int)
* @see Parameters#setPictureSize(int, int)
* @see Parameters#setJpegThumbnailSize(int, int)
*/
public int orientation;
};
见名知义,它就是一个 Camera 信息类。它是通过与屏幕的方向是否一致来定义前后置摄像头的。
与屏幕方向相反即为 BACK_FACING_CAMERA
与屏幕方向一致即为 FRONT_FACING_CAMERA
那么在代码中我们是如何获取当前使用的 CamerInfo 呢
[java] view
plaincopy
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
当然,使用该代码的前提是要 import android.hardware.Camera.CameraInfo;
判断使用是前置还是后置摄像头,可以通过if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 来判断。
当Camera 的实例已经创建了的情况下,则需要通过如下方式来判断。
[java] view
plaincopy
CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
//stopFaceDetection();
}
也可以通过 if(mCameraId == CameraInfo.CAMERA_FACING_FRONT) 来判断。
其中 mCameraId 是当前使用的 CameraId, 一般前置为1, 后置为 0。
相关文章推荐
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- android Camera 如何判断当前使用的摄像头是前置还是后置
- 如何判断当前使用的是何种浏览器?
- 如何使用PHP或者js判断当前是微信浏览器访问
- 如何判断当前移动设备是IOS还是安卓
- 如何判断当前设备是pad还是phone
- 在android 5.0以上,如何判断当前应用是在前台还是后台
- 电脑大小端问题以及如何判断当前机器是大端还是小端
- java中如何判断是否使用继承还是合成
- 如何判断当前主机是物理机还是虚拟机?
- 什么时候应该使用==?什么时候应该使用Equals?(如何判断值相等还是引用相等。)
- Android ViewPager 如何判断当前页面是从前一页还是后一页滑动过来
- VC++如何判断当前操作系统是32位还是64位
- 使用UAParser在C#MVC项目中如何判断用户是在用什么设备进行访问(手机,平板还是普通的电脑)
- VC++如何判断当前操作系统是32位还是64位?