OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.shape_detection; | |
6 | |
7 import android.graphics.Bitmap; | |
8 | |
9 import com.google.android.gms.vision.Frame; | |
10 | |
11 import org.chromium.mojo.system.MojoException; | |
12 import org.chromium.mojo.system.SharedBufferHandle; | |
13 import org.chromium.mojo.system.SharedBufferHandle.MapFlags; | |
14 | |
15 import java.nio.ByteBuffer; | |
16 | |
17 /** | |
18 * Utility class to convert a SharedBufferHandle to a GMS core YUV Frame. | |
19 */ | |
20 public class SharedBufferUtils { | |
21 public static Frame convertToFrame( | |
22 SharedBufferHandle frameData, final int width, final int height) { | |
23 final long numPixels = (long) width * height; | |
24 // TODO(mcasas): https://crbug.com/670028 homogeneize overflow checking. | |
25 if (!frameData.isValid() || width <= 0 || height <= 0 || numPixels > (Lo
ng.MAX_VALUE / 4)) { | |
26 return null; | |
27 } | |
28 | |
29 // Mapping |frameData| will fail if the intended mapped size is larger | |
30 // than its actual capacity, which is limited by the appropriate | |
31 // mojo::edk::Configuration entry. | |
32 ByteBuffer imageBuffer = frameData.map(0, numPixels * 4, MapFlags.none()
); | |
33 if (imageBuffer.capacity() <= 0) { | |
34 return null; | |
35 } | |
36 | |
37 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_88
88); | |
38 bitmap.copyPixelsFromBuffer(imageBuffer); | |
39 try { | |
40 frameData.unmap(imageBuffer); | |
41 frameData.close(); | |
42 } catch (MojoException e) { | |
43 } | |
44 | |
45 try { | |
46 // This constructor implies a pixel format conversion to YUV. | |
47 return new Frame.Builder().setBitmap(bitmap).build(); | |
48 } catch (IllegalArgumentException | IllegalStateException ex) { | |
49 return null; | |
50 } | |
51 } | |
52 } | |
OLD | NEW |