Android相机自动对焦 Android实现手机摄像头的自动对焦
m2yy 人气:0想了解Android实现手机摄像头的自动对焦的相关内容吗,m2yy在本文为您仔细讲解Android相机自动对焦的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android相机自动对焦,Android手机摄像头自动对焦,Android摄像头自动对焦,Android手机自动对焦,下面大家一起来学习吧。
如何实现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; } }
加载全部内容