2011-11-08 44 views
4

我想使用自動檢測我不知道在哪裏把自動對焦任何人有一個想法如何實現自動對焦?在哪裏把自動對焦在類安卓相機

----------預覽類----------

package com.marakana; 

import java.io.IOException; 

import android.content.Context; 
import android.hardware.Camera; 
import android.hardware.Camera.PreviewCallback; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

class Preview extends SurfaceView implements SurfaceHolder.Callback { // <1> 
private static final String TAG = "Preview"; 

SurfaceHolder mHolder; // <2> 
public Camera camera; // <3> 

Preview(Context context) { 
super(context); 

// Install a SurfaceHolder.Callback so we get notified when the 
// underlying surface is created and destroyed. 
mHolder = getHolder(); // <4> 
mHolder.addCallback(this); // <5> 
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); // <6> 
} 

// Called once the holder is ready 
public void surfaceCreated(SurfaceHolder holder) { // <7> 
// The Surface has been created, acquire the camera and tell it where 
// to draw. 
camera = Camera.open(); // <8> 
try { 
    camera.setPreviewDisplay(holder); // <9> 

    camera.setPreviewCallback(new PreviewCallback() { // <10> 
    // Called for each frame previewed 
    public void onPreviewFrame(byte[] data, Camera camera) { // <11> 
     Log.d(TAG, "onPreviewFrame called at: " + System.currentTimeMillis()); 
     Preview.this.invalidate(); // <12> 
    } 
    }); 
} catch (IOException e) { // <13> 
    e.printStackTrace(); 
    } 
    } 

// Called when the holder is destroyed 
public void surfaceDestroyed(SurfaceHolder holder) { // <14> 
camera.stopPreview(); 
camera = null; 
} 

// Called when holder has changed 
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // <15> 
    camera.startPreview(); 
} 

} 

-----------------相機活動------------------------

preview = new Preview(this); // <3> 
((FrameLayout) findViewById(R.id.preview)).addView(preview); // <4> 

buttonClick = (Button) findViewById(R.id.buttonClick); 
buttonClick.setOnClickListener(new OnClickListener() { 
    public void onClick(View v) { // <5> 

     preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
    } 
}); 

// Handles data for jpeg picture 
PictureCallback jpegCallback = new PictureCallback() { // <8> 
public void onPictureTaken(byte[] data, Camera camera) { 
    FileOutputStream outStream = null; 
    try { 
    // Write to SD Card 

    outStream = new FileOutputStream(imgPath); // <9> 
    outStream.write(data); 
    outStream.close(); 
    intImg = new Intent(CameraActivity.this, DisplayImg.class); 
    intImg.putExtra("imgPath",imgPath); 
    startActivity(intImg); 
    Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length); 
    } catch (FileNotFoundException e) { // <10> 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    } 
    Log.d(TAG, "onPictureTaken - jpeg"); 
} 
}; 

回答

-1

你可以使用它,你需要之前圖片。

preview.camera.autoFocus(null) // you can make optional callback once focus is achieved 
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback); 

在表面變化和預覽之前,你可以使用自動設置焦距,然後開始預覽,然後自動對焦,顯示無論在自動對焦設置的。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 

// Set camera properties first 
Camera.Parameters parameters = camera.getParameters(); 
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); 
     camera.setParameters(parameters); 

camera.startPreview(); 

camera.autoFocus(null); 
} 
+0

拍攝照片之前,您必須等待焦點。如果沒有,這是沒用的 – Kasas

14

如果你有自動對焦設置,你應該做這樣的事情:

camera.autoFocus(new AutoFocusCallback() { 
     @Override 
     public void onAutoFocus(boolean success, Camera camera) { 
      if(success){ 
       camera.takePicture(shutterCallback, rawCallback, jpegCallback); 
      } 
     } 
    }); 

這是相機等待自動對焦的方式;)

+1

這應該被標記爲正確的答案。 – bcorso

1

由於API 9級就可以實現自動重點關注(自動):

surfaceChanged()方法中,請進行以下更改:

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 

// Set camera properties first 
Camera.Parameters parameters = camera.getParameters(); 
     parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); 
     camera.setParameters(parameters); 

camera.startPreview(); 

camera.autoFocus(null); 
}