旋转相机会导致表面视图缩小

时间:2018-03-23 12:54:18

标签: android camera

我已经在我的Android应用程序中实现了一个自定义相机,但是当我旋转我的设备时,我的表面视图被旋转,但问题是它缩小了,我希望两个方向都有相同的比率图像。

This is Landscape view This is Portrait view

在我的纵向视图中,我的预览正在缩小,但是我得到了全尺寸图像,例如当我从横向视图中拍摄图像时,我得到尺寸为1080 * 960的图像,其中1080是宽度,960是高度当我从纵向视图中单击图像时,图像的高度为1080,宽度为960,但我希望图像与横向模式相同

以下是我的Surface代码

 public void surfaceCreated(SurfaceHolder holder) {
    mSurfaceHolder = holder;
    if (mCamera != null) {
        stopCameraPreview();
        mCamera.release();

    }
    getCamera(mCameraID);
    setCameraDisplayOrientation(activity,mCameraID,mCamera);
    startCameraPreview();

}

public static void setCameraDisplayOrientation(Activity activity,
                                               int cameraId, android.hardware.Camera camera) {

    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();

    android.hardware.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);
}


private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 0.1;
    double targetRatio = (double) h / w;

    if (sizes == null)
        return null;

    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;

    int targetHeight = h;

    for (Camera.Size size : sizes) {
        double ratio = (double) size.height / size.width;
        if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
            continue;

        if (Math.abs(size.height - targetHeight) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }

    if (optimalSize == null) {
        minDiff = Double.MAX_VALUE;
        for (Camera.Size size : sizes) {
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }
    }

    return optimalSize;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    try {
        mCamera.startPreview();
        mIsSafeToTakePhoto = true;
    } catch (Exception e) {
        e.printStackTrace();
    }


}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // The surface is destroyed with the visibility of the SurfaceView is set to View.Invisible
    try {

        if (mCamera != null) {
            stopCameraPreview();
            mCamera.release();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案