I am trying to use autofoucs i have no idea where to put autofocus anyone has an idea how to implement autofocus?
----------preview class----------
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();
}
}
-----------------camera activity------------------------
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");
}
};
If you have autofocus set, you should do something like this:
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if(success){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
});
That's the way the camera waits for the autofocus ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With