我跟隨youtube tutorial,試圖學習Camera2 API。當然,我在開發自己的應用程序的同時正在學習這一點。教程和我的應用程序之間的一個不一致之處在於,教程僅在我的應用程序處於橫向模式時纔將相機設置爲縱向模式。Android Camera2預覽在風景中旋轉90度
我目前能夠查看相機的預覽,儘管當我的應用處於橫向或橫向時,相機預覽看起來旋轉了90度。它幾乎感覺我可以旋轉TextureView,但看起來不正確,就像我拍照時,它會被錯誤地旋轉。
下面是與圖像尺寸做的代碼(整個代碼很長)
private void setupCamera(int width, int height) {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(camera_id);
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height);
mCameraId = camera_id;
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
//TODO Look for a way to make this horizontal
private Size getPreferredPreviewSize(Size[] mapSizes, int width, int height) {
List<Size> collectorSizes = new ArrayList<>();
for (Size option : mapSizes) {
if (width > height) { //If the screen is in landscape
Toast.makeText(getApplicationContext(), "Screen is Landscape", Toast.LENGTH_SHORT).show();
if (option.getWidth() > width && option.getHeight() > height) {
collectorSizes.add(option);
}
} else { //if the screen is in portrait
Toast.makeText(getApplicationContext(), "Screen is Portrait", Toast.LENGTH_SHORT).show();
if (option.getWidth() > height && option.getHeight() > width) {
collectorSizes.add(option);
}
}
}
if (collectorSizes.size() > 0) {
return Collections.min(collectorSizes, new Comparator<Size>() {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() + rhs.getHeight());
}
});
}
return mapSizes[0];
}
private void openCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, null);
} catch (CameraAccessException e){
e.printStackTrace();
}
}
private void createCameraPreviewSession() {
try {
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewCaptureRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
if(mCameraDevice == null){
return;
}
try {
mPreviewCaptureRequest = mPreviewCaptureRequestBuilder.build();
mCameraCaptureSession = session;
mCameraCaptureSession.setRepeatingRequest(mPreviewCaptureRequest, mSessionCaptureCallback, null);
} catch (CameraAccessException e){
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(), "Preview Session Failed", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e){
e.printStackTrace();
}
}
我一直在玩的getPreferredPreviewSize
方法,但我不明白它,以及我應該。我不確定在使用lhs
和rhs
的末尾compare
。
我是否缺少一些簡單的東西讓它旋轉?
這可能有助於... [Android的camera2 API - 第13部分固色屏幕預覽方向(https://www.youtube.com/watch?v=YvS3iGKhQ_g) – ntgCleaner