Android相机视图的长宽比

时间:2018-10-17 21:36:00

标签: android camera

我根据此处描述的示例在Android上创建了相机预览:https://github.com/googlesamples/android-Camera2Basic/blob/master/Application/src/main/java/com/example/android/camera2basic/Camera2BasicFragment.java

我计算了相机的视野和视图的大小。为了简化问题,我将视图的宽度保持为基本,并根据FoV比例缩放垂直尺寸。结果宽度为1080px,高度为1374px。该比例显示的相机视图没有失真,因此正如我所预期的那样看起来是真实的1:1。

我将宽度更改为四分之一(270像素),并保持了高度(1374像素),但是我希望通过裁切图片来保持1:1的比例。我将图片的x值乘以4以达到目标。缩放比例不为1:1后,图片更宽。我大约乘以3会得出1:1的比例。

怎么了?

这是计算值的代码。我删除了所有轮换内容,只保留了相关代码段。

private void configureTransform(int viewWidth, int viewHeight) {
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();

    Camera.FOV cameraFov = getCameraFOV(); // it calculates with the 90 degree rotation of the camera
    float ratioFov = cameraFov.horizontal / cameraFov.vertical; // = 0.7859
    float ratioView = (float) viewWidth / viewHeight; // = 0.1965

    if (ratioFov > ratioView) {
        matrix.postScale(ratioFov / ratioView, 1, centerX, centerY); // this branch evaluated; ratioFov / ratioView = 4
    } else {
        matrix.postScale(1, ratioView / ratioFov, centerX, centerY);
    }

    mTextureView.setTransform(matrix);
}

0 个答案:

没有答案