| 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.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
| 8 import android.graphics.PointF; | 8 import android.graphics.PointF; |
| 9 import android.media.FaceDetector; | 9 import android.media.FaceDetector; |
| 10 import android.media.FaceDetector.Face; | 10 import android.media.FaceDetector.Face; |
| 11 import android.os.AsyncTask; | 11 import android.os.AsyncTask; |
| 12 | 12 |
| 13 import org.chromium.base.Log; | |
| 14 import org.chromium.gfx.mojom.RectF; | 13 import org.chromium.gfx.mojom.RectF; |
| 15 import org.chromium.mojo.system.MojoException; | 14 import org.chromium.mojo.system.MojoException; |
| 16 import org.chromium.mojo.system.SharedBufferHandle; | |
| 17 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
| 18 import org.chromium.shape_detection.mojom.FaceDetection; | 15 import org.chromium.shape_detection.mojom.FaceDetection; |
| 19 import org.chromium.shape_detection.mojom.FaceDetectionResult; | 16 import org.chromium.shape_detection.mojom.FaceDetectionResult; |
| 20 import org.chromium.shape_detection.mojom.FaceDetectorOptions; | 17 import org.chromium.shape_detection.mojom.FaceDetectorOptions; |
| 21 import org.chromium.shape_detection.mojom.Landmark; | 18 import org.chromium.shape_detection.mojom.Landmark; |
| 22 | 19 |
| 23 import java.nio.ByteBuffer; | |
| 24 | |
| 25 /** | 20 /** |
| 26 * Android implementation of the FaceDetection service defined in | 21 * Android implementation of the FaceDetection service defined in |
| 27 * services/shape_detection/public/interfaces/facedetection.mojom | 22 * services/shape_detection/public/interfaces/facedetection.mojom |
| 28 */ | 23 */ |
| 29 public class FaceDetectionImpl implements FaceDetection { | 24 public class FaceDetectionImpl implements FaceDetection { |
| 30 private static final String TAG = "FaceDetectionImpl"; | 25 private static final String TAG = "FaceDetectionImpl"; |
| 31 private static final int MAX_FACES = 32; | 26 private static final int MAX_FACES = 32; |
| 32 private final boolean mFastMode; | 27 private final boolean mFastMode; |
| 33 private final int mMaxFaces; | 28 private final int mMaxFaces; |
| 34 | 29 |
| 35 FaceDetectionImpl(FaceDetectorOptions options) { | 30 FaceDetectionImpl(FaceDetectorOptions options) { |
| 36 mFastMode = options.fastMode; | 31 mFastMode = options.fastMode; |
| 37 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); | 32 mMaxFaces = Math.min(options.maxDetectedFaces, MAX_FACES); |
| 38 } | 33 } |
| 39 | 34 |
| 40 @Override | 35 @Override |
| 41 public void detect(SharedBufferHandle frameData, final int width, final int
height, | 36 public void detect(org.chromium.skia.mojom.Bitmap bitmapData, final DetectRe
sponse callback) { |
| 42 final DetectResponse callback) { | 37 final int width = bitmapData.width; |
| 43 final long numPixels = (long) width * height; | 38 final int height = bitmapData.height; |
| 44 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking
. | |
| 45 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { | |
| 46 Log.d(TAG, "Invalid argument(s)."); | |
| 47 callback.call(new FaceDetectionResult[0]); | |
| 48 return; | |
| 49 } | |
| 50 | 39 |
| 51 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); | 40 // TODO(junwei.fu): Use |bitmapData| directly for |unPremultipliedBitmap
| to spare a copy |
| 52 if (imageBuffer.capacity() <= 0) { | 41 // if the bitmap pixel format is RGB_565, the ARGB_8888 Bitmap doesn't n
eed to be created |
| 53 Log.d(TAG, "Failed to map from SharedBufferHandle."); | 42 // in this case, https://crbug.com/684930. |
| 54 callback.call(new FaceDetectionResult[0]); | 43 Bitmap bitmap = BitmapUtils.createBitmap(bitmapData); |
| 55 return; | |
| 56 } | |
| 57 | |
| 58 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | |
| 59 | |
| 60 // An int array is needed to construct a Bitmap. However the Bytebuffer | |
| 61 // we get from |sharedBufferHandle| is directly allocated and does not | |
| 62 // have a supporting array. Therefore we need to copy from |imageBuffer| | |
| 63 // to create this intermediate Bitmap. | |
| 64 // TODO(xianglu): Consider worker pool as appropriate threads. | |
| 65 // http://crbug.com/655814 | |
| 66 bitmap.copyPixelsFromBuffer(imageBuffer); | |
| 67 | 44 |
| 68 // A Bitmap must be in 565 format for findFaces() to work. See | 45 // A Bitmap must be in 565 format for findFaces() to work. See |
| 69 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro
id/media/FaceDetector.java#124 | 46 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro
id/media/FaceDetector.java#124 |
| 70 // | 47 // |
| 71 // It turns out that FaceDetector is not able to detect correctly if | 48 // It turns out that FaceDetector is not able to detect correctly if |
| 72 // simply using pixmap.setConfig(). The reason might be that findFaces() | 49 // simply using pixmap.setConfig(). The reason might be that findFaces() |
| 73 // needs non-premultiplied ARGB arrangement, while the alpha type in the | 50 // needs non-premultiplied ARGB arrangement, while the alpha type in the |
| 74 // original image is premultiplied. We can use getPixels() which does | 51 // original image is premultiplied. We can use getPixels() which does |
| 75 // the unmultiplication while copying to a new array. See | 52 // the unmultiplication while copying to a new array. See |
| 76 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an
droid/graphics/Bitmap.java#538 | 53 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an
droid/graphics/Bitmap.java#538 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 92 } |
| 116 | 93 |
| 117 @Override | 94 @Override |
| 118 public void close() {} | 95 public void close() {} |
| 119 | 96 |
| 120 @Override | 97 @Override |
| 121 public void onConnectionError(MojoException e) { | 98 public void onConnectionError(MojoException e) { |
| 122 close(); | 99 close(); |
| 123 } | 100 } |
| 124 } | 101 } |
| OLD | NEW |