尽管更改预览尺寸,CameraSource仍无法自动对焦

时间:2016-04-05 05:06:08

标签: java android camera android-camera

我试图通过实现CameraSource来使用条形码阅读器,通过这样做,我不再能够自动对焦应该能够这样做的设备。

cameraSource = new CameraSource.Builder(this, detector).setAutoFocusEnabled(true).build();

我用过这个,但是logcat对我喊道:

I/CameraSource: Camera auto focus is not supported on this device.

所以,我在像hereherehere这样的stackoverflow上挖掘。最后,这就是我所拥有的:

FrameLayout cameraFrame = (FrameLayout) findViewById(R.id.welcome_camera);
    if (cameraFrame != null) {
        cameraFrame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Field[] declaredFields = CameraSource.class.getDeclaredFields();

                for (Field field : declaredFields) {
                    if (field.getType() == Camera.class) {
                        field.setAccessible(true);
                        try {
                            Camera camera = (Camera) field.get(cameraSource);
                            if (camera != null) {
                                Camera.Parameters params = camera.getParameters();
                                for (Camera.Size size : params.getSupportedPictureSizes()) {
                                    Log.d("Sizes", size.height + " and width " + size.width);
                                }
                                params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
                                camera.setParameters(params);
                            }
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            }
        });
        cameraFrame.addView(preview);
    }

我想知道尺寸,因此对于xml我可以改变尺寸。此处的surfaceChanged函数也确认了该大小:     public void surfaceChanged(SurfaceHolder holder,int format,int w,int h){         //如果您的预览可以更改或旋转,请在此处理这些事件。         //确保在调整大小或重新格式化之前停止预览。

    if (mHolder.getSurface() == null){
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        cameraSource.stop();
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
    }
    // set preview size and make any resize, rotate or
    // reformatting changes here
    Log.d("Camerapreview", "h = " + h + " w = " + w);
    // start preview with new settings
    try {
        cameraSource.start(holder);
    } catch (SecurityException s) {
        Log.d(TAG, "shouldn't be here as permission should have been acquired by the main thread");
    }
    catch (Exception e){
        Log.d(TAG, "Error starting camera preview: " + e.getMessage());
    }
}

来自相机预览课程。

但是,上面的代码导致setParameters的RuntimeException在行camera.setParameters(params);处失败。我也试过FOCUS_MODE_CONTINUOUS_PICTURE,但这没有帮助。

0 个答案:

没有答案