Android Camera细节

308 阅读2分钟

该文章主要讲解一下Camera有关图像角度的问题!

首先附上两张图片,这两张图片是将Camera的实时数据保存为图片,获取实时数据的接口如下

public interface PreviewCallback
{
    /**
     * Called as preview frames are displayed.  This callback is invoked
     * on the event thread {@link #open(int)} was called from.
     *
     * <p>If using the {@link android.graphics.ImageFormat#YV12} format,
     * refer to the equations in {@link Camera.Parameters#setPreviewFormat}
     * for the arrangement of the pixel data in the preview callback
     * buffers.
     *
     * @param data the contents of the preview frame in the format defined
     *  by {@link android.graphics.ImageFormat}, which can be queried
     *  with {@link android.hardware.Camera.Parameters#getPreviewFormat()}.
     *  If {@link android.hardware.Camera.Parameters#setPreviewFormat(int)}
     *             is never called, the default will be the YCbCr_420_SP
     *             (NV21) format.
     * @param camera the Camera service object.
     */
    void onPreviewFrame(byte[] data, Camera camera);
};

将前置摄像头获取的NV21数据转换成图片 a.jpg,举起的手是左手,左手,左手

8eb4df8650e9816cb366131a331be9a.png

将后置摄像头获取的NV21数据转换成图片b.jpg,举起的手是左手,左手,左手

a4a2fc11e1529a027a423780ed037a5.png

分析这两张图片----->

疑问1:在屏幕上看到的视频,和保存在本地的NV21数据为什么不一样

1、NV21数据是Camera硬件直接采集的数据

2、Camera图像预览前有一个方法很重要,这个方法的作用是将原始数据顺时针旋转一定角度后显示在屏幕上。一般设置是90度,如果90不对那就需要针对该设备重新计算角度。

分析前置摄像头的NV21数据将其转换为图片后得到a.jpg,当前置摄像头调用setDisplayOrientation(90)时,NV21数据会进行左右镜像然后顺时针旋转90度,最终显示在屏幕上。虽然屏幕上的到了与现实镜像的数据,但是NV21数据不受影响,不会被旋转或镜像。我推测前置摄像头的Camera在setDisplayOrientation(90)时,不但旋转了,而且也镜像了,并且是先左右镜像,然后再顺时针旋转90度。

分析后置摄像头的NV21数据将其转换为图片后的到b.jpg,当后置摄像头调用setDisplayOrientation(90)时,NV21数据直接顺时针旋转90度,显示在屏幕上

/**
 * 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.
 **/
mCamera.setDisplayOrientation(mCameraClockwiseRotationValue);

因为在屏幕上预览的视频图像是NV21经过旋转或镜像后得到的图像,

疑问2:前置摄像头和后置摄像头的NV21数据为什么不一样

1、主要是因为前置摄像头需要镜像吧