Skip to content

Instantly share code, notes, and snippets.

@suplo
Forked from gregkorossy/VisionApiFocusFix.java
Last active January 29, 2019 04:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suplo/20ceac9df234c3e8bba5 to your computer and use it in GitHub Desktop.
Save suplo/20ceac9df234c3e8bba5 to your computer and use it in GitHub Desktop.
Mobile Vision API fix for missing autofocus feature
/*
* IF YOU WANT TO JUST ACCESS THE CAMERA INSTANCE SO THAT YOU CAN SET ANY OF THE PARAMETERS, VISIT THE FOLLOWING LINK:
* https://gist.github.com/Gericop/364dd12b105fdc28a0b6
*/
/**
* <p>
* Sets the Mobile Vision API provided {@link com.google.android.gms.vision.CameraSource}'s
* focus mode. Use {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE} or
* {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} for continuous autofocus.
* </p>
* <p>
* Note that the CameraSource's {@link CameraSource#start()} or
* {@link CameraSource#start(SurfaceHolder)} has to be called and the camera image has to be
* showing prior using this method as the CameraSource only creates the camera after calling
* one of those methods and the camera is not available immediately. You could implement some
* kind of a callback method for the SurfaceHolder that notifies you when the imaging is ready
* or use a direct action (e.g. button press) to set the focus mode.
* </p>
* <p>
* Check out <a href="https://github.com/googlesamples/android-vision/blob/master/face/multi-tracker/app/src/main/java/com/google/android/gms/samples/vision/face/multitracker/ui/camera/CameraSourcePreview.java#L84">CameraSourcePreview.java</a>
* which contains the method <code>startIfReady()</code> that has the following line:
* <blockquote><code>mCameraSource.start(mSurfaceView.getHolder());</code></blockquote><br>
* After this call you can use our <code>cameraFocus(...)</code> method because the camera is ready.
* </p>
*
* @param cameraSource The CameraSource built with {@link com.google.android.gms.vision.CameraSource.Builder}.
* @param focusMode The focus mode. See {@link android.hardware.Camera.Parameters} for possible values.
* @return true if the camera's focus is set; false otherwise.
* @see com.google.android.gms.vision.CameraSource
* @see android.hardware.Camera.Parameters
*/
public static boolean cameraFocus(@NonNull CameraSource cameraSource, @NonNull String focusMode) {
Field[] declaredFields = CameraSource.class.getDeclaredFields();
for (Field field : declaredFields) {
if (field.getType() == Camera.class) {
field.setAccessible(true);
try {
Camera camera = (Camera) field.get(cameraSource);
if (camera != null) {
Camera.Parameters params = camera.getParameters();
params.setFocusMode(focusMode);
camera.setParameters(params);
return true;
}
return false;
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment