Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: services/shape_detection/android/java/src/org/chromium/shape_detection/FaceDetectionImpl.java

Issue 2875243002: RELAND: ShapeDetection: use mojom::Bitmap for mojo interface. (Closed)
Patch Set: RELAND2: ShapeDetection: use mojom::Bitmap for mojo interface. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // TODO(junwei.fu): Use |bitmapData| directly for |unPremultipliedBitmap | to spare a copy
43 final long numPixels = (long) width * height; 38 // if the bitmap pixel format is RGB_565, the ARGB_8888 Bitmap doesn't n eed to be created
44 // TODO(xianglu): https://crbug.com/670028 homogeneize overflow checking . 39 // in this case, https://crbug.com/684930.
45 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo ng.MAX_VALUE / 4)) { 40 Bitmap bitmap = BitmapUtils.convertToBitmap(bitmapData);
46 Log.d(TAG, "Invalid argument(s).");
47 callback.call(new FaceDetectionResult[0]);
48 return;
49 }
50
51 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none() );
52 if (imageBuffer.capacity() <= 0) {
53 Log.d(TAG, "Failed to map from SharedBufferHandle.");
54 callback.call(new FaceDetectionResult[0]);
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 41
68 // A Bitmap must be in 565 format for findFaces() to work. See 42 // 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 43 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/media/java/andro id/media/FaceDetector.java#124
70 // 44 //
71 // It turns out that FaceDetector is not able to detect correctly if 45 // It turns out that FaceDetector is not able to detect correctly if
72 // simply using pixmap.setConfig(). The reason might be that findFaces() 46 // simply using pixmap.setConfig(). The reason might be that findFaces()
73 // needs non-premultiplied ARGB arrangement, while the alpha type in the 47 // needs non-premultiplied ARGB arrangement, while the alpha type in the
74 // original image is premultiplied. We can use getPixels() which does 48 // original image is premultiplied. We can use getPixels() which does
75 // the unmultiplication while copying to a new array. See 49 // 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 50 // http://androidxref.com/7.0.0_r1/xref/frameworks/base/graphics/java/an droid/graphics/Bitmap.java#538
51 final int width = bitmapData.width;
52 final int height = bitmapData.height;
77 int[] pixels = new int[width * height]; 53 int[] pixels = new int[width * height];
78 bitmap.getPixels(pixels, 0, width, 0, 0, width, height); 54 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
79 final Bitmap unPremultipliedBitmap = 55 final Bitmap unPremultipliedBitmap =
80 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 ); 56 Bitmap.createBitmap(pixels, width, height, Bitmap.Config.RGB_565 );
81 57
82 // FaceDetector creation and findFaces() might take a long time and trig ger a 58 // FaceDetector creation and findFaces() might take a long time and trig ger a
83 // "StrictMode policy violation": they should happen in a background thr ead. 59 // "StrictMode policy violation": they should happen in a background thr ead.
84 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() { 60 AsyncTask.THREAD_POOL_EXECUTOR.execute(new Runnable() {
85 @Override 61 @Override
86 public void run() { 62 public void run() {
(...skipping 28 matching lines...) Expand all
115 } 91 }
116 92
117 @Override 93 @Override
118 public void close() {} 94 public void close() {}
119 95
120 @Override 96 @Override
121 public void onConnectionError(MojoException e) { 97 public void onConnectionError(MojoException e) {
122 close(); 98 close();
123 } 99 }
124 } 100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698