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?
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