OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.Point; | 8 import android.graphics.Point; |
9 import android.graphics.Rect; | 9 import android.graphics.Rect; |
10 import android.util.SparseArray; | 10 import android.util.SparseArray; |
11 | 11 |
12 import com.google.android.gms.common.ConnectionResult; | 12 import com.google.android.gms.common.ConnectionResult; |
13 import com.google.android.gms.common.GoogleApiAvailability; | 13 import com.google.android.gms.common.GoogleApiAvailability; |
14 import com.google.android.gms.vision.Frame; | 14 import com.google.android.gms.vision.Frame; |
15 import com.google.android.gms.vision.barcode.Barcode; | 15 import com.google.android.gms.vision.barcode.Barcode; |
16 import com.google.android.gms.vision.barcode.BarcodeDetector; | 16 import com.google.android.gms.vision.barcode.BarcodeDetector; |
17 | 17 |
18 import org.chromium.base.Log; | 18 import org.chromium.base.Log; |
19 import org.chromium.gfx.mojom.PointF; | 19 import org.chromium.gfx.mojom.PointF; |
20 import org.chromium.gfx.mojom.RectF; | 20 import org.chromium.gfx.mojom.RectF; |
21 import org.chromium.mojo.system.MojoException; | 21 import org.chromium.mojo.system.MojoException; |
22 import org.chromium.mojo.system.SharedBufferHandle; | |
23 import org.chromium.services.service_manager.InterfaceFactory; | 22 import org.chromium.services.service_manager.InterfaceFactory; |
24 import org.chromium.shape_detection.mojom.BarcodeDetection; | 23 import org.chromium.shape_detection.mojom.BarcodeDetection; |
25 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; | 24 import org.chromium.shape_detection.mojom.BarcodeDetectionResult; |
26 | 25 |
27 /** | 26 /** |
28 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. | 27 * Implementation of mojo BarcodeDetection, using Google Play Services vision pa
ckage. |
29 */ | 28 */ |
30 public class BarcodeDetectionImpl implements BarcodeDetection { | 29 public class BarcodeDetectionImpl implements BarcodeDetection { |
31 private static final String TAG = "BarcodeDetectionImpl"; | 30 private static final String TAG = "BarcodeDetectionImpl"; |
32 | 31 |
33 private final Context mContext; | 32 private final Context mContext; |
34 private BarcodeDetector mBarcodeDetector; | 33 private BarcodeDetector mBarcodeDetector; |
35 | 34 |
36 public BarcodeDetectionImpl(Context context) { | 35 public BarcodeDetectionImpl(Context context) { |
37 mContext = context; | 36 mContext = context; |
38 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); | 37 mBarcodeDetector = new BarcodeDetector.Builder(mContext).build(); |
39 } | 38 } |
40 | 39 |
41 @Override | 40 @Override |
42 public void detect( | 41 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse
callback) { |
43 SharedBufferHandle frameData, int width, int height, DetectResponse
callback) { | |
44 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mC
ontext) | 42 if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(mC
ontext) |
45 != ConnectionResult.SUCCESS) { | 43 != ConnectionResult.SUCCESS) { |
46 Log.e(TAG, "Google Play Services not available"); | 44 Log.e(TAG, "Google Play Services not available"); |
47 callback.call(new BarcodeDetectionResult[0]); | 45 callback.call(new BarcodeDetectionResult[0]); |
48 return; | 46 return; |
49 } | 47 } |
50 // The vision library will be downloaded the first time the API is used | 48 // The vision library will be downloaded the first time the API is used |
51 // on the device; this happens "fast", but it might have not completed, | 49 // on the device; this happens "fast", but it might have not completed, |
52 // bail in this case. Also, the API was disabled between and v.9.0 and | 50 // bail in this case. Also, the API was disabled between and v.9.0 and |
53 // v.9.2, see https://developers.google.com/android/guides/releases. | 51 // v.9.2, see https://developers.google.com/android/guides/releases. |
54 if (!mBarcodeDetector.isOperational()) { | 52 if (!mBarcodeDetector.isOperational()) { |
55 Log.e(TAG, "BarcodeDetector is not operational"); | 53 Log.e(TAG, "BarcodeDetector is not operational"); |
56 callback.call(new BarcodeDetectionResult[0]); | 54 callback.call(new BarcodeDetectionResult[0]); |
57 return; | 55 return; |
58 } | 56 } |
59 | 57 |
60 Frame frame = SharedBufferUtils.convertToFrame(frameData, width, height)
; | 58 Frame frame = SharedBufferUtils.convertToFrame(bitmapData); |
61 if (frame == null) { | 59 if (frame == null) { |
62 Log.e(TAG, "Error converting SharedMemory to Frame"); | 60 Log.e(TAG, "Error converting SharedMemory to Frame"); |
63 callback.call(new BarcodeDetectionResult[0]); | 61 callback.call(new BarcodeDetectionResult[0]); |
64 return; | 62 return; |
65 } | 63 } |
66 | 64 |
67 final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame); | 65 final SparseArray<Barcode> barcodes = mBarcodeDetector.detect(frame); |
68 | 66 |
69 BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barco
des.size()]; | 67 BarcodeDetectionResult[] barcodeArray = new BarcodeDetectionResult[barco
des.size()]; |
70 for (int i = 0; i < barcodes.size(); i++) { | 68 for (int i = 0; i < barcodes.size(); i++) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 public Factory(Context context) { | 105 public Factory(Context context) { |
108 mContext = context; | 106 mContext = context; |
109 } | 107 } |
110 | 108 |
111 @Override | 109 @Override |
112 public BarcodeDetection createImpl() { | 110 public BarcodeDetection createImpl() { |
113 return new BarcodeDetectionImpl(mContext); | 111 return new BarcodeDetectionImpl(mContext); |
114 } | 112 } |
115 } | 113 } |
116 } | 114 } |
OLD | NEW |