Camera2 set preview (View) and get preview callbacks

北城余情 提交于 2019-12-02 17:28:17

问题


I want to get preview from Camera2 and also byte[] callback for processing frames

mImageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(),
        ImageFormat.RAW_SENSOR,1);
mImageReader.setOnImageAvailableListener(
        mOnImageAvailableListener, mBackgroundHandler);

.

SurfaceTexture texture = mTextureView.getSurfaceTexture();
assert texture != null;

// We configure the size of default buffer to be the size of camera preview we want.
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());

// This is the output Surface we need to start preview.
Surface surface = new Surface(texture);

// We set up a CaptureRequest.Builder with the output Surface.
mPreviewRequestBuilder
        = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
mPreviewRequestBuilder.addTarget(surface);
// also for preview callbacks
mPreviewRequestBuilder.addTarget(mImageReader.getSurface());

But when I add second target (ImageReader's surface) fps get very bad 10-15 instead of 30

With Legacy Camera API it was working ok

byte[] frames callback

mCamera.setPreviewCallback(this);

@Override
public void onPreviewFrame(byte[] data, Camera camera) {

}

preview

mCamera.setPreviewDisplay(mSurfaceView.getSurfaceHolder());

It didn't effect FPS quality with legacy Camera API

So how to get they same result with Camera2?


回答1:


You're requesting full-resolution RAW frames; those aren't guaranteed to be available at 30fps (because they tend to be large). You can check their max frame rate via StreamConfigurationMap queries.

If you want to match the old API behavior, you want to set the ImageReader resolution to match your preview resolution, and the format to YUV_420_888.
On higher-end devices, you can probably set the resolution to maximum with YUV_420_888, or even use RAW output, but that's going to be device specific (look for devices that support the BURST_CAPTURE capability)



来源:https://stackoverflow.com/questions/51579859/camera2-set-preview-view-and-get-preview-callbacks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!