Skip to content

Commit 0c2a85a

Browse files
author
Ben Johns
committed
Minor updates for IO 2018. Happy Codelabbing, all.
1 parent 2ce3132 commit 0c2a85a

File tree

18 files changed

+522
-509
lines changed

18 files changed

+522
-509
lines changed
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion '25.0.0'
4+
compileSdkVersion 27
5+
buildToolsVersion '27.0.3'
66

77
defaultConfig {
8-
applicationId "com.google.android.gms.samples.vision.barcodereader"
8+
applicationId "com.google.android.gms.samples.vision.ocrreader"
99
minSdkVersion 21
10-
targetSdkVersion 23
10+
targetSdkVersion 27
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -20,8 +20,8 @@ android {
2020
}
2121

2222
dependencies {
23-
compile fileTree(dir: 'libs', include: ['*.jar'])
24-
compile 'com.android.support:support-v4:24.2.0'
25-
compile 'com.google.android.gms:play-services-vision:9.4.0+'
26-
compile 'com.android.support:design:24.2.0'
23+
implementation fileTree(dir: 'libs', include: ['*.jar'])
24+
implementation 'com.android.support:support-v4:27.1.1'
25+
implementation 'com.android.support:design:27.1.1'
26+
implementation 'com.google.android.gms:play-services-vision:15.0.0'
2727
}

visionSamples/ocr-codelab/ocr-reader-complete/app/src/main/java/com/google/android/gms/samples/vision/ocrreader/OcrCaptureActivity.java

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import android.content.IntentFilter;
2727
import android.content.pm.PackageManager;
2828
import android.hardware.Camera;
29-
import android.os.Build;
3029
import android.os.Bundle;
3130
import android.speech.tts.TextToSpeech;
3231
import android.support.annotation.NonNull;
@@ -42,7 +41,6 @@
4241

4342
import com.google.android.gms.common.ConnectionResult;
4443
import com.google.android.gms.common.GoogleApiAvailability;
45-
import com.google.android.gms.common.api.CommonStatusCodes;
4644
import com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSource;
4745
import com.google.android.gms.samples.vision.ocrreader.ui.camera.CameraSourcePreview;
4846
import 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
}

visionSamples/ocr-codelab/ocr-reader-complete/app/src/main/java/com/google/android/gms/samples/vision/ocrreader/OcrDetectorProcessor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*/
2929
public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
3030

31-
private GraphicOverlay<OcrGraphic> mGraphicOverlay;
31+
private GraphicOverlay<OcrGraphic> graphicOverlay;
3232

3333
OcrDetectorProcessor(GraphicOverlay<OcrGraphic> ocrGraphicOverlay) {
34-
mGraphicOverlay = ocrGraphicOverlay;
34+
graphicOverlay = ocrGraphicOverlay;
3535
}
3636

3737
/**
@@ -43,15 +43,15 @@ public class OcrDetectorProcessor implements Detector.Processor<TextBlock> {
4343
*/
4444
@Override
4545
public void receiveDetections(Detector.Detections<TextBlock> detections) {
46-
mGraphicOverlay.clear();
46+
graphicOverlay.clear();
4747
SparseArray<TextBlock> items = detections.getDetectedItems();
4848
for (int i = 0; i < items.size(); ++i) {
4949
TextBlock item = items.valueAt(i);
5050
if (item != null && item.getValue() != null) {
5151
Log.d("OcrDetectorProcessor", "Text detected! " + item.getValue());
52+
OcrGraphic graphic = new OcrGraphic(graphicOverlay, item);
53+
graphicOverlay.add(graphic);
5254
}
53-
OcrGraphic graphic = new OcrGraphic(mGraphicOverlay, item);
54-
mGraphicOverlay.add(graphic);
5555
}
5656
}
5757

@@ -60,6 +60,6 @@ public void receiveDetections(Detector.Detections<TextBlock> detections) {
6060
*/
6161
@Override
6262
public void release() {
63-
mGraphicOverlay.clear();
63+
graphicOverlay.clear();
6464
}
6565
}

visionSamples/ocr-codelab/ocr-reader-complete/app/src/main/java/com/google/android/gms/samples/vision/ocrreader/OcrGraphic.java

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,45 +32,45 @@
3232
*/
3333
public class OcrGraphic extends GraphicOverlay.Graphic {
3434

35-
private int mId;
35+
private int id;
3636

3737
private static final int TEXT_COLOR = Color.WHITE;
3838

39-
private static Paint sRectPaint;
40-
private static Paint sTextPaint;
41-
private final TextBlock mText;
39+
private static Paint rectPaint;
40+
private static Paint textPaint;
41+
private final TextBlock textBlock;
4242

4343
OcrGraphic(GraphicOverlay overlay, TextBlock text) {
4444
super(overlay);
4545

46-
mText = text;
46+
textBlock = text;
4747

48-
if (sRectPaint == null) {
49-
sRectPaint = new Paint();
50-
sRectPaint.setColor(TEXT_COLOR);
51-
sRectPaint.setStyle(Paint.Style.STROKE);
52-
sRectPaint.setStrokeWidth(4.0f);
48+
if (rectPaint == null) {
49+
rectPaint = new Paint();
50+
rectPaint.setColor(TEXT_COLOR);
51+
rectPaint.setStyle(Paint.Style.STROKE);
52+
rectPaint.setStrokeWidth(4.0f);
5353
}
5454

55-
if (sTextPaint == null) {
56-
sTextPaint = new Paint();
57-
sTextPaint.setColor(TEXT_COLOR);
58-
sTextPaint.setTextSize(54.0f);
55+
if (textPaint == null) {
56+
textPaint = new Paint();
57+
textPaint.setColor(TEXT_COLOR);
58+
textPaint.setTextSize(54.0f);
5959
}
6060
// Redraw the overlay, as this graphic has been added.
6161
postInvalidate();
6262
}
6363

6464
public int getId() {
65-
return mId;
65+
return id;
6666
}
6767

6868
public void setId(int id) {
69-
this.mId = id;
69+
this.id = id;
7070
}
7171

7272
public TextBlock getTextBlock() {
73-
return mText;
73+
return textBlock;
7474
}
7575

7676
/**
@@ -81,14 +81,11 @@ public TextBlock getTextBlock() {
8181
* @return True if the provided point is contained within this graphic's bounding box.
8282
*/
8383
public boolean contains(float x, float y) {
84-
if (mText == null) {
84+
if (textBlock == null) {
8585
return false;
8686
}
87-
RectF rect = new RectF(mText.getBoundingBox());
88-
rect.left = translateX(rect.left);
89-
rect.top = translateY(rect.top);
90-
rect.right = translateX(rect.right);
91-
rect.bottom = translateY(rect.bottom);
87+
RectF rect = new RectF(textBlock.getBoundingBox());
88+
rect = translateRect(rect);
9289
return (rect.left < x && rect.right > x && rect.top < y && rect.bottom > y);
9390
}
9491

@@ -97,24 +94,21 @@ public boolean contains(float x, float y) {
9794
*/
9895
@Override
9996
public void draw(Canvas canvas) {
100-
if (mText == null) {
97+
if (textBlock == null) {
10198
return;
10299
}
103100

104101
// Draws the bounding box around the TextBlock.
105-
RectF rect = new RectF(mText.getBoundingBox());
106-
rect.left = translateX(rect.left);
107-
rect.top = translateY(rect.top);
108-
rect.right = translateX(rect.right);
109-
rect.bottom = translateY(rect.bottom);
110-
canvas.drawRect(rect, sRectPaint);
102+
RectF rect = new RectF(textBlock.getBoundingBox());
103+
rect = translateRect(rect);
104+
canvas.drawRect(rect, rectPaint);
111105

112106
// Break the text into multiple lines and draw each one according to its own bounding box.
113-
List<? extends Text> textComponents = mText.getComponents();
107+
List<? extends Text> textComponents = textBlock.getComponents();
114108
for(Text currentText : textComponents) {
115109
float left = translateX(currentText.getBoundingBox().left);
116110
float bottom = translateY(currentText.getBoundingBox().bottom);
117-
canvas.drawText(currentText.getValue(), left, bottom, sTextPaint);
111+
canvas.drawText(currentText.getValue(), left, bottom, textPaint);
118112
}
119113
}
120114
}

0 commit comments

Comments
 (0)