@Romanski의 초기 해결 방법은 잘 작동하지만 자르기와 함께 확장됩니다. 크기에 맞게 조정해야하는 경우 다음 솔루션을 사용하십시오. 표면 뷰가 변경 될 때마다 updateTextureMatrix를 호출하십시오. 즉, onSurfaceTextureAvailable 및 onSurfaceTextureSizeChanged 메소드에서 호출하십시오. 또한이 솔루션은 활동이 구성 변경 사항을 무시한다는 점에 유의하십시오 (예 :안드로이드 : configChanges = "오리엔테이션 | 화면 크기 | keyboardHidden"또는 그런 일) :
또한
private void updateTextureMatrix(int width, int height)
{
boolean isPortrait = false;
Display display = getWindowManager().getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180) isPortrait = true;
else if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) isPortrait = false;
int previewWidth = orgPreviewWidth;
int previewHeight = orgPreviewHeight;
if (isPortrait)
{
previewWidth = orgPreviewHeight;
previewHeight = orgPreviewWidth;
}
float ratioSurface = (float) width/height;
float ratioPreview = (float) previewWidth/previewHeight;
float scaleX;
float scaleY;
if (ratioSurface > ratioPreview)
{
scaleX = (float) height/previewHeight;
scaleY = 1;
}
else
{
scaleX = 1;
scaleY = (float) width/previewWidth;
}
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
textureView.setTransform(matrix);
float scaledWidth = width * scaleX;
float scaledHeight = height * scaleY;
float dx = (width - scaledWidth)/2;
float dy = (height - scaledHeight)/2;
textureView.setTranslationX(dx);
textureView.setTranslationY(dy);
}
당신이 필요로하는 다음과 같은 필드 :
private int orgPreviewWidth;
private int orgPreviewHeight;
이 onSurfaceTextureAvailable mathod에 초기화 updateTextureMatrix를 호출하기 전에 :
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
Pair<Integer, Integer> size = getMaxSize(parameters.getSupportedPreviewSizes());
parameters.setPreviewSize(size.first, size.second);
orgPreviewWidth = size.first;
orgPreviewHeight = size.second;
camera.setParameters(parameters);
getMaxSize 방법 :
private static Pair<Integer, Integer> getMaxSize(List<Camera.Size> list)
{
int width = 0;
int height = 0;
for (Camera.Size size : list) {
if (size.width * size.height > width * height)
{
width = size.width;
height = size.height;
}
}
return new Pair<Integer, Integer>(width, height);
}
그리고 마지막으로 카메라 회전을 수정해야합니다. 그래서 활동의 onConfigurationChanged 방법 setCameraDisplayOrientation 메소드를 호출 (도 onSurfaceTextureAvailable 방법에서 초기 호출 할) : 난 그냥 픽셀 화를받지 않고 실제 입력의 2 배와 미리보기를 표시하는 데 필요한 작업 응용 프로그램을 만들었습니다
public static void setCameraDisplayOrientation(Activity activity, int cameraId, Camera camera)
{
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
{
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360; // compensate the mirror
}
else
{ // back-facing
result = (info.orientation - degrees + 360) % 360;
}
camera.setDisplayOrientation(result);
Camera.Parameters params = camera.getParameters();
params.setRotation(result);
camera.setParameters(params);
}
작은 미리보기로 시도해 볼 수 있을지 잘 모르겠다. 처음에는 기본 미리보기 크기가 큰 카메라를 제공합니다. – Amrendra
미리보기 크기에 대해 알고 있습니다. 카메라 미리보기가 미리보기 크기와 다른 크기로 표시되므로보기에는 도움이되지 않습니다. –
화면에서 조각을 시도 할 수 있습니다. – Amrendra