'How to set android camera2 preview and capture size?
I am using a SurfaceView
to show the preview I capture. I want to use width=1080,height=1920 for the preview. Where can I set the size of the preview?
I googled for an answer, but they are all for camera version one. I am using the android.hardware.camera2.
private void takePreview() {
try {
final CaptureRequest.Builder previewRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
previewRequestBuilder.addTarget(mSurfaceHolder.getSurface());
mCameraDevice.createCaptureSession(Arrays.asList(mSurfaceHolder.getSurface(), mImageReader.getSurface()), new CameraCaptureSession.StateCallback() // ③
{
@Override
public void onConfigured(CameraCaptureSession cameraCaptureSession) {
if (null == mCameraDevice) return;
mCameraCaptureSession = cameraCaptureSession;
try {
previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
previewRequestBuilder.set(CaptureRequest.JPEG_THUMBNAIL_SIZE, new Size(1080,1920));
CaptureRequest previewRequest = previewRequestBuilder.build();
mCameraCaptureSession.setRepeatingRequest(previewRequest, null, childHandler);
} catch (CameraAccessException e) {
Log.e("takePreview","onConfigured(CameraCaptureSession cameraCaptureSession)",e);
}
}
@Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
Log.e("takePreview","onConfigureFailed");
}
}, childHandler);
} catch (CameraAccessException e) {
Log.e("takePreview","CameraAccessException");
}
}
Solution 1:[1]
As documented in the reference to createCaptureSession:
For drawing to a SurfaceView: Once the SurfaceView's Surface is created, set the size of the Surface with setFixedSize(int, int) to be one of the sizes returned by getOutputSizes(SurfaceHolder.class) and then obtain the Surface by calling getSurface(). If the size is not set by the application, it will be rounded to the nearest supported size less than 1080p, by the camera device.
Solution 2:[2]
Take a look at the Camera2Basic example that Google provides on GitHub: https://github.com/googlesamples/android-Camera2Basic
There is a method in the main fragment which chooses the optimal preview size for a given device. This may be a better approach if you want to make your app more flexible, rather than hardcoding the size, but even if you would still rather stick with set sizes you can see how they use the results.
The summary is that you simply set the size of the, in their case, TextureView to whatever size preview you want.
The method name is 'chooseOptimalSize' and it includes this comment/explanation:
/**
* Given {@code choices} of {@code Size}s supported by a camera, choose the smallest one that
* is at least as large as the respective texture view size, and that is at most as large as the
* respective max size, and whose aspect ratio matches with the specified value. If such size
* doesn't exist, choose the largest one that is at most as large as the respective max size,
* and whose aspect ratio matches with the specified value.
*
* @param choices The list of sizes that the camera supports for the intended output
* class
* @param textureViewWidth The width of the texture view relative to sensor coordinate
* @param textureViewHeight The height of the texture view relative to sensor coordinate
* @param maxWidth The maximum width that can be chosen
* @param maxHeight The maximum height that can be chosen
* @param aspectRatio The aspect ratio
* @return The optimal {@code Size}, or an arbitrary one if none were big enough
*/
Solution 3:[3]
to set preview size: mSurfaceTexture.setDefaultBufferSize(width, height);
in onSurfaceTextureAvailable
function.
to set capture (picture) size: ImageReader.newInstance(width, height, format, maxSize);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eddy Talvala |
Solution 2 | SaadurRehman |
Solution 3 | Talha Ç |