在创建程序布局时,预览在Camera2 API中拉伸

Ent*_*ain 5 java android android-camera android-camera2

在我们的应用程序更新后,我们现在使用Camera2 API,遗憾的是预览在我们的测试设备上拉伸:Samsung SM-J330F/DS,Android-Version 8.0.0,API 26

因为我们在同一设备上没有遇到Googles Camera2Basic项目的这个问题,所以我们尝试调整我们的项目以使用相同的预览实现.目前我们无法弄清楚两个项目之间不同预览的确切原因.

我们的预览体验了预览.

这是预期的:

在同一设备上正确预览Camera2Basic项目

与Camera2Basic项目相比,我们RelativeLayout在Activity类中以编程方式创建,AutoFitTextureView然后将其添加到此对象:

mMainLayout = new FrameLayout(this);
mMainLayout.setBackgroundColor(Color.BLACK);
mMainLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));

mPreview = new AutoFitTextureView(this);
mPreview.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT, Gravity.TOP));

mMainLayout.addView(mPreview);
this.setContentView(mMainLayout);
Run Code Online (Sandbox Code Playgroud)

实现AutoFitTextureView与Camera2Basic项目相同.

稍后在将预览设置setUpCameraOutputs()为640x480像素(所有设备应支持的分辨率)之后的Camera2类中,我们调用setAspectRatio(width, height)mPreview:

mPreviewSize = new Size(640, 480);

if (mFrameOrientation == Configuration.ORIENTATION_LANDSCAPE) {
    mTextureView.setAspectRatio(
        mPreviewSize.getWidth(), mPreviewSize.getHeight()
    );
} else {
    mTextureView.setAspectRatio(
        mPreviewSize.getHeight(), mPreviewSize.getWidth()
    );
}
Run Code Online (Sandbox Code Playgroud)

注意:同样在较新的设备上(如荣誉10),预览看起来很好.

Ent*_*ain 0

openCamera()该问题的解决方案是在和中调用以下函数onSurfaceTextureSizeChanged()

/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 */
private void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    if (null == mTextureView || null == mPreviewSize || null == activity) {
        return;
    }
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max(
                (float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    mTextureView.setTransform(matrix);
}
Run Code Online (Sandbox Code Playgroud)