Skip to content

Commit 9a98adc

Browse files
committed
Added autofocus feature
Updated with Mobile Vision API fix for missing autofocus feature. Thanks to @Gericop fix https://gist.github.com/Gericop/7de0b9fdd7a444e53b5a
1 parent 0a8e076 commit 9a98adc

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

AndroidVisionQRReader/src/main/java/com/gnzlt/AndroidVisionQRReader/camera/CameraSourcePreview.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import android.content.Context;
1919
import android.content.res.Configuration;
20+
import android.hardware.Camera;
2021
import android.util.AttributeSet;
2122
import android.util.Log;
2223
import android.view.SurfaceHolder;
@@ -78,6 +79,7 @@ public void release() {
7879
private void startIfReady() throws IOException {
7980
if (mStartRequested && mSurfaceAvailable) {
8081
mCameraSource.start(mSurfaceView.getHolder());
82+
VisionApiCameraFix.cameraFocus(mCameraSource, Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
8183
mStartRequested = false;
8284
}
8385
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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

Comments
 (0)