在小米手机3上发现正常运行的程序拍照无法正常回调,且点击拍照按钮后会停留在最后拍照的界面,且日志里不会有任何报错信息。经排查发现是初始化相机的时候设置了setPictureSize()导致的。
奇怪的是系统相机一切正常,且可以用硬件最大分辨率,可是我的代码里只能支持最高为2688x1520的pictureSize,否则均无法收到onPictureTaken回调。
后来发现我的previewSize设置的是系统最高分辨率3840x2160,后来把分辨率改为系统屏幕近似分辨率后,pictureSize可以设置最高分辨率了。
下面给出代码:
/** 预览界面分辨率 */
public static void setPreviewSize(Camera.Parameters parametes) {
List<Camera.Size> localSizes = parametes.getSupportedPreviewSizes();
Camera.Size biggestSize = null;
Camera.Size fitSize = null;// 优先选屏幕分辨率
Camera.Size targetSize = null;// 没有屏幕分辨率就取跟屏幕分辨率相近(大)的size
Camera.Size targetSiz2 = null;// 没有屏幕分辨率就取跟屏幕分辨率相近(小)的size
if(localSizes != null) {
int cameraSizeLength = localSizes.size();
for (int n = 0; n < cameraSizeLength; n++) {
Camera.Size size = localSizes.get(n);
if(biggestSize == null ||
(size.width >= biggestSize.width && size.height >= biggestSize.height)) {
biggestSize = size;
}
if(size.width == DeviceUtils.screenHPixels
&& size.height == DeviceUtils.screenWPixels) {
fitSize = size;
} else if (size.width == DeviceUtils.screenHPixels
|| size.height == DeviceUtils.screenWPixels) {
if(targetSize == null) {
targetSize = size;
} else if (size.width < DeviceUtils.screenHPixels
|| size.height < DeviceUtils.screenWPixels) {
targetSiz2 = size;
}
}
}
if(fitSize == null) {
fitSize = targetSize;
}
if(fitSize == null) {
fitSize = targetSiz2;
}
if(fitSize == null) {
fitSize = biggestSize;
}
parametes.setPreviewSize(fitSize.width, fitSize.height);
}
}
/** 输出的照片为最高像素 */
public static void setPictureSize(Camera.Parameters parametes) {
List<Camera.Size> localSizes = parametes.getSupportedPictureSizes();
Camera.Size biggestSize = null;
Camera.Size fitSize = null;// 优先选预览界面的尺寸
Camera.Size previewSize = parametes.getPreviewSize();
float previewSizeScale = 0;
if(previewSize != null) {
previewSizeScale = previewSize.width / (float) previewSize.height;
}
if(localSizes != null) {
int cameraSizeLength = localSizes.size();
for (int n = 0; n < cameraSizeLength; n++) {
Camera.Size size = localSizes.get(n);
if(biggestSize == null) {
biggestSize = size;
} else if(size.width >= biggestSize.width && size.height >= biggestSize.height) {
biggestSize = size;
}
// 选出与预览界面等比的最高分辨率
if(previewSizeScale > 0
&& size.width >= previewSize.width && size.height >= previewSize.height) {
float sizeScale = size.width / (float) size.height;
if(sizeScale == previewSizeScale) {
if(fitSize == null) {
fitSize = size;
} else if(size.width >= fitSize.width && size.height >= fitSize.height) {
fitSize = size;
}
}
}
}
// 如果没有选出fitSize, 那么最大的Size就是FitSize
if(fitSize == null) {
fitSize = biggestSize;
}
parametes.setPictureSize(fitSize.width, fitSize.height);
}
}
原文地址:http://blog.csdn.net/h3c4lenovo/article/details/44808877