4

我遇到了 Camera2 API 的问题,我基本上是在使用 Google 示例代码(Camera2Basic),但我确实更改了 TextureView 的大小,因此它不会跨越整个屏幕,屏幕也有一个ToolBar.

我得到的预览显示非常倾斜。

The TextureView size is    w:1080   h:1620   (2/3)
The preview size is        w:720    h:480    (3/2)

这是 configureTransform 的代码

  private void configureTransform(int viewWidth, int viewHeight) {
        if (null == mTextureView || null == mPreviewSize || null == activity) {
            return;
        }

        Log.i(CameraFragment.LOG_TAG, "viewWidth: " + viewWidth + "   viewHeight: " + viewHeight + "   ratio: " + (float)viewWidth/(float)viewHeight);
        Log.i(CameraFragment.LOG_TAG, "previewWidth: " + mPreviewSize.getWidth() + "   previewHeight: " + mPreviewSize.getHeight() + "   ratio: " + (float)mPreviewSize.getWidth()/(float)mPreviewSize.getHeight());
        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);
    }
4

0 回答 0