@Romanskiの以前の解決方法は問題なく動作しますが、トリミングで拡大縮小されます。フィットするようにスケールする必要がある場合は、次のソリューションを使用します。サーフェイスビューが変更されるたびに、つまりonSurfaceTextureAvailableメソッドとonSurfaceTextureSizeChangedメソッドでupdateTextureMatrixを呼び出します。また、このソリューションは、アクティビティが設定の変更を無視していることにも注意してください。アンドロイド: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;
はupdateTextureMatrixを呼び出す前にonSurfaceTextureAvailable mathodでそれを初期化します。
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方法で最初の呼び出しします):私はちょうど私がピクセル化を得ることなく、実際の入力のX2とのプレビューを表示するために必要な作業のアプリを作っている
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);
}
私はあなたが小さいプレビューで試してみることができますbtを確認していません。最初のカメラは、デフォルトのプレビューサイズに大きなものを与えます.. – Amrendra
私はプレビューサイズについて知っています。カメラのプレビューを表示するビューのサイズがプレビューのサイズと異なるため、これは役に立ちません。 –
画面上でフラグメントを試すことができます。 – Amrendra