How to achieve automatic focus of Android cameras, and it is continuous automatic focus. Of course, it goes without saying that you can call the system camera directly, that is very simple. Let’s mainly take a look at how to implement a camera by yourself 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; parameters; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); surfaceView = (SurfaceView) findViewById(.surfaceView1); Button button=(Button) findViewById(); surfaceHolder = (); (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); (true); (true); (TRIM_MEMORY_BACKGROUND); (new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub (); (); camera=null; } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub if(null==camera){ camera=(); try { (surfaceHolder); initCamera(); (); } catch (IOException e) { // TODO Auto-generated catch block (); } } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){ //Implement autofocus (new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if(success){ initCamera();//Realize the initialization of the camera parameters ();//Only by adding this sentence, the autofocus will be automatically focused. } } }); } }); } //Initialization settings of camera parameters private void initCamera() { parameters=(); (); //((), ()); // Some customized mobile phones cannot recognize this method normally. (Parameters.FLASH_MODE_TORCH); (.FOCUS_MODE_CONTINUOUS_PICTURE);//1 Continuous focus setDispaly(parameters,camera); (parameters); (); ();// 2 If you want to achieve continuous autofocus, this sentence must be added } //Control the correct display direction of the image private void setDispaly( parameters,Camera camera) { if (() >= 8){ setDisplayOrientation(camera,90); } else{ (90); } } //The correct display of the image implemented private void setDisplayOrientation(Camera camera, int i) { Method downPolymorphic; try{ downPolymorphic=().getMethod("setDisplayOrientation", new Class[]{}); if(downPolymorphic!=null) { (camera, new Object[]{i}); } } catch(Exception e){ ("Came_e", "Image error"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(, menu); return true; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.