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.content.browser.shapedetection; | 5 package org.chromium.content.browser.shapedetection; |
6 | 6 |
7 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
8 import android.graphics.Bitmap.Config; | |
8 import android.graphics.PointF; | 9 import android.graphics.PointF; |
9 import android.media.FaceDetector; | 10 import android.media.FaceDetector; |
10 import android.media.FaceDetector.Face; | 11 import android.media.FaceDetector.Face; |
11 | 12 |
12 import org.chromium.base.Log; | 13 import org.chromium.base.Log; |
13 import org.chromium.gfx.mojom.RectF; | 14 import org.chromium.gfx.mojom.RectF; |
14 import org.chromium.mojo.system.MojoException; | 15 import org.chromium.mojo.system.MojoException; |
15 import org.chromium.mojo.system.SharedBufferHandle; | |
16 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
17 import org.chromium.shape_detection.mojom.FaceDetection; | 16 import org.chromium.shape_detection.mojom.FaceDetection; |
18 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 17 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
19 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 18 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
19 import org.chromium.skia.mojom.ColorType; | |
20 | 20 |
21 import java.nio.ByteBuffer; | 21 import java.nio.ByteBuffer; |
22 | 22 |
23 /** | 23 /** |
24 * Android implementation of the FaceDetection service defined in | 24 * Android implementation of the FaceDetection service defined in |
25 * services/shape_detection/public/interfaces/facedetection.mojom | 25 * services/shape_detection/public/interfaces/facedetection.mojom |
26 */ | 26 */ |
27 public class FaceDetectionImpl implements FaceDetection { | 27 public class FaceDetectionImpl implements FaceDetection { |
28 private static final String TAG = "FaceDetectionImpl"; | 28 private static final String TAG = "FaceDetectionImpl"; |
29 private static final int MAX_FACES = 32; | 29 private static final int MAX_FACES = 32; |
30 private final boolean mFastMode; | 30 private final boolean mFastMode; |
31 private final int mMaxFaces; | 31 private final int mMaxFaces; |
32 private Config mBitmapConfig; | |
mcasas
2017/01/24 00:52:06
We don't need a member variable, validConfig()
cou
| |
32 | 33 |
33 FaceDetectionImpl(FaceDetectorOptions options) { | 34 FaceDetectionImpl(FaceDetectorOptions options) { |
34 mFastMode = options.fastMode; | 35 mFastMode = options.fastMode; |
35 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); | 36 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); |
36 } | 37 } |
37 | 38 |
39 private boolean validConfig(int colorType) { | |
40 switch (colorType) { | |
41 case ColorType.ALPHA_8: | |
42 mBitmapConfig = Config.ALPHA_8; | |
43 return true; | |
44 case ColorType.RGB_565: | |
45 mBitmapConfig = Config.RGB_565; | |
46 return true; | |
47 case ColorType.ARGB_4444: | |
48 mBitmapConfig = Config.ARGB_4444; | |
49 return true; | |
50 case ColorType.RGBA_8888: | |
51 mBitmapConfig = Config.ARGB_8888; | |
52 return true; | |
53 } | |
54 return false; | |
55 } | |
56 | |
38 @Override | 57 @Override |
39 public void detect( | 58 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, DetectResponse callback) { |
40 SharedBufferHandle frameData, int width, int height, DetectResponse callback) { | 59 int width = bitmapData.width; |
60 int height = bitmapData.height; | |
41 final long numPixels = (long) width * height; | 61 final long numPixels = (long) width * height; |
42 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . | 62 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . |
43 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { | 63 if (bitmapData.pixelData == null || width <= 0 || height <= 0 |
64 || numPixels > (Long.MAX_VALUE / 4) || !validConfig(bitmapData.c olorType)) { | |
mcasas
2017/01/24 00:52:05
Same comments as in Barcode/TextDetectionImpl
rega
| |
44 Log.d(TAG, "Invalid argument(s)."); | 65 Log.d(TAG, "Invalid argument(s)."); |
45 callback.call(new FaceDetectionResult()); | 66 callback.call(new FaceDetectionResult()); |
46 return; | 67 return; |
47 } | 68 } |
48 | 69 |
49 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() ); | 70 ByteBuffer imageBuffer = ByteBuffer.wrap(bitmapData.pixelData); |
50 if (imageBuffer.capacity() <= 0) { | 71 if (imageBuffer.capacity() <= 0) { |
51 Log.d(TAG, "Failed to map from SharedBufferHandle."); | 72 Log.d(TAG, "Failed to wrap from Bitmap."); |
52 callback.call(new FaceDetectionResult()); | 73 callback.call(new FaceDetectionResult()); |
53 return; | 74 return; |
54 } | 75 } |
55 | 76 |
56 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88 88); | 77 Bitmap unPremultipliedBitmap; |
78 if (bitmapData.colorType == ColorType.RGB_565) { | |
mcasas
2017/01/24 00:52:06
If blink is never sending this format, how could w
| |
79 unPremultipliedBitmap = Bitmap.createBitmap(width, height, mBitmapCo nfig); | |
57 | 80 |
58 // An int array is needed to construct a Bitmap. However the Bytebuffer | 81 // An int array is needed to construct a Bitmap. However the Bytebuf fer |
59 // we get from |sharedBufferHandle| is directly allocated and does not | 82 // we get from |Bitmap| is directly allocated and does not have a su pporting array. |
mcasas
2017/01/24 00:52:06
|| in comments applies to variables, not types, so
| |
60 // have a supporting array. Therefore we need to copy from |imageBuffer| | 83 // Therefore we need to copy from |imageBuffer| to create this inter mediate Bitmap. |
61 // to create this intermediate Bitmap. | 84 // TODO(xianglu): Consider worker pool as appropriate threads. |
62 // TODO(xianglu): Consider worker pool as appropriate threads. | 85 // http://crbug.com/655814 |
63 // http://crbug.com/655814 | 86 unPremultipliedBitmap.copyPixelsFromBuffer(imageBuffer); |
64 bitmap.copyPixelsFromBuffer(imageBuffer); | 87 } else { |
88 Bitmap bitmap = Bitmap.createBitmap(width, height, mBitmapConfig); | |
65 | 89 |
66 // A Bitmap must be in 565 format for findFaces() to work. See | 90 bitmap.copyPixelsFromBuffer(imageBuffer); |
67 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124 | 91 |
68 // | 92 // A Bitmap must be in 565 format for findFaces() to work. See |
69 // It turns out that FaceDetector is not able to detect correctly if | 93 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/a ndroid/media/FaceDetector.java#124 |
70 // simply using pixmap.setConfig(). The reason might be that findFaces() | 94 // |
71 // needs non-premultiplied ARGB arrangement, while the alpha type in the | 95 // It turns out that FaceDetector is not able to detect correctly if |
72 // original image is premultiplied. We can use getPixels() which does | 96 // simply using pixmap.setConfig(). The reason might be that findFac es() |
73 // the unmultiplication while copying to a new array. See | 97 // needs non-premultiplied ARGB arrangement, while the alpha type in the |
74 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538 | 98 // original image is premultiplied. We can use getPixels() which doe s |
75 int[] pixels = new int[width * height]; | 99 // the unmultiplication while copying to a new array. See |
76 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); | 100 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/jav a/android/graphics/Bitmap.java#538 |
77 Bitmap unPremultipliedBitmap = | 101 int[] pixels = new int[width * height]; |
78 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 ); | 102 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); |
103 unPremultipliedBitmap = | |
104 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB _565); | |
105 } | |
79 | 106 |
80 FaceDetector detector = new FaceDetector(width, height, mMaxFaces); | 107 FaceDetector detector = new FaceDetector(width, height, mMaxFaces); |
81 Face[] detectedFaces = new Face[mMaxFaces]; | 108 Face[] detectedFaces = new Face[mMaxFaces]; |
82 // findFaces() will stop at |mMaxFaces|. | 109 // findFaces() will stop at |mMaxFaces|. |
83 final int numberOfFaces = detector.findFaces(unPremultipliedBitmap, dete ctedFaces); | 110 final int numberOfFaces = detector.findFaces(unPremultipliedBitmap, dete ctedFaces); |
84 | 111 |
85 FaceDetectionResult faceDetectionResult = new FaceDetectionResult(); | 112 FaceDetectionResult faceDetectionResult = new FaceDetectionResult(); |
86 faceDetectionResult.boundingBoxes = new RectF[numberOfFaces]; | 113 faceDetectionResult.boundingBoxes = new RectF[numberOfFaces]; |
87 for (int i = 0; i < numberOfFaces; i++) { | 114 for (int i = 0; i < numberOfFaces; i++) { |
88 final Face face = detectedFaces[i]; | 115 final Face face = detectedFaces[i]; |
(...skipping 15 matching lines...) Expand all Loading... | |
104 } | 131 } |
105 | 132 |
106 @Override | 133 @Override |
107 public void close() {} | 134 public void close() {} |
108 | 135 |
109 @Override | 136 @Override |
110 public void onConnectionError(MojoException e) { | 137 public void onConnectionError(MojoException e) { |
111 close(); | 138 close(); |
112 } | 139 } |
113 } | 140 } |
OLD | NEW |