Android realizes auto focus on mobile camera _android

Source: Internet
Author: User
Tags stub

How to achieve the auto focus of the Android camera, and it is continuously autofocus. Of course the direct call to the system camera is needless to say, that is very simple. Let's take a look at how to implement a camera on your own and achieve automatic continuous focus.

The code is as follows:

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 Au
  to-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) { Implement AF Camera.autofocus (new Autofocuscallback () {@Override public void Onautofocus (Boolean success, Ca Mera camera) {if (success) {Initcamera ();//implementation of the camera's parameter initialization camera.cancelautofocus (); only by adding this sentence, will automatically
         Coke.
    }
        }

  });
 }

 });
    }//Camera parameter initialization setting private void Initcamera () {parameters=camera.getparameters ();
    Parameters.setpictureformat (Pixelformat.jpeg); Parameters.setpicturesize (Surfaceview.getwidth (), Surfaceview.getheight ());
  Some custom phones do not recognize the method properly. Parameters.setflashmode (parameters.fLash_mode_torch);
  Parameters.setfocusmode (Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);//1 continuous Focus setdispaly (Parameters,camera);
  Camera.setparameters (parameters);
  Camera.startpreview (); Camera.cancelautofocus ()//2 if you want to achieve continuous autofocus, this sentence must be added}//control the correct display direction of the image private void Setdispaly (Camera.parameters pa
  Rameters,camera Camera) {if (Integer.parseint (Build.VERSION.SDK) >= 8) {setdisplayorientation (camera,90);
  } else{Parameters.setrotation (90);
 }///implementation of the correct display of the image 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});
  The catch (Exception e) {log.e ("came_e", "image Error"); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the AC tion Bar if it is PreseNt.
    
    Getmenuinflater (). Inflate (R.menu.main, menu);
  return true;
 }
  
 
 
}

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.