26
26
import android .content .IntentFilter ;
27
27
import android .content .pm .PackageManager ;
28
28
import android .hardware .Camera ;
29
- import android .os .Build ;
30
29
import android .os .Bundle ;
31
30
import android .speech .tts .TextToSpeech ;
32
31
import android .support .annotation .NonNull ;
42
41
43
42
import com .google .android .gms .common .ConnectionResult ;
44
43
import com .google .android .gms .common .GoogleApiAvailability ;
45
- import com .google .android .gms .common .api .CommonStatusCodes ;
46
44
import com .google .android .gms .samples .vision .ocrreader .ui .camera .CameraSource ;
47
45
import com .google .android .gms .samples .vision .ocrreader .ui .camera .CameraSourcePreview ;
48
46
import com .google .android .gms .samples .vision .ocrreader .ui .camera .GraphicOverlay ;
@@ -71,9 +69,9 @@ public final class OcrCaptureActivity extends AppCompatActivity {
71
69
public static final String UseFlash = "UseFlash" ;
72
70
public static final String TextBlockObject = "String" ;
73
71
74
- private CameraSource mCameraSource ;
75
- private CameraSourcePreview mPreview ;
76
- private GraphicOverlay <OcrGraphic > mGraphicOverlay ;
72
+ private CameraSource cameraSource ;
73
+ private CameraSourcePreview preview ;
74
+ private GraphicOverlay <OcrGraphic > graphicOverlay ;
77
75
78
76
// Helper objects for detecting taps and pinches.
79
77
private ScaleGestureDetector scaleGestureDetector ;
@@ -90,8 +88,8 @@ public void onCreate(Bundle bundle) {
90
88
super .onCreate (bundle );
91
89
setContentView (R .layout .ocr_capture );
92
90
93
- mPreview = (CameraSourcePreview ) findViewById (R .id .preview );
94
- mGraphicOverlay = (GraphicOverlay <OcrGraphic >) findViewById (R .id .graphicOverlay );
91
+ preview = (CameraSourcePreview ) findViewById (R .id .preview );
92
+ graphicOverlay = (GraphicOverlay <OcrGraphic >) findViewById (R .id .graphicOverlay );
95
93
96
94
// Set good defaults for capturing text.
97
95
boolean autoFocus = true ;
@@ -109,7 +107,7 @@ public void onCreate(Bundle bundle) {
109
107
gestureDetector = new GestureDetector (this , new CaptureGestureListener ());
110
108
scaleGestureDetector = new ScaleGestureDetector (this , new ScaleListener ());
111
109
112
- Snackbar .make (mGraphicOverlay , "Tap to Speak. Pinch/Stretch to zoom" ,
110
+ Snackbar .make (graphicOverlay , "Tap to Speak. Pinch/Stretch to zoom" ,
113
111
Snackbar .LENGTH_LONG )
114
112
.show ();
115
113
@@ -155,7 +153,7 @@ public void onClick(View view) {
155
153
}
156
154
};
157
155
158
- Snackbar .make (mGraphicOverlay , R .string .permission_camera_rationale ,
156
+ Snackbar .make (graphicOverlay , R .string .permission_camera_rationale ,
159
157
Snackbar .LENGTH_INDEFINITE )
160
158
.setAction (R .string .ok , listener )
161
159
.show ();
@@ -187,7 +185,7 @@ private void createCameraSource(boolean autoFocus, boolean useFlash) {
187
185
// graphics for each text block on screen. The factory is used by the multi-processor to
188
186
// create a separate tracker instance for each text block.
189
187
TextRecognizer textRecognizer = new TextRecognizer .Builder (context ).build ();
190
- textRecognizer .setProcessor (new OcrDetectorProcessor (mGraphicOverlay ));
188
+ textRecognizer .setProcessor (new OcrDetectorProcessor (graphicOverlay ));
191
189
192
190
if (!textRecognizer .isOperational ()) {
193
191
// Note: The first time that an app using a Vision API is installed on a
@@ -214,7 +212,7 @@ private void createCameraSource(boolean autoFocus, boolean useFlash) {
214
212
215
213
// Creates and starts the camera. Note that this uses a higher resolution in comparison
216
214
// to other detection examples to enable the text recognizer to detect small pieces of text.
217
- mCameraSource =
215
+ cameraSource =
218
216
new CameraSource .Builder (getApplicationContext (), textRecognizer )
219
217
.setFacing (CameraSource .CAMERA_FACING_BACK )
220
218
.setRequestedPreviewSize (1280 , 1024 )
@@ -239,8 +237,8 @@ protected void onResume() {
239
237
@ Override
240
238
protected void onPause () {
241
239
super .onPause ();
242
- if (mPreview != null ) {
243
- mPreview .stop ();
240
+ if (preview != null ) {
241
+ preview .stop ();
244
242
}
245
243
}
246
244
@@ -251,8 +249,8 @@ protected void onPause() {
251
249
@ Override
252
250
protected void onDestroy () {
253
251
super .onDestroy ();
254
- if (mPreview != null ) {
255
- mPreview .release ();
252
+ if (preview != null ) {
253
+ preview .release ();
256
254
}
257
255
}
258
256
@@ -285,7 +283,7 @@ public void onRequestPermissionsResult(int requestCode,
285
283
if (grantResults .length != 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
286
284
Log .d (TAG , "Camera permission granted - initialize the camera source" );
287
285
// we have permission, so create the camerasource
288
- boolean autoFocus = getIntent ().getBooleanExtra (AutoFocus ,false );
286
+ boolean autoFocus = getIntent ().getBooleanExtra (AutoFocus ,true );
289
287
boolean useFlash = getIntent ().getBooleanExtra (UseFlash , false );
290
288
createCameraSource (autoFocus , useFlash );
291
289
return ;
@@ -322,13 +320,13 @@ private void startCameraSource() throws SecurityException {
322
320
dlg .show ();
323
321
}
324
322
325
- if (mCameraSource != null ) {
323
+ if (cameraSource != null ) {
326
324
try {
327
- mPreview .start (mCameraSource , mGraphicOverlay );
325
+ preview .start (cameraSource , graphicOverlay );
328
326
} catch (IOException e ) {
329
327
Log .e (TAG , "Unable to start camera source." , e );
330
- mCameraSource .release ();
331
- mCameraSource = null ;
328
+ cameraSource .release ();
329
+ cameraSource = null ;
332
330
}
333
331
}
334
332
}
@@ -341,7 +339,7 @@ private void startCameraSource() throws SecurityException {
341
339
* @return true if the tap was on a TextBlock
342
340
*/
343
341
private boolean onTap (float rawX , float rawY ) {
344
- OcrGraphic graphic = mGraphicOverlay .getGraphicAtLocation (rawX , rawY );
342
+ OcrGraphic graphic = graphicOverlay .getGraphicAtLocation (rawX , rawY );
345
343
TextBlock text = null ;
346
344
if (graphic != null ) {
347
345
text = graphic .getTextBlock ();
@@ -418,8 +416,8 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
418
416
*/
419
417
@ Override
420
418
public void onScaleEnd (ScaleGestureDetector detector ) {
421
- if (mCameraSource != null ) {
422
- mCameraSource .doZoom (detector .getScaleFactor ());
419
+ if (cameraSource != null ) {
420
+ cameraSource .doZoom (detector .getScaleFactor ());
423
421
}
424
422
}
425
423
}
0 commit comments