Android Camera之常用接口(人脸追踪源码)简介

2019-10-07  本文已影响0人  晓涵说
在这里插入图片描述 Github:源码路径

1.CameraInfo

用于描述camera的内部类,主要包括以下参数:

    /**
         * 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;//和屏幕一个方向,前置相机

一般我们在使用相机初始化时,需要确定是开启前置还是后置相机。如果没有增加其他外置相机的条件下,后置和前置相机的ID分别为0和1,如果包含其他的外置相机,则相应的为2或其他值。为便于编程理解,一般直接使用以上两个值作为开启相机的ID。

mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);//开启后置相机
 /**
         * <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>

上一部分的翻译在上面的定义中已经说明,下面一段是针对该定义举例,翻译如下:

举例,假设设备处于自然竖直状态,后置相机的传感器是水平方向,你面对手机,如果传感器的顶部在手机屏幕的右边,那么这个值为90;如果前置摄像头的传感器顶部在手机右边,那么该值就是270。
Set the clockwise rotation of preview display in degrees. This affects
     * the preview frames and the picture displayed after snapshot. This method
     * is useful for portrait mode applications. Note that preview display of
     * front-facing cameras is flipped horizontally before the rotation, that
     * is, the image is reflected along the central vertical axis of the camera
     * sensor. So the users can see themselves as looking into a mirror.
    
    <p>This does not affect the order of byte array passed in {@link
     * PreviewCallback#onPreviewFrame}, JPEG pictures, or recorded videos. This
     * method is not allowed to be called during preview.

在相机预览时,通过设置该参数的顺时针的角度,来影响预览和拍照的图片效果,尤其在竖直使用相机时。但需要注意前置相机使用时,显示的效果会水平翻转,预览的效果如图照镜子。
注意,该参数的设置仅仅是影响预览的界面效果,不影响真实的相机采集的数据,因此在PreviewCallback和onPreviewFrame中获取的数据转为图像后,仍然呈现图像传感器方向的图片效果。在预览时,不可调用该参数。
提到上面的方向问题,需要结合安卓的坐标特点分析:

2 .Parameters

该参数是用于对相机的参数的设置,使相机捕获的效果符合我们的要求,但是源码中的注解我们可以看出,对于该参的设置我们不能随意设置,因为不同的相机具体的性能,例如图片大小和闪光模式等。因此在设置该参数前,需要先查询一下相机支持参数,例如在设置setColorEffect(String)参数前,先调用getSupportedColorEffects(),查看支持参数,如果不支持该参数的设置,则反回为空。

默认情况下对于尺寸的获取和设置,其单位均是像素。一般在调用尺寸或格式的getXX时,默认会返回包含至少一组的有效元素集合。

Different devices may have different camera capabilities, such as
     * picture size or flash modes. The application should query the camera
     * capabilities before setting parameters. For example, the application
     * should call {@link Camera.Parameters#getSupportedColorEffects()} before
     * calling {@link Camera.Parameters#setColorEffect(String)}. If the
     * camera does not support color effects,
     * {@link Camera.Parameters#getSupportedColorEffects()} will return null.

一般在调用setXX前建议先调用对应的getXX方法,否则会显示如下异常:

java.lang.RuntimeException: setParameters failed
        at android.hardware.Camera.native_setParameters(Native Method)

小结:

在调用相机前需要根据需要对相机的参数进行设置,但是在设置时为避免设置的参数,该相机不支持,因此在设置前,建议调用对应的get方法,查看相机支持的对应参数。

3. Face

其实安卓系统从SDK 1.0开始就支持简单的人脸识别接口,在调用相机前,添加检测的监听(setFaceDetectionListener)在回调的onFaceDetection(Camera.Face[] faces, Camera camera)就可以获取Face信息,包括以下。

注意:并不是所有的相机都支持左眼、右眼和嘴的坐标参数回调,如果不支持则返回为null,并且该参数的坐标系与rect相同,因此需要我们手动转换为相机参数坐标系。

4. Camera类中的方法

onPreviewFrame(byte[] data, Camera camera)方法,获取每一帧的相机预览数据。

小结:

以上方法中介绍了相机使用的关键方法,分类总结如下:

1.开启/关闭 :通过open()开启后置相机,或通过open(int cameraId)开启指定相机,在结束相机使用时,需要调用release()释放相机资源。

2.预览的设置:需要根据需要设置预览的方向,设置预览的载体,在开始预览前需要先设置载体,否则预览并未真正开始,结束预览后需要先将回调置为null然后调用release方法。

3.人脸检测:在添加FaceDetectionListener前,需要先调用startFaceDetection()方法,在检测结束时,调用stopFaceDetection()。

5.录像

相机录像的操作,主要通过MediaRecorder实现,通过对该类中参数的设置,实现相机录像的效果,下面主要讲解常用的接口,具体的实现在下面系列文章中会讲解,并附有github的Demo路径。

小结:
本部分主要讲解了关于视频录制(MediaRecorder)相关的接口,在录制视频前需要必须设置相机、音频格式、视频格式、帧率、分辨率和输出路径等,在调用prepare()前必须要设置好该参数。

6.总结

本文介绍了相机相关的接口参数,包括以下内容:

1.CameraInfo相关的角度、角度设置以及安卓的坐标;

2.Parameters相关的预览尺寸、格式、对焦模式和灯光等;

3.Face:相关的矩形框、人脸中眼睛和嘴的坐标等;

4.Camera类中相关的方法,包括相机参数、回调和人脸检测等;

5.录像中MediaRecorder类的主要参数的讲解等。

致谢:Android: Camera相机开发详解(上) —— 知识储备

上一篇下一篇

猜你喜欢

热点阅读