我的程序需要通过摄像头采集视频,然后将实时视频数据压缩编码传输,但是摄像头不能自动对焦,导致有的时候拍摄远的或者近的模糊,网上找的一些方法试了一下貌似都不行,有过相关方面的大神说说你们的见解,谢谢android摄像头 自动对焦
解决方案 »
- 关于Box2D在Android项目中的问题
- eclipse菜单里面的clean和build是干什么用的?
- android版随手记点击上面的账户,会弹出一个有按钮的框,这个功能怎么实现呢
- android调用自带摄像机 无法指定文件路径 ?
- notification上的图标设置后,呈拉伸状态,如何设置居中并保持图片大小不拉伸
- android中app注册时用手机号怎么弄
- 这么通过代码设置里面的号码
- android 一个app可以有多个 threadpool(线程池) 吗?
- 为什么我每次String转int时都有问题,文本框显示不出想要的数值?代码如下:
- Installation error:INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION
- 求嵌入pdf方法
- 第三方二维码软件扫描后 打开指定应用并转到指定界面,各位大神有何思路?
private void initCamera() {
try {
camera.stopPreview();
} catch (Exception e) {
// 忽略:试图停止不存在的预览
}
// 在此进行缩放、旋转和重新组织格式
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPictureSize();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPreviewSize(
sizes, (double) size.width / size.height);
if (optimalSize != null) {
parameters.setPreviewSize(optimalSize.width, optimalSize.height);
}
List<Integer> frameRates = parameters.getSupportedPreviewFrameRates();
if (frameRates != null) {
Integer max = Collections.max(frameRates);
parameters.setPreviewFrameRate(max);
}
// Size pSize = parameters.getPreviewSize();
parameters.setPictureFormat(PixelFormat.JPEG);
// parameters.setPreviewSize(pSize.width, pSize.height);
camera.setParameters(parameters);
// 以新的设置启动预览
try {
camera.startPreview();
} catch (Exception e) {
Log.d("TAG", "Error starting camera preview: " + e.getMessage());
}
}
这是我的方法,大部分看源码写的,下面这个方法更是抄源码的,效果不错,你可以试试,其实我感觉是分辨率的问题,也可能是对焦的问题,我再查查去。
private Size getOptimalPreviewSize(List<Size> sizes, double targetRatio) {
final double ASPECT_TOLERANCE = 0.05;
if (sizes == null) return null; Size optimalSize = null;
double minDiff = Double.MAX_VALUE; // Because of bugs of overlay and layout, we sometimes will try to
// layout the viewfinder in the portrait orientation and thus get the
// wrong size of mSurfaceView. When we change the preview size, the
// new overlay will be created before the old one closed, which causes
// an exception. For now, just get the screen size Display display = getWindowManager().getDefaultDisplay();
int targetHeight = Math.min(display.getHeight(), display.getWidth()); if (targetHeight <= 0) {
// We don't know the size of SurefaceView, use screen height
WindowManager windowManager = (WindowManager)
getSystemService(Context.WINDOW_SERVICE);
targetHeight = windowManager.getDefaultDisplay().getHeight();
} // Try to find an size match aspect ratio and size
for (Size size : sizes) {
double 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 (Size size : sizes) {
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
}
return optimalSize;
}
用这个对焦模式试试。