首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何拍摄与预览尺寸相同的高宽比照片?

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

Stack Overflow用户
提问于 2014-06-24 19:02:58
回答 1查看 2.4K关注 0票数 6

99.9%有关题目的问题的答案如下:

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

这很好,但如果我找不到的图片大小与预览大小相同(Android不能保证摄像机支持的每一个预览大小都有相同的高宽比),那么所有其他图片大小(尽可能接近)都可以像这和这问题一样拉伸我的照片吗?如果Camera.Parameters中设置的图片大小和预览比例不同,我如何制作一张与预览中相同的宽宽比的照片?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-24 19:02:58

我们可以在这个答案中了解解决这一问题的一般方法。简短引述:

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

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

代码语言:javascript
复制
// 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)方法从图片中分割这个区域。

就是这样。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24394334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档