1. 程式人生 > >Android Camera對焦相關

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 視訊記錄的連續自動對焦

這裡寫圖片描述

二、將相機設定為自動連續對焦模式

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。