gpt4 book ai didi

android - 如何拍摄与预览尺寸相同纵横比的照片?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:40:23 24 4
gpt4 key购买 nike

99.9% 的标题问题的答案如下:

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.

没关系,但是如果我找不到与预览尺寸相同纵横比的图片尺寸怎么办(Android 不保证相机支持的每种预览尺寸都有相同宽高比的图片尺寸)和所有其他图片尺寸(尽可能接近)只是拉伸(stretch)我的照片,如 thisthis问题?即使在 Camera.Parameters 中设置了图片尺寸和预览尺寸,我如何制作与预览中的纵横比完全相同的照片有不同的纵横比?

最佳答案

我们可以在 this answer 中了解解决此问题的一般方法.简短引述:

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.

现在,进入实际代码。执行缩放的最简单方法是使用 Matrix .它有方法 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);

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

就是这样。

关于android - 如何拍摄与预览尺寸相同纵横比的照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24394334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com