Android Camera2 getPreviewFrame [英] Android Camera2 getPreviewFrame

查看:108
本文介绍了Android Camera2 getPreviewFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使相机框处于预览模式.我正在从github https://github.com/googlesamples/android-Camera2Basic

I am trying to get the camera frame in preview mode. I am running the sample project from github https://github.com/googlesamples/android-Camera2Basic

我遇到的问题是使框架处于预览模式.

The issue I am having is getting the frame in preview mode.

这是代码:

private CameraCaptureSession.CaptureCallback mCaptureCallback = new CameraCaptureSession.CaptureCallback() {

    private void process(CaptureResult result) {
        switch (mState) {
            case STATE_PREVIEW: {

                //HERE, HOW CAN I RETRIEVE THE CURRENT FRAME?

                break;
            }
            case STATE_WAITING_LOCK: {
               ...
                break;
            }
            case STATE_WAITING_PRECAPTURE: {
             ...
                break;
            }
            case STATE_WAITING_NON_PRECAPTURE: {
             ...
                break;
            }
        }
    }

我试图获取框架的另一件事是设置mImageReader.setOnImageAvailableListener. 我期望能够获得onImageAvailable回调的帧,但是从不调用onImageAvailable. onPreviewFrame是我自己的方法,我需要将当前帧传递给它.

Another thing I tried to get the frame is setting the mImageReader.setOnImageAvailableListener. I was expecting to be able to get the frame onImageAvailable callback, but onImageAvailable is never called. onPreviewFrame is my own method, I need to pass it the current frame.

  mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG, /*maxImages*/2);
  mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);

  private final ImageReader.OnImageAvailableListener mOnImageAvailableListener  = new ImageReader.OnImageAvailableListener() {

    @Override
    public void onImageAvailable(ImageReader reader) {
        mTextureView.onPreviewFrame(reader.acquireNextImage().getPlanes([0].getBuffer().array());
    }

};

我做错了什么? 谢谢.

What I am doing wrong? Thanks.

推荐答案

当预览帧可用时,永远不会调用OnImageAvailableListener.onImageAvailable回调,因为发送到CameraCaptureSession.setRepeatingRequest()方法的CaptureRequest没有列出ImageReaderSurface作为输出目标.

The OnImageAvailableListener.onImageAvailable callback is never called when a preview frame is available because the CaptureRequest which was sent to the CameraCaptureSession.setRepeatingRequest() method did not list the ImageReader's Surface as an output target.

您确定在将请求发送到相机时,每个捕获数据要到达的输出Surface(本质上是原始字节缓冲区).因此,要获取预览框架"以触发onImageAvailable()回调,然后将其发送到您的onPreviewFrame()方法,只需添加以下行:

You identify what output Surfaces (raw byte buffers, essentially) you want the data of each capture to go to when you send the request to the camera. So to get the "preview frames" to trigger the onImageAvailable() callback and then be sent to your onPreviewFrame() method, simply add the line:

mPreviewRequestBuilder.addTarget(mImageReader.getSurface());

该行可以在另一个类似的行之后,例如 g .该行将SurfaceTextureSurface添加到同一请求构建器.

This line can go, e.g., after the other similar line that adds the SurfaceTexture's Surface to the same request builder.

请注意,这会将每个预览帧以及捕获"按钮的输出帧"发送到您的函数.您可能希望区分onImageAvailable()回调中的某些代码.

Note that this will send every preview frame to your function, as well as the "output frames" from the capture button. You may want some code in the onImageAvailable() callback to discriminate.

这篇关于Android Camera2 getPreviewFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆