Android:拍摄的照片比预览大 [英] Android: Taken photo is bigger than the preview

查看:341
本文介绍了Android:拍摄的照片比预览大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图直接使用camera api捕获照片,但这是我的预览:

I'm trying to capture photos directly using the camera api, but this is the preview I got:

&这是调用 takePicture()之后拍摄的图片,比预览本身大:

& this is the image taken after calling takePicture() which is bigger than the preview itself:

(注意:我裁剪了前面2张照片,以提高问题的可读性,并保持原样的宽度)

(note: I cropped the height of the previous 2 photos to enhance question readability, & kept the width as is)

我使用此实用程序方法选择最佳最佳预览大小开始相机预览之前:

I'm using this utility method to choose best optimal preview size before starting the camera preview:

public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
                                                   int width,
                                                   int height,
                                                   Camera.Parameters parameters) {
    double targetRatio = (double) width / height;
    Camera.Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    if (displayOrientation == 90 || displayOrientation == 270) {
        targetRatio = (double) height / width;
    }
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Collections.sort(sizes,
            Collections.reverseOrder(new SizeComparator()));
    for (Camera.Size size : sizes) {
        double ratio = (double) size.width / size.height;
        if (Math.abs(ratio - targetRatio) < minDiff) {
            optimalSize = size;
            minDiff = Math.abs(ratio - targetRatio);
        }
        if (minDiff < 0.0d) {
            break;
        }
    }
    return (optimalSize);
}

&这种方法选择合适的图片大小:

& this method to choose a suitable picture size:

public static Camera.Size getBiggestSafePictureSize(Camera.Parameters parameters) {
    Camera.Size result = null;
    long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    long availableMemory = Runtime.getRuntime().maxMemory() - used;
    for (Camera.Size size : parameters.getSupportedPictureSizes()) {
        int newArea = size.width * size.height;
        long neededMemory = newArea * 4 * 4; // newArea * 4 Bytes/pixel * 4 needed copies of the bitmap (for safety :) )
        if (neededMemory > availableMemory)
            continue;
        if (result == null) {
            result = size;
        } else {
            int resultArea = result.width * result.height;
            if (newArea > resultArea) {
                result = size;
            }
        }
    }
    return (result);
}

&这是布局中的相机预览元素:

& this is the camera preview element in the layout:

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/cameraPreview"></FrameLayout>

&我正在官方文档中创建相机预览本身

& I'm following the official documentation for creating the camera preview itself

那么,如何强制相机预览显示准确的照片?

So, how to force the camera preview to show the exact photo that will be taken?

推荐答案

最后我发现:

根据 answer &我引用:

according to this answer & I quote:


虽然典型的相机是 4:3 宽高比,在5:3和16:9的比例,这似乎是通过实际扩展水平视野...

While the typical camera is a 4:3 aspect ratio, the preview may also be available in 5:3 and 16:9 ratios and this seems to be accomplished by actually extending the horizontal field of view...

我们需要找到一个预览大小图片尺寸(均为 4:3 宽高比),以便能够利用相机的全角度,因此我更改了我的代码:

So we need to find a preview size & a picture size, both with 4:3 aspect ratio to be able to utilize the full angle of the camera, so I changed my code like this:

public static Camera.Size determineBestPreviewSize(Camera.Parameters parameters) {
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    return determineBestSize(sizes);
}

public static Camera.Size determineBestPictureSize(Camera.Parameters parameters) {
    List<Camera.Size> sizes = parameters.getSupportedPictureSizes();
    return determineBestSize(sizes);
}

protected static Camera.Size determineBestSize(List<Camera.Size> sizes) {
    Camera.Size bestSize = null;
    long used = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
    long availableMemory = Runtime.getRuntime().maxMemory() - used;
    for (Camera.Size currentSize : sizes) {
        int newArea = currentSize.width * currentSize.height;
        long neededMemory = newArea * 4 * 4; // newArea * 4 Bytes/pixel * 4 needed copies of the bitmap (for safety :) )
        boolean isDesiredRatio = (currentSize.width / 4) == (currentSize.height / 3);
        boolean isBetterSize = (bestSize == null || currentSize.width > bestSize.width);
        boolean isSafe = neededMemory < availableMemory;
        if (isDesiredRatio && isBetterSize && isSafe) {
            bestSize = currentSize;
        }
    }
    if (bestSize == null) {
        return sizes.get(0);
    }
    return bestSize;
}

这篇关于Android:拍摄的照片比预览大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆