|
| 1 | +package com.gnzlt.AndroidVisionQRReader.camera; |
| 2 | + |
| 3 | +import android.hardware.Camera; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.support.annotation.StringDef; |
| 6 | + |
| 7 | +import com.google.android.gms.vision.CameraSource; |
| 8 | + |
| 9 | +import java.lang.annotation.Retention; |
| 10 | +import java.lang.annotation.RetentionPolicy; |
| 11 | +import java.lang.reflect.Field; |
| 12 | + |
| 13 | +public class VisionApiCameraFix { |
| 14 | + /* |
| 15 | + * IF YOU WANT TO JUST ACCESS THE CAMERA INSTANCE SO THAT YOU CAN SET ANY OF THE PARAMETERS, VISIT THE FOLLOWING LINK: |
| 16 | + * https://gist.github.com/Gericop/364dd12b105fdc28a0b6 |
| 17 | + */ |
| 18 | + |
| 19 | + /** |
| 20 | + * Custom annotation to allow only valid focus modes. |
| 21 | + */ |
| 22 | + @StringDef({ |
| 23 | + Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE, |
| 24 | + Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO, |
| 25 | + Camera.Parameters.FOCUS_MODE_AUTO, |
| 26 | + Camera.Parameters.FOCUS_MODE_EDOF, |
| 27 | + Camera.Parameters.FOCUS_MODE_FIXED, |
| 28 | + Camera.Parameters.FOCUS_MODE_INFINITY, |
| 29 | + Camera.Parameters.FOCUS_MODE_MACRO |
| 30 | + }) |
| 31 | + @Retention(RetentionPolicy.SOURCE) |
| 32 | + private @interface FocusMode { |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * <p> |
| 37 | + * Sets the Mobile Vision API provided {@link com.google.android.gms.vision.CameraSource}'s |
| 38 | + * focus mode. Use {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_PICTURE} or |
| 39 | + * {@link Camera.Parameters#FOCUS_MODE_CONTINUOUS_VIDEO} for continuous autofocus. |
| 40 | + * </p> |
| 41 | + * <p> |
| 42 | + * Note that the CameraSource's {@link CameraSource#start()} or |
| 43 | + * {@link CameraSource#start(SurfaceHolder)} has to be called and the camera image has to be |
| 44 | + * showing prior using this method as the CameraSource only creates the camera after calling |
| 45 | + * one of those methods and the camera is not available immediately. You could implement some |
| 46 | + * kind of a callback method for the SurfaceHolder that notifies you when the imaging is ready |
| 47 | + * or use a direct action (e.g. button press) to set the focus mode. |
| 48 | + * </p> |
| 49 | + * <p> |
| 50 | + * 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> |
| 51 | + * which contains the method <code>startIfReady()</code> that has the following line: |
| 52 | + * <blockquote><code>mCameraSource.start(mSurfaceView.getHolder());</code></blockquote><br> |
| 53 | + * After this call you can use our <code>cameraFocus(...)</code> method because the camera is ready. |
| 54 | + * </p> |
| 55 | + * |
| 56 | + * @param cameraSource The CameraSource built with {@link com.google.android.gms.vision.CameraSource.Builder}. |
| 57 | + * @param focusMode The focus mode. See {@link android.hardware.Camera.Parameters} for possible values. |
| 58 | + * @return true if the camera's focus is set; false otherwise. |
| 59 | + * @see com.google.android.gms.vision.CameraSource |
| 60 | + * @see android.hardware.Camera.Parameters |
| 61 | + */ |
| 62 | + public static boolean cameraFocus(@NonNull CameraSource cameraSource, @FocusMode @NonNull String focusMode) { |
| 63 | + Field[] declaredFields = CameraSource.class.getDeclaredFields(); |
| 64 | + |
| 65 | + for (Field field : declaredFields) { |
| 66 | + if (field.getType() == Camera.class) { |
| 67 | + field.setAccessible(true); |
| 68 | + try { |
| 69 | + Camera camera = (Camera) field.get(cameraSource); |
| 70 | + if (camera != null) { |
| 71 | + Camera.Parameters params = camera.getParameters(); |
| 72 | + |
| 73 | + if (!params.getSupportedFocusModes().contains(focusMode)) { |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + params.setFocusMode(focusMode); |
| 78 | + camera.setParameters(params); |
| 79 | + return true; |
| 80 | + } |
| 81 | + |
| 82 | + return false; |
| 83 | + } catch (IllegalAccessException e) { |
| 84 | + e.printStackTrace(); |
| 85 | + } |
| 86 | + |
| 87 | + break; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return false; |
| 92 | + } |
| 93 | +} |
0 commit comments