如何采取同样的纵横比的照片在preVIEW大小?大小、照片、preVIEW

2023-09-06 03:34:56 作者:三千句情话不如一个拥抱

是99.9%,在标题中的问题的所有答案如下:

99.9% of all answers on a question in title are as follows:

当你通过名单,其中搜索; Camera.Size> ,提供的 Camera.Parameters#getSupportedPictureSizes()试图找到您通过 Camera.Parameters.html#设置previewSize(INT,INT),或尽可能接近。

When you searching through the List<Camera.Size>, provided by Camera.Parameters#getSupportedPictureSizes() try to find the size of the same aspect ratio as the camera's preview size which you set via Camera.Parameters.html#setPreviewSize(int,int), or as close as possible.

这很好,但如果我找不到同宽高比的画面尺寸在preVIEW大小(安卓让你不能保证每一个preVIEW大小相机支持有相同的宽高比的照片尺寸),和所有其他照片大小(这是的尽可能接近的)只是伸出我的照片在this和this问题吗?我怎么能做出完全相同的宽高比的照片在preVIEW,即使画面大小和preVIEW大小的 Camera.Parameters 有不同的纵横比?

That's fine, but what if I can't find the picture size of the same aspect ratio as in preview size (Android gives you no guarantee that for every preview size supported by the camera there is a picture size of the same aspect ratio), and all other picture sizes (which are as close as possible) just stretch my photo as in this and this questions? How I can make a photo of the exactly same aspect ratio as in preview, even if picture size and preview size set in Camera.Parameters have different aspect ratios?

推荐答案

我们可以了解在这个答案一般方法这个问题。短期报价:

We can learn about general approach to this issue in this answer. Short quote:

我的解决办法是,首先扩大了preVIEW选择矩形对本机相机图像尺寸。那么,现在我所知道的原始分辨率的哪个区域包含了我想要的内容,我可以做一个类似的操作,然后,扩展的原始分辨率较小的画面,实际上每Camera.Parameters.setPictureSize拍摄的矩形。

My solution was to first scale the preview selection rectangle to the native camera picture size. Then, now that I know which area of the native resolution contains the content I want, I can do a similar operation to then scale that rectangle on the native resolution to the smaller picture that was actually captured per Camera.Parameters.setPictureSize.

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

Now, to the actual code. The easiest way to perform scaling is to use Matrix. It has the method Matrix#setRectToRect(android.graphics.RectF, android.graphics.RectF, android.graphics.Matrix.ScaleToFit) which we can use like so:

// 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);

所有这些操作后,用 resultRect 将举行地区所采取的对应于你见过在$的完全一样的图像在相机里面的图片的坐标您的应用程序的对$ PVIEW。您可以从图片中的Bitma$p$pgionDe$c$cr.html#de$c$cRegion(android.graphics.Rect, android.graphics.BitmapFactory.Options)方法。

After all these manipulations the resultRect will hold the coordinates of area inside the picture taken by the camera which correspond to the exactly the same image you've seen in the preview of your app. You can cut this area from the picture by the BitmapRegionDecoder.html#decodeRegion(android.graphics.Rect, android.graphics.BitmapFactory.Options) method.

这就是它。