我發現了一個很奇怪的解決這一問題。當我的相機預覽和拍攝的照片具有相同的比例時,在所有測試過的設備上生成的照片看起來不錯因此,在獲得最佳預覽尺寸後,我正在以相同的比例搜索支持的圖片尺寸。
這很奇怪,但它的工作原理。
所以,首先我們需要預覽大小。
protected Size getOptimalPreviewSize(List<Size> sizes, int width, int height) {
Log.d(TAG, String
.format("getOptimalPreviewSize: width = %d, height = %d",
width, height));
final double ASPECT_TOLERANCE = 0.01;
final double targetRatio = (double) 4/3d;
if (sizes == null)
return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
// Try to find an size match aspect ratio and size
double ratio;
Size size;
for (int i = 0; i < sizes.size(); i++) {
size = sizes.get(i);
ratio = (double) size.width/size.height;
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
continue;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
// Cannot find the one match the aspect ratio, ignore the requirement
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (int i = 0; i < sizes.size(); i++) {
size = sizes.get(i);
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
if (optimalSize == null) {
Log.d(TAG, "Optimal size not found");
} else {
Log.d(TAG,
String.format(
"getOptimalPreviewSize result: width = %d, height = %d for input width = %d, height = %d",
optimalSize.width, optimalSize.height, width,
height));
}
return optimalSize;
}
然後我們需要得到圖片大小,這將與預覽大小相同。
private Size getOptimalPictureSize() {
if (mCamera == null)
return null;
List<Size> cameraSizes = mCamera.getParameters()
.getSupportedPictureSizes();
Size optimalSize = mCamera.new Size(0, 0);
double previewRatio = (double) mPreviewSize.width/mPreviewSize.height;
for (Size size : cameraSizes) {
double ratio = (double) size.width/size.height;
if (Math.abs(ratio - previewRatio) > 0.01f)
continue;
if (size.height > optimalSize.height) {
optimalSize = size;
}
}
if (optimalSize.height == 0) {
for (Size size : cameraSizes) {
if (size.height > optimalSize.height) {
optimalSize = size;
}
}
}
return optimalSize;
}
然後將此大小來Camera.Parametres
Size optimalSize = getOptimalPictureSize();
Parameters params = mCamera.getParameters();
Log.d(TAG, "picture size " + optimalSize.width + " "
+ optimalSize.height);
params.setPictureSize(optimalSize.width, optimalSize.height);
mCamera.setParameters(params);
有關更改曝光補償什麼?你可以使用'setExposureCompensation(int)'手動設置。 –
感謝您的回覆。我的應用應該能夠在不同類型的閃電中拍攝出好的照片,所以我只想使用自動設置。 – rstk