打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
android Camera 如何判断当前使用的摄像头是前置还是后置

标签:

现在 android 平台的智能手机一般都标配有两颗摄像头。在 Camera 中都存在摄像头切换的功能。

并且有一些功能前后置摄像头上会有所不同。譬如人脸检测,人脸识别,自动对焦,闪光灯等功能,

如果前置摄像头的像素太低,不支持该功能的话,就需要在前置摄像头上关掉该 feature.

 

那么是如何判断并切换前后置摄像头的呢?

我们先来看下 CameraInfo 这个类,

 1 /**  2  * Information about a camera  3  */   4 public static class CameraInfo {   5     /**  6      * The facing of the camera is opposite to that of the screen.  7      */   8     public static final int CAMERA_FACING_BACK = 0;   9   10     /** 11      * The facing of the camera is the same as that of the screen. 12      */  13     public static final int CAMERA_FACING_FRONT = 1;  14   15     /** 16      * The direction that the camera faces. It should be 17      * CAMERA_FACING_BACK or CAMERA_FACING_FRONT. 18      */  19     public int facing;  20   21     /** 22      * <p>The orientation of the camera image. The value is the angle that the 23      * camera image needs to be rotated clockwise so it shows correctly on 24      * the display in its natural orientation. It should be 0, 90, 180, or 270.</p> 25      * 26      * <p>For example, suppose a device has a naturally tall screen. The 27      * back-facing camera sensor is mounted in landscape. You are looking at 28      * the screen. If the top side of the camera sensor is aligned with the 29      * right edge of the screen in natural orientation, the value should be 30      * 90. If the top side of a front-facing camera sensor is aligned with 31      * the right of the screen, the value should be 270.</p> 32      * 33      * @see #setDisplayOrientation(int) 34      * @see Parameters#setRotation(int) 35      * @see Parameters#setPreviewSize(int, int) 36      * @see Parameters#setPictureSize(int, int) 37      * @see Parameters#setJpegThumbnailSize(int, int) 38      */  39     public int orientation;  40 };  

见名知义,它就是一个 Camera 信息类。它是通过与屏幕的方向是否一致来定义前后置摄像头的。

 

与屏幕方向相反即为 BACK_FACING_CAMERA

与屏幕方向一致即为 FRONT_FACING_CAMERA

那么在代码中我们是如何获取当前使用的 CamerInfo 呢

1 Camera.CameraInfo info = new Camera.CameraInfo();  2 Camera.getCameraInfo(cameraId, info); 

当然,使用该代码的前提是要 import android.hardware.Camera.CameraInfo;

 

判断使用是前置还是后置摄像头,可以通过if (info.facing == CameraInfo.CAMERA_FACING_FRONT) 来判断。

当Camera 的实例已经创建了的情况下,则需要通过如下方式来判断。

1 CameraInfo info = CameraHolder.instance().getCameraInfo()[mCameraId];  2 if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {  3     //stopFaceDetection();  4 }  

也可以通过 if(mCameraId == CameraInfo.CAMERA_FACING_FRONT) 来判断。

其中 mCameraId 是当前使用的 CameraId, 一般前置为1, 后置为 0。

android Camera 如何判断当前使用的摄像头是前置还是后置

标签:

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
【Android音视频开发】【012】Android中Camera的采集方向和预览方向
Android相机开发详解
自己动手写一个Camera
Android2.2平台上支持多camera(一)
苹果2019秋季发布会:传说中的“浴霸三摄”亮相
ANDROID JNI之JAVA域与c域的互操作
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服