我正在尝试使用autofoucs我不知道在哪里放置自动对焦任何人都知道如何实现自动对焦?
----------预览班----------
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");
}
};
答案 0 :(得分:14)
如果您设置了自动对焦,则应该执行以下操作:
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if(success){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
});
这就是相机等待自动对焦的方式;)
答案 1 :(得分: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);
}
答案 2 :(得分:0)
camera.autoFocus (new Camera.AutoFocusCallback {});
答案 3 :(得分:-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);
}