【问题标题】:How to take a photo of the same aspect ratio as in preview size?如何拍摄与预览尺寸相同纵横比的照片?
【发布时间】:2014-08-15 04:05:11
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-camera aspect-ratio


    【解决方案1】:

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

    我的解决方案是首先将预览选择矩形缩放到本机相机图片大小。然后,既然我知道原生分辨率的哪个区域包含我想要的内容,我可以执行类似的操作,然后将原生分辨率上的矩形缩放为每个 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) 方法从图片中剪切此区域。

    就是这样。

    【讨论】:

    • 嘿,我知道这个解决方案已经过时了,但也许你可以帮忙 - 当照片的预览比屏幕尺寸大 时,我怎样才能让它工作?所以 screenWidth
    • 嘿。你如何获得 nativeResRect?
    • 谢谢。这可以更好地了解从传感器到 jpeg 的所有层中发生的情况
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 2017-01-23
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多