Android相机是如何获取到图像的

在研究zxing的过程中,脑袋中一直有个疑惑,那个相机并没有拍照,它是怎么获取图像的

带着这个疑惑查看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);
    };

Called as preview frames are displayed. This callback is invoked on the event thread open(int) was called from.

其中这个  “open(int) ”很关键

android.hardware.Camera.PreviewCallback

Callback interface used to deliver copies of preview frames as they are displayed.

See Also:
setPreviewCallback(Camera.PreviewCallback)
setOneShotPreviewCallback(Camera.PreviewCallback)
setPreviewCallbackWithBuffer(Camera.PreviewCallback)
startPreview()

选这其中一个打开:

/**
    * <p>Installs a callback to be invoked for the next preview frame in
    * addition to displaying it on the screen.  After one invocation, the
    * callback is cleared. This method can be called any time, even when
    * preview is live.  Any other preview callbacks are overridden.</p>
    *
    * <p>If you are using the preview data to create video or still images,
    * strongly consider using {@link android.media.MediaActionSound} to
    * properly indicate image capture or recording start/stop to the user.</p>
    *
    * @param cb a callback object that receives a copy of the next preview frame,
    *    or null to stop receiving callbacks.
    * @see android.media.MediaActionSound
    */
    public final void setOneShotPreviewCallback(PreviewCallback cb) {
        mPreviewCallback = cb;
        mOneShot = true;
        mWithBuffer = false;
        if (cb != null) {
            mUsingPreviewAllocation = false;
        }
        setHasPreviewCallback(cb != null, false);
    }

单触发模式,只要预览ok,就会启动回调函数
onPreviewFrame(byte[] data, Camera camera)

把当前的视窗类的Frame的信息装到byte[] data ,这样程序就得到预览窗口的图像数据。

其他的两个是连续触发模式,接近视频流的模式,每一帧都会返给数组,摄像机程序的实现底层肯定调用这个函数。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/93718a825559727621ed3f2da032f3b4.html