2626import android .content .IntentFilter ;
2727import android .content .pm .PackageManager ;
2828import android .hardware .Camera ;
29- import android .os .Build ;
3029import android .os .Bundle ;
3130import android .speech .tts .TextToSpeech ;
3231import android .support .annotation .NonNull ;
4241
4342import com .google .android .gms .common .ConnectionResult ;
4443import com .google .android .gms .common .GoogleApiAvailability ;
45- import com .google .android .gms .common .api .CommonStatusCodes ;
4644import com .google .android .gms .samples .vision .ocrreader .ui .camera .CameraSource ;
4745import com .google .android .gms .samples .vision .ocrreader .ui .camera .CameraSourcePreview ;
4846import com .google .android .gms .samples .vision .ocrreader .ui .camera .GraphicOverlay ;
@@ -71,9 +69,9 @@ public final class OcrCaptureActivity extends AppCompatActivity {
7169 public static final String UseFlash = "UseFlash" ;
7270 public static final String TextBlockObject = "String" ;
7371
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 ;
7775
7876 // Helper objects for detecting taps and pinches.
7977 private ScaleGestureDetector scaleGestureDetector ;
@@ -90,8 +88,8 @@ public void onCreate(Bundle bundle) {
9088 super .onCreate (bundle );
9189 setContentView (R .layout .ocr_capture );
9290
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 );
9593
9694 // Set good defaults for capturing text.
9795 boolean autoFocus = true ;
@@ -109,7 +107,7 @@ public void onCreate(Bundle bundle) {
109107 gestureDetector = new GestureDetector (this , new CaptureGestureListener ());
110108 scaleGestureDetector = new ScaleGestureDetector (this , new ScaleListener ());
111109
112- Snackbar .make (mGraphicOverlay , "Tap to Speak. Pinch/Stretch to zoom" ,
110+ Snackbar .make (graphicOverlay , "Tap to Speak. Pinch/Stretch to zoom" ,
113111 Snackbar .LENGTH_LONG )
114112 .show ();
115113
@@ -155,7 +153,7 @@ public void onClick(View view) {
155153 }
156154 };
157155
158- Snackbar .make (mGraphicOverlay , R .string .permission_camera_rationale ,
156+ Snackbar .make (graphicOverlay , R .string .permission_camera_rationale ,
159157 Snackbar .LENGTH_INDEFINITE )
160158 .setAction (R .string .ok , listener )
161159 .show ();
@@ -187,7 +185,7 @@ private void createCameraSource(boolean autoFocus, boolean useFlash) {
187185 // graphics for each text block on screen. The factory is used by the multi-processor to
188186 // create a separate tracker instance for each text block.
189187 TextRecognizer textRecognizer = new TextRecognizer .Builder (context ).build ();
190- textRecognizer .setProcessor (new OcrDetectorProcessor (mGraphicOverlay ));
188+ textRecognizer .setProcessor (new OcrDetectorProcessor (graphicOverlay ));
191189
192190 if (!textRecognizer .isOperational ()) {
193191 // 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) {
214212
215213 // Creates and starts the camera. Note that this uses a higher resolution in comparison
216214 // to other detection examples to enable the text recognizer to detect small pieces of text.
217- mCameraSource =
215+ cameraSource =
218216 new CameraSource .Builder (getApplicationContext (), textRecognizer )
219217 .setFacing (CameraSource .CAMERA_FACING_BACK )
220218 .setRequestedPreviewSize (1280 , 1024 )
@@ -239,8 +237,8 @@ protected void onResume() {
239237 @ Override
240238 protected void onPause () {
241239 super .onPause ();
242- if (mPreview != null ) {
243- mPreview .stop ();
240+ if (preview != null ) {
241+ preview .stop ();
244242 }
245243 }
246244
@@ -251,8 +249,8 @@ protected void onPause() {
251249 @ Override
252250 protected void onDestroy () {
253251 super .onDestroy ();
254- if (mPreview != null ) {
255- mPreview .release ();
252+ if (preview != null ) {
253+ preview .release ();
256254 }
257255 }
258256
@@ -285,7 +283,7 @@ public void onRequestPermissionsResult(int requestCode,
285283 if (grantResults .length != 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ) {
286284 Log .d (TAG , "Camera permission granted - initialize the camera source" );
287285 // we have permission, so create the camerasource
288- boolean autoFocus = getIntent ().getBooleanExtra (AutoFocus ,false );
286+ boolean autoFocus = getIntent ().getBooleanExtra (AutoFocus ,true );
289287 boolean useFlash = getIntent ().getBooleanExtra (UseFlash , false );
290288 createCameraSource (autoFocus , useFlash );
291289 return ;
@@ -322,13 +320,13 @@ private void startCameraSource() throws SecurityException {
322320 dlg .show ();
323321 }
324322
325- if (mCameraSource != null ) {
323+ if (cameraSource != null ) {
326324 try {
327- mPreview .start (mCameraSource , mGraphicOverlay );
325+ preview .start (cameraSource , graphicOverlay );
328326 } catch (IOException e ) {
329327 Log .e (TAG , "Unable to start camera source." , e );
330- mCameraSource .release ();
331- mCameraSource = null ;
328+ cameraSource .release ();
329+ cameraSource = null ;
332330 }
333331 }
334332 }
@@ -341,7 +339,7 @@ private void startCameraSource() throws SecurityException {
341339 * @return true if the tap was on a TextBlock
342340 */
343341 private boolean onTap (float rawX , float rawY ) {
344- OcrGraphic graphic = mGraphicOverlay .getGraphicAtLocation (rawX , rawY );
342+ OcrGraphic graphic = graphicOverlay .getGraphicAtLocation (rawX , rawY );
345343 TextBlock text = null ;
346344 if (graphic != null ) {
347345 text = graphic .getTextBlock ();
@@ -418,8 +416,8 @@ public boolean onScaleBegin(ScaleGestureDetector detector) {
418416 */
419417 @ Override
420418 public void onScaleEnd (ScaleGestureDetector detector ) {
421- if (mCameraSource != null ) {
422- mCameraSource .doZoom (detector .getScaleFactor ());
419+ if (cameraSource != null ) {
420+ cameraSource .doZoom (detector .getScaleFactor ());
423421 }
424422 }
425423 }
0 commit comments