Android autofocus does not work after creating camera view

I'm trying to create my own Camera View, everything works for me, except for autofocus, I can’t understand why this will not 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" /> 
+5
source share
3 answers

if you added <uses-feature android:name="android.hardware.camera.autofocus" /> to your manifest, this does not mean that the camera will make autofocus . This means that you grant your application permission to use the camera hardware or software that focuses on autofocus.

The purpose of the announcement is to inform any external object about the set of hardware and software functions on which your application depends.

To set the camera to focus, you can add this method to the 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() as follows:

 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 for shooting.

String FOCUS_MODE_CONTINUOUS_VIDEO Continuous autofocus mode for video recording.

String FOCUS_MODE_EDOF Extended Depth of Field (EDOF).

String FOCUS_MODE_FIXED Focus fixed.

String FOCUS_MODE_INFINITY The focus is set to infinity.

String FOCUS_MODE_MACRO Macro mode (macro).

+8
source
 //set camera to continually auto-focus Camera.Parameters params = c.getParameters(); //*EDIT*//params.setFocusMode("continuous-picture"); //It is better to use defined constraints as opposed to String, thanks to AbdelHady params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); c.setParameters(params); 
+5
source

Here are a few options:

  • Arsalank2 recommends using “continuous autofocus” as described in this answer . However, some HTC devices do not seem to support this. Check with: parameters.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTIN‌​UOUS_VIDEO)

  • You can implement the onSensorChanged and focus with a callback when certain criteria are met, see this answer by Juan Acevedo .

  • Handle each case differently to support the widest range of devices. Check that it works with different models of different devices, as you cannot completely rely on what the API level says.

I would recommend switching to option 3, since there seems to be no method that works for every single device.

+1
source

Source: https://habr.com/ru/post/1239339/


All Articles