我正在为Android创建自定义相机,但相机previewSize显示低分辨率,我怎样才能使其达到最佳预览尺寸
这是我使用get previewSize的代码
private Camera.Size getBestPreviewSize(int width, int height) {
Camera.Size result = null;
Camera.Parameters p = camera.getParameters();
for (Camera.Size size : p.getSupportedPreviewSizes()) {
if (size.width <= width && size.height <= height) {
if (result == null) {
result = size;
} else {
int resultArea = result.width * result.height;
int newArea = size.width * size.height;
if (newArea > resultArea) {
result = size;
}
}
}
}
return result;
}
此代码的结果显示为width: 352: height: 288
使用下面的代码显示了其他一些解决方案
private Camera.Size getBestPreviewSize(int width, int height)
{
List<Size> sizes = camera.getParameters().getSupportedPreviewSizes();
if (sizes == null) return null;
Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = height;
int targetWidth = width;
int minWidthDiff = 0;
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.width - targetWidth) < minDiff) {
if(size.width > width) {
if (minWidthDiff == 0) {
minWidthDiff = size.width - width;
optimalSize = size;
}
else if (Math.abs(size.width - targetWidth) < minWidthDiff) {
minWidthDiff = size.width - width;
optimalSize = size;
}
minDiff = Math.abs(size.width - targetWidth);
}
}
}
}
if (optimalSize == null) {
minDiff = Double.MAX_VALUE;
for (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
此代码width: 640 : height: 480
的结果
但仍然预览看起来模糊
编辑:在使用之前停止预览
camera.stopPreview();
parameters.setPreviewSize(size.width, size.height);
parameters.setRotation(0);