OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.shape_detection; | 5 package org.chromium.shape_detection; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.graphics.Rect; | 8 import android.graphics.Rect; |
9 import android.util.SparseArray; | 9 import android.util.SparseArray; |
10 | 10 |
11 import com.google.android.gms.common.ConnectionResult; | 11 import com.google.android.gms.common.ConnectionResult; |
12 import com.google.android.gms.common.GoogleApiAvailability; | 12 import com.google.android.gms.common.GoogleApiAvailability; |
13 import com.google.android.gms.vision.Frame; | 13 import com.google.android.gms.vision.Frame; |
14 import com.google.android.gms.vision.text.TextBlock; | 14 import com.google.android.gms.vision.text.TextBlock; |
15 import com.google.android.gms.vision.text.TextRecognizer; | 15 import com.google.android.gms.vision.text.TextRecognizer; |
16 | 16 |
17 import org.chromium.base.Log; | 17 import org.chromium.base.Log; |
18 import org.chromium.gfx.mojom.RectF; | 18 import org.chromium.gfx.mojom.RectF; |
19 import org.chromium.mojo.system.MojoException; | 19 import org.chromium.mojo.system.MojoException; |
20 import org.chromium.mojo.system.SharedBufferHandle; | |
21 import org.chromium.services.service_manager.InterfaceFactory; | 20 import org.chromium.services.service_manager.InterfaceFactory; |
22 import org.chromium.shape_detection.mojom.TextDetection; | 21 import org.chromium.shape_detection.mojom.TextDetection; |
23 import org.chromium.shape_detection.mojom.TextDetectionResult; | 22 import org.chromium.shape_detection.mojom.TextDetectionResult; |
24 | 23 |
25 | 24 |
26 /** | 25 /** |
27 * Implementation of mojo TextDetection, using Google Play Services vision packa
ge. | 26 * Implementation of mojo TextDetection, using Google Play Services vision packa
ge. |
28 */ | 27 */ |
29 public class TextDetectionImpl implements TextDetection { | 28 public class TextDetectionImpl implements TextDetection { |
30 private static final String TAG = "TextDetectionImpl"; | 29 private static final String TAG = "TextDetectionImpl"; |
31 | 30 |
32 private final Context mContext; | 31 private final Context mContext; |
33 private TextRecognizer mTextRecognizer; | 32 private TextRecognizer mTextRecognizer; |
34 | 33 |
35 public TextDetectionImpl(Context context) { | 34 public TextDetectionImpl(Context context) { |
36 mContext = context; | 35 mContext = context; |
37 mTextRecognizer = new TextRecognizer.Builder(mContext).build(); | 36 mTextRecognizer = new TextRecognizer.Builder(mContext).build(); |
38 } | 37 } |
39 | 38 |
40 @Override | 39 @Override |
41 public void detect( | 40 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { |
42 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { | |
43 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mC
ontext) | 41 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mC
ontext) |
44 != ConnectionResult.SUCCESS) { | 42 != ConnectionResult.SUCCESS) { |
45 Log.e(TAG, "Google Play Services not available"); | 43 Log.e(TAG, "Google Play Services not available"); |
46 callback.call(new TextDetectionResult[0]); | 44 callback.call(new TextDetectionResult[0]); |
47 return; | 45 return; |
48 } | 46 } |
49 // The vision library will be downloaded the first time the API is used | 47 // The vision library will be downloaded the first time the API is used |
50 // on the device; this happens "fast", but it might have not completed, | 48 // on the device; this happens "fast", but it might have not completed, |
51 // bail in this case. Also, the API was disabled between and v.9.0 and | 49 // bail in this case. Also, the API was disabled between and v.9.0 and |
52 // v.9.2, see https://developers.google.com/android/guides/releases. | 50 // v.9.2, see https://developers.google.com/android/guides/releases. |
53 if (!mTextRecognizer.isOperational()) { | 51 if (!mTextRecognizer.isOperational()) { |
54 Log.e(TAG, "TextDetector is not operational"); | 52 Log.e(TAG, "TextDetector is not operational"); |
55 callback.call(new TextDetectionResult[0]); | 53 callback.call(new TextDetectionResult[0]); |
56 return; | 54 return; |
57 } | 55 } |
58 | 56 |
59 Frame frame = SharedBufferUtils.convertToFrame(frameData, width, height)
; | 57 Frame frame = SharedBufferUtils.convertToFrame(bitmapData); |
60 if (frame == null) { | 58 if (frame == null) { |
61 Log.e(TAG, "Error converting SharedMemory to Frame"); | 59 Log.e(TAG, "Error converting SharedMemory to Frame"); |
62 callback.call(new TextDetectionResult[0]); | 60 callback.call(new TextDetectionResult[0]); |
63 return; | 61 return; |
64 } | 62 } |
65 | 63 |
66 final SparseArray<TextBlock> textBlocks = mTextRecognizer.detect(frame); | 64 final SparseArray<TextBlock> textBlocks = mTextRecognizer.detect(frame); |
67 | 65 |
68 TextDetectionResult[] detectedTextArray = new TextDetectionResult[textBl
ocks.size()]; | 66 TextDetectionResult[] detectedTextArray = new TextDetectionResult[textBl
ocks.size()]; |
69 for (int i = 0; i < textBlocks.size(); i++) { | 67 for (int i = 0; i < textBlocks.size(); i++) { |
(...skipping 28 matching lines...) Expand all Loading... |
98 public Factory(Context context) { | 96 public Factory(Context context) { |
99 mContext = context; | 97 mContext = context; |
100 } | 98 } |
101 | 99 |
102 @Override | 100 @Override |
103 public TextDetection createImpl() { | 101 public TextDetection createImpl() { |
104 return new TextDetectionImpl(mContext); | 102 return new TextDetectionImpl(mContext); |
105 } | 103 } |
106 } | 104 } |
107 } | 105 } |
OLD | NEW |