我尝试在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实现了Comparator {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * rhs.getHeight() / (long) rhs.getWidth() * lhs.getHeight());
}
}