Android Camera对焦相关

一、Camera 支持对焦模式简介

在维护MTK Camera模块的时候,遇到Camera对焦的问题,Camera支持的对焦模式定义在frameworks/base/core/java/android/hardware/Camera.java下,主要有

FOCUS_MODE_AUTO 自动对焦
FOCUS_MODE_INFINITY  无穷远
FOCUS_MODE_MACRO 微距拍摄
FOCUS_MODE_FIXED 固定对焦
FOCUS_MODE_EDOF 扩展景深
FOCUS_MODE_CONTINUOUS_VIDEO 视频记录的连续自动对焦

Android Camera对焦相关_第1张图片

二、将相机设置为自动连续对焦模式

public class MainActivity extends Activity {

    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private boolean flag = false;
    private String fileUrl="";
    Camera camera;
    Camera.Parameters parameters;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


      setContentView(R.layout.activity_main);
      surfaceView =  (SurfaceView) findViewById(R.id.surfaceView1);
      Button  button=(Button) findViewById(R.id.takepicture);
      surfaceHolder =  surfaceView.getHolder();
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
      surfaceHolder.setKeepScreenOn(true);
      surfaceView.setFocusable(true);
      surfaceView.setBackgroundColor(TRIM_MEMORY_BACKGROUND);
      surfaceHolder.addCallback(new Callback() {
      @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
             camera.stopPreview();
             camera.release();
             camera=null;

        }

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            if(null==camera){
                camera=Camera.open();
            try {
                      camera.setPreviewDisplay(surfaceHolder);
                      initCamera();
                      camera.startPreview();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
          }

        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
            //实现自动对焦
            camera.autoFocus(new AutoFocusCallback() {
                @Override
                public void onAutoFocus(boolean success, Camera camera) {
                    if(success){
                        initCamera();//实现相机的参数初始化
                        camera.cancelAutoFocus();//只有加上了这一句,才会自动对焦。
                    }
                }

            });
         }

    });
  }
      //相机参数的初始化设置
      private void initCamera()
        {
            parameters=camera.getParameters();
            parameters.setPictureFormat(PixelFormat.JPEG);
            //parameters.setPictureSize(surfaceView.getWidth(), surfaceView.getHeight());  // 部分定制手机,无法正常识别该方法。
            parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);   
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//1连续对焦
            setDispaly(parameters,camera);
            camera.setParameters(parameters);
            camera.startPreview();
            camera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上

        }

      //控制图像的正确显示方向
      private void setDispaly(Camera.Parameters parameters,Camera camera)
      {
          if (Integer.parseInt(Build.VERSION.SDK) >= 8){
                setDisplayOrientation(camera,90);
            }
        else{
                parameters.setRotation(90);
            }

      }     

      //实现的图像的正确显示
      private void setDisplayOrientation(Camera camera, int i) {
          Method downPolymorphic;
         try{
                downPolymorphic=camera.getClass().getMethod("setDisplayOrientation", new Class[]{int.class});
                if(downPolymorphic!=null) {
                    downPolymorphic.invoke(camera, new Object[]{i});
                }
            }
            catch(Exception e){
                Log.e("Came_e", "图像出错");
            }
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }     
}

三、对焦框绘制

1、AutoFocus流程会有3种图片,5种状态。
3种图片:正在对焦显示白框;对焦成功显示绿框;对焦失败显示红框。
5种状态:定义在vendor/mediatek/proprietary/packages/apps/Camera/src/com/android/camera/FocusManager.java

public static final int STATE_IDLE = 0; //Focus is not active,未启用对焦
public static final int STATE_FOCUSING = 1; //Focus is in progress,正在对焦
// Focus is in progress and the camera should take a picture after 
// focus finished.正在对焦并且对焦成功后会拍摄照片
public static final int STATE_FOCUSING_SNAP_ON_FINISH = 2;
public static final int STATE_SUCCESS = 3;// Focus finishes and succeeds.对焦成功完成
public static final int STATE_FAIL = 4;// Focus finishes and fails.对焦失败

2、对焦框与人脸框是互斥的,不能同时存在

// Ignore if the camera has detected some faces.
if (mFaceView != null && mFaceView.faceExists()) {
    mFocusIndicatorRotateLayout.clear();
}

三、其他

手机所搭载的摄像头一般都是定焦的,只是各个焦段的特性不一样,即只有一个焦平面是可以合焦的。因此在无穷远和微距模式下都是通过事先写在驱动中的一些参数去设置的。auto和continuous-picture 是摄像头本身就支持的对焦模式,可以把 continuous-picture理解为程序在检测到场景变化时自动autoFocus。

你可能感兴趣的:(android-camera,android,java,camera源码)