gpt4 book ai didi

Android:拍摄的照片比预览大

转载 作者:可可西里 更新时间:2023-11-01 19:00:23 25 4
gpt4 key购买 nike

我正在尝试使用相机 API 直接拍摄照片,但这是我得到的预览: enter image description here

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

enter image description here

(注意:我裁剪了前两张照片的高度以增强问题的可读性,并保持宽度不变)

在开始相机预览之前,我正在使用此实用程序方法来选择最佳预览尺寸:

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

&此方法选择合适的图片尺寸:

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

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

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

&我正在关注the official documentation用于创建相机预览本身

那么,如何强制相机预览显示将要拍摄的确切照片?

最佳答案

终于找到了:)

根据这个answer &我引用:

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 的宽高比,以便能够利用相机的全角,所以我这样更改了我的代码:

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:拍摄的照片比预览大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958110/

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