【发布时间】:2014-06-07 14:47:30
【问题描述】:
我在 Android 上遇到了相机问题,基本上,当我使用 Camera 类拍照时,拍摄的输出图片实际上显示的不仅仅是预览。最好的表达方式是裁剪预览。我该如何解决这个问题?
我在 Samsung Galaxy S4、Samsung Galaxy S3、HTC One M8 和 Nexus One 上运行过该应用程序,都出现了同样的问题。 (所有运行 Android 4.0 ICS)
在没有操作栏和隐藏状态栏的 Activity 中,相机预览是全屏的。
我要创建的相机是纵向的,所以我将 displayOrientation 设置为 90。
我用于确定预览大小和输出照片大小的代码如下(我相信问题很可能出在某处):
public static Camera.Size getBestAspectPreviewSize(int displayOrientation,
int width,
int height,
Camera.Parameters parameters,
double closeEnough) {
double targetRatio=(double)width / height;
Camera.Size optimalSize=null;
double minDiff=Double.MAX_VALUE;
if (displayOrientation == 90 || displayOrientation == 270) {
targetRatio=(double)height / width;
}
List<Size> sizes=parameters.getSupportedPreviewSizes();
Collections.sort(sizes,
Collections.reverseOrder(new SizeComparator()));
for (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 < closeEnough) {
break;
}
}
return(optimalSize);
}
public static Camera.Size getLargestPictureSize(CameraHost host,
Camera.Parameters parameters,
boolean enforceProfile) {
Camera.Size result=null;
for (Camera.Size size : parameters.getSupportedPictureSizes()) {
// android.util.Log.d("CWAC-Camera",
// String.format("%d x %d", size.width, size.height));
if (!enforceProfile
|| (size.height <= host.getDeviceProfile()
.getMaxPictureHeight() && size.height >= host.getDeviceProfile()
.getMinPictureHeight())) {
if (result == null) {
result=size;
}
else {
int resultArea=result.width * result.height;
int newArea=size.width * size.height;
if (newArea > resultArea) {
result=size;
}
}
}
}
if (result == null && enforceProfile) {
result=getLargestPictureSize(host, parameters, false);
}
return(result);
}
尺寸比较器类:
private static class SizeComparator implements
Comparator<Camera.Size> {
@Override
public int compare(Size lhs, Size rhs) {
int left=lhs.width * lhs.height;
int right=rhs.width * rhs.height;
if (left < right) {
return(-1);
}
else if (left > right) {
return(1);
}
return(0);
}
}
这是使用 cwac-camera 并使用 SimpleCameraHost,没有任何变化。
感谢任何帮助!
【问题讨论】:
标签: android camera commonsware-cwac