实现Android手机摄像头的自动对焦

m2yy的头像 m2yy 12 2015-02-01 21:36 1

 基本信息

× 1   

浏览数: 10389

分享时间: 4 年 前

6
如何实现Android相机的自动对焦,而且是连续自动对焦的。当然直接调用系统相机就不用说了,那个很简单的。下面我们主要来看看如如何自己实现一个相机,并且实现自动连续对焦。

代码如下:

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


  • LavenderMa的头像 LavenderMa 2017-11-22 17:18 代码数:0

    请问一下作者,为什么这个在中兴5.0.1的手机上可以自动聚焦,但是在oppoR9s 6.0.1和红米3s 6.0.1上面并没有实现自动聚焦,请问针对这个现象有什么解决方案么??

     

  • ossaa的头像 ossaa 2018-12-26 05:29 代码数:0

    Thanks for your post. I’ve been thinking about writing a very comparable post over the last couple of weeks, I’ll probably keep it short and sweet and link  to this instead if thats cool. Thanks. PLTNUM

  • ossaa的头像 ossaa 2018-12-27 03:35 代码数:0

    Thanks for your insight for your fantastic posting. I’m glad I have taken the time to see this.

    lottozahlenonline

  • ossaa的头像 ossaa 2019-01-08 19:02 代码数:0

    Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. Easel stands

  • tangpig的头像 tangpig 2017-04-12 15:21 代码数:0

    不错

  • 65271243的头像 65271243 2017-05-29 21:36 代码数:0

    我就来拿积分就走

您的评论: