Camera2 set preview (View) and get preview callbac

2019-03-04 09:06发布

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条回答
甜甜的少女心
2楼-- · 2019-03-04 09:27

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)

查看更多
登录 后发表回答