我正在尝试在 android 中使用camera2 api进行录制,并且我的预览大小会延伸。
可以帮助我为什么预览会延伸
这是我的代码:
private void setUpCamera(int width, int height) {
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (String camId : cameraManager.getCameraIdList()) {
cameraCharacteristics = cameraManager.getCameraCharacteristics(camId);
if (cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
//handling Rotation of the screen
int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
totalRotation = deviceRotation(cameraCharacteristics, deviceOrientation);
boolean swapRotation = totalRotation == 90 || totalRotation == 270;
int rotatedWidth = height;
int rotatedHeigth = width;
if (swapRotation) {
rotatedWidth = width;
rotatedHeigth = height;
}
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeigth);
mVideoSize = chooseOptimalSize(map.getOutputSizes(MediaRecorder.class), rotatedWidth, rotatedHeigth);
mcamId = camId;
return;
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private static Size chooseOptimalSize(Size[] choices, int width, int height) {
List<Size> bigEnough = new ArrayList<Size>();
for (Size option : choices) {
if (option.getHeight() == option.getWidth() * height / width &&
option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
}
}
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizebyArea());
} else {
return choices[0];
}
}
私有静态类 CompareSizebyArea 实现比较器 {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * rhs.getHeight() / (long) rhs.getWidth() * lhs.getHeight());
}
}