我的程序需要通过摄像头采集视频,然后将实时视频数据压缩编码传输,但是摄像头不能自动对焦,导致有的时候拍摄远的或者近的模糊,网上找的一些方法试了一下貌似都不行,有过相关方面的大神说说你们的见解,谢谢android摄像头 自动对焦

解决方案 »

  1.   


    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;
        }
      

  2.   

    mParameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
    用这个对焦模式试试。
      

  3.   

    自动对焦可以根据检测重力传感器的变化来做,每次xyz方向发生一定的变化时让camera去对焦就可以了,单独设定楼上的属性是没有用的,我试过,而且在做自动对焦前要先看下手机支持自动对焦否
      

  4.   

    楼上 正解 我曾经测试过 mParameters.setFocusMode(Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);如果把手机固定好,不太晃动手机,无论你推进或者放远手机 都不会聚焦