如何拍摄与预览尺寸相同的宽高比?

aga*_*aga 6 android aspect-ratio android-camera

标题中所有答案的99.9%如下:

当你搜索List<Camera.Size>,由Camera.Parameters#getSupportedPictureSizes()提供时,尝试找到与通过Camera.Parameters.html#setPreviewSize(int,int)设置的相机预览大小相同的宽高比的大小,或者尽可能接近尽可能.

这很好,但是如果我找不到与预览尺寸相同的宽高比的图片尺寸(Android不能保证对于相机支持的每个预览尺寸都有相同宽高比的图片尺寸),和所有其他图片尺寸(尽可能接近)只是拉伸我的照片,如此这个问题?即使在Camera.Parameters中设置的图片大小和预览大小具有不同的宽高比,我如何制作与预览完全相同的宽高比的照片?

aga*_*aga 10

我们可以在这个答案中了解这个问题的一般方法.简短的报价:

我的解决方案是首先将预览选择矩形缩放到原始相机图片大小.然后,现在我知道原始分辨率的哪个区域包含我想要的内容,我可以执行类似的操作,然后将原始分辨率上的矩形缩放到每个Camera.Parameters.setPictureSize实际捕获的较小图片.

现在,到实际的代码.执行缩放的最简单方法是使用Matrix.它具有方法矩阵#setRectToRect(android.graphics.RectF,android.graphics.RectF,android.graphics.Matrix.ScaleToFit) ,我们可以使用像这样:

// Here previewRect is a rectangle which holds the camera's preview size,
// pictureRect and nativeResRect hold the camera's picture size and its 
// native resolution, respectively.
RectF previewRect = new RectF(0, 0, 480, 800),
      pictureRect = new RectF(0, 0, 1080, 1920),
      nativeResRect = new RectF(0, 0, 1952, 2592),
      resultRect = new RectF(0, 0, 480, 800);

final Matrix scaleMatrix = new Matrix();

// create a matrix which scales coordinates of preview size rectangle into the 
// camera's native resolution.
scaleMatrix.setRectToRect(previewRect, nativeResRect, Matrix.ScaleToFit.CENTER);

// map the result rectangle to the new coordinates
scaleMatrix.mapRect(resultRect);

// create a matrix which scales coordinates of picture size rectangle into the 
// camera's native resolution.
scaleMatrix.setRectToRect(pictureRect, nativeResRect, Matrix.ScaleToFit.CENTER);

// invert it, so that we get the matrix which downscales the rectangle from 
// the native resolution to the actual picture size
scaleMatrix.invert(scaleMatrix);

// and map the result rectangle to the coordinates in the picture size rectangle
scaleMatrix.mapRect(resultRect);
Run Code Online (Sandbox Code Playgroud)

在所有这些操作之后,resultRect将保持摄像机拍摄的照片内部区域的坐标,该坐标对应于您在应用预览中看到的完全相同的图像.您可以通过画面切这方面BitmapRegionDecoder.html#decodeRegion(android.graphics.Rect,android.graphics.BitmapFactory.Options)方法.

就是这样.