【发布时间】:2015-01-13 12:06:09
【问题描述】:
我正在尝试使用相机 api 直接拍摄照片,但这是我得到的预览:
& 这是调用takePicture() 后拍摄的图像,比预览本身大:
(注意:我裁剪了前两张照片的高度以增强问题的可读性,并保持宽度不变)
我正在使用这个实用方法在开始相机预览之前选择最佳的最佳预览尺寸:
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 自己创建相机预览
那么,如何强制相机预览显示将要拍摄的确切照片?
【问题讨论】:
标签: android camera android-camera