问题
I'm trying to display normal Full HD preview in landscape orientation (1920x1080) using Camera2
, but camera returns 1440x1080 as the highest resolution.
With Legacy Camera (android.hardware.camera
) there was no such issue for the same device. What am I doing wrong?
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
for (Size size : map.getOutputSizes(SurfaceTexture.class)) {
Log.i(TAG, "imageDimension " + size);
}
Output:
imageDimension 1440x1080
imageDimension 1280x960
imageDimension 1280x720
imageDimension 864x480
imageDimension 640x640
imageDimension 832x486
imageDimension 800x480
imageDimension 720x480
imageDimension 768x432
imageDimension 640x480
Also it seems that to correctly display preview in landscape we need a lot of headache code like:
private void configureTransform(int viewWidth, int viewHeight) {
Activity activity = getActivity();
if (null == mTextureView || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}
From official sample https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java#L740
Isn't Camera2 supposed to make things easier than legacy camera? I'm not sure...
Though one good thing I see: we can set many surface targets, with legacy camera we could not use setPreviewDisplay
and setPreviewTexture
together, but Camera2 allows many outputs
UPDATE
OMG!
Next information shocked me:
p.s. I have tested on Xiaomi device
Android Camera2 Output sizes
https://github.com/googlesamples/android-Camera2Basic/issues/113
https://github.com/googlesamples/android-Camera2Basic/issues/123
回答1:
TL;DR
If you have CameraMetadata.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY
basically your phone manufacturer does not support Camera API2.
For those who end up here as I did you can find the explanation in this related question and this github issue .
来源:https://stackoverflow.com/questions/52374888/camera2-1440x1080-is-maximum