1 package com.example.helloandroidcamera2;
2 
3 import android.graphics.ImageFormat;
4 import android.media.Image;
5 
6 import java.nio.ByteBuffer;
7 
8 /**
9  * A Java wrapper around a native Halide YuvBufferT.
10  */
11 public class HalideYuvBufferT implements AutoCloseable {
12 
13     // Load native Halide shared library.
14     static {
15         System.loadLibrary("HelloAndroidCamera2");
16     }
17 
18     private long mHandle;
19 
20     /**
21      * Allocate a native YUV handle to an Image. It needs to be closed with close().
22      */
fromImage(Image image)23     public static HalideYuvBufferT fromImage(Image image) {
24         if (image.getFormat() != ImageFormat.YUV_420_888) {
25             throw new IllegalArgumentException("src must have format YUV_420_888.");
26         }
27         Image.Plane[] planes = image.getPlanes();
28         // Spec guarantees that planes[0] is luma and has pixel stride of 1.
29         // It also guarantees that planes[1] and planes[2] have the same row and
30         // pixel stride.
31         long handle = AndroidBufferUtilities.allocNativeYuvBufferT(
32                 image.getWidth(),image.getHeight(),
33                 planes[0].getBuffer(), planes[0].getRowStride(), planes[1].getBuffer(),
34                 planes[2].getBuffer(), planes[1].getPixelStride(), planes[1].getRowStride());
35         return new HalideYuvBufferT(handle);
36     }
37 
HalideYuvBufferT(long handle)38     public HalideYuvBufferT(long handle) {
39         mHandle = handle;
40     }
41 
handle()42     public long handle() {
43         return mHandle;
44     }
45 
rotate180()46     public void rotate180() {
47         AndroidBufferUtilities.rotateNativeYuvBufferT180(mHandle);
48     }
49 
50     @Override
close()51     public void close() {
52         if (mHandle != 0L) {
53             AndroidBufferUtilities.freeNativeYuvBufferT(mHandle);
54             mHandle = 0L;
55         }
56     }
57 
58     @Override
finalize()59     protected void finalize() {
60         close();
61     }
62 }
63