I am trying to create my own Camera View, I have everything working except the autofocus, I can't seem to figure out why it won't work. Here is my code for CameraView.java
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder surface_Holder;
private Camera main_Camera;
boolean on;
public CameraView(Context context, Camera camera){
super(context);
main_Camera = camera;
main_Camera.setDisplayOrientation(90);
surface_Holder = getHolder();
surface_Holder.addCallback(this);
surface_Holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
}
public boolean isOn(){
return on;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try{
main_Camera.setPreviewDisplay(holder);
main_Camera.startPreview();
}catch (Exception e){
Log.d("Error", "Canmera error on surfaceCreated" + e.getMessage());
main_Camera.release();
main_Camera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(holder.getSurface()==null){
return;
}
try{
main_Camera.stopPreview();
}catch (Exception e){
}
try{
main_Camera.setPreviewDisplay(surface_Holder);
main_Camera.startPreview();
}catch (IOException e){
Log.d("Error", "Camera error on surfaceChanged " + e.getMessage());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
main_Camera.setPreviewCallback(null);
main_Camera.stopPreview();
main_Camera.release();
main_Camera= null;
}
}
Inside my manifest I have the following:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
if you added <uses-feature android:name="android.hardware.camera.autofocus" />
to your manifest it doesn't mean the camera will make autofocus
. It means you give your app the permission to use camera hardware or software that take care of autofocus.
The purpose of a declaration is to inform any external entity of the set of hardware and software features on which your application depends.
To set your camera to focus you can add this method to your CameraView
class:
private void setFocus(String mParameter) {
Camera.Parameters mParameters = mCamera.getParameters();
mParameters.setFocusMode(mParameter);
mCamera.setParameters(mParameters);
}
And then call this method in surfaceChanged()
like this:
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
...//your code here
// Set focus mode to continuous picture
setFocus(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
// Start camera preview
mCamera.startPreview();
}
You can choose between these focus parameters
:
String FOCUS_MODE_AUTO Auto-focus mode.
String FOCUS_MODE_CONTINUOUS_PICTURE Continuous auto focus mode intended for taking pictures.
String FOCUS_MODE_CONTINUOUS_VIDEO Continuous auto focus mode intended for video recording.
String FOCUS_MODE_EDOF Extended depth of field (EDOF).
String FOCUS_MODE_FIXED Focus is fixed.
String FOCUS_MODE_INFINITY Focus is set at infinity.
String FOCUS_MODE_MACRO Macro (close-up) focus mode.
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