Image.java revision 5e712064dfe48992f8f732208fa4fc13f3455b30
1/* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.media; 18 19import java.nio.ByteBuffer; 20import java.lang.AutoCloseable; 21 22/** 23 * <p>A single complete image buffer to use with a media source such as a 24 * {@link MediaCodec} or a 25 * {@link android.hardware.camera2.CameraDevice CameraDevice}.</p> 26 * 27 * <p>This class allows for efficient direct application access to the pixel 28 * data of the Image through one or more 29 * {@link java.nio.ByteBuffer ByteBuffers}. Each buffer is encapsulated in a 30 * {@link Plane} that describes the layout of the pixel data in that plane. Due 31 * to this direct access, and unlike the {@link android.graphics.Bitmap Bitmap} class, 32 * Images are not directly usable as as UI resources.</p> 33 * 34 * <p>Since Images are often directly produced or consumed by hardware 35 * components, they are a limited resource shared across the system, and should 36 * be closed as soon as they are no longer needed.</p> 37 * 38 * <p>For example, when using the {@link ImageReader} class to read out Images 39 * from various media sources, not closing old Image objects will prevent the 40 * availability of new Images once 41 * {@link ImageReader#getMaxImages the maximum outstanding image count} is 42 * reached. When this happens, the function acquiring new Images will typically 43 * throw a 44 * {@link ImageReader.MaxImagesAcquiredException MaxImagesAcquiredException}.</p> 45 * 46 * @see ImageReader 47 */ 48public abstract class Image implements AutoCloseable { 49 /** 50 * @hide 51 */ 52 protected Image() { 53 } 54 55 /** 56 * Get the format for this image. This format determines the number of 57 * ByteBuffers needed to represent the image, and the general layout of the 58 * pixel data in each in ByteBuffer. 59 * 60 * <p> 61 * The format is one of the values from 62 * {@link android.graphics.ImageFormat ImageFormat}. The mapping between the 63 * formats and the planes is as follows: 64 * </p> 65 * 66 * <table> 67 * <tr> 68 * <th>Format</th> 69 * <th>Plane count</th> 70 * <th>Layout details</th> 71 * </tr> 72 * <tr> 73 * <td>{@link android.graphics.ImageFormat#JPEG JPEG}</td> 74 * <td>1</td> 75 * <td>Compressed data, so row and pixel strides are 0. To uncompress, use 76 * {@link android.graphics.BitmapFactory#decodeByteArray BitmapFactory#decodeByteArray}. 77 * </td> 78 * </tr> 79 * <tr> 80 * <td>{@link android.graphics.ImageFormat#YUV_420_888 YUV_420_888}</td> 81 * <td>3</td> 82 * <td>A luminance plane followed by the Cb and Cr chroma planes. 83 * The chroma planes have half the width and height of the luminance 84 * plane (4:2:0 subsampling). Each pixel sample in each plane has 8 bits. 85 * Each plane has its own row stride and pixel stride.</td> 86 * </tr> 87 * <tr> 88 * <td>{@link android.graphics.ImageFormat#RAW_SENSOR RAW_SENSOR}</td> 89 * <td>1</td> 90 * <td>A single plane of raw sensor image data, with 16 bits per color 91 * sample. The details of the layout need to be queried from the source of 92 * the raw sensor data, such as 93 * {@link android.hardware.camera2.CameraDevice CameraDevice}. 94 * </td> 95 * </tr> 96 * </table> 97 * 98 * @see android.graphics.ImageFormat 99 */ 100 public abstract int getFormat(); 101 102 /** 103 * The width of the image in pixels. For formats where some color channels 104 * are subsampled, this is the width of the largest-resolution plane. 105 */ 106 public abstract int getWidth(); 107 108 /** 109 * The height of the image in pixels. For formats where some color channels 110 * are subsampled, this is the height of the largest-resolution plane. 111 */ 112 public abstract int getHeight(); 113 114 /** 115 * Get the timestamp associated with this frame. 116 * <p> 117 * The timestamp is measured in nanoseconds, and is monotonically 118 * increasing. However, the zero point and whether the timestamp can be 119 * compared against other sources of time or images depend on the source of 120 * this image. 121 * </p> 122 */ 123 public abstract long getTimestamp(); 124 125 /** 126 * Get the array of pixel planes for this Image. The number of planes is 127 * determined by the format of the Image. 128 */ 129 public abstract Plane[] getPlanes(); 130 131 /** 132 * Free up this frame for reuse. 133 * <p> 134 * After calling this method, calling any methods on this {@code Image} will 135 * result in an {@link IllegalStateException}, and attempting to read from 136 * {@link ByteBuffer ByteBuffers} returned by an earlier 137 * {@link Plane#getBuffer} call will have undefined behavior. 138 * </p> 139 */ 140 @Override 141 public abstract void close(); 142 143 /** 144 * <p>A single color plane of image data.</p> 145 * 146 * <p>The number and meaning of the planes in an Image are determined by the 147 * format of the Image.</p> 148 * 149 * <p>Once the Image has been closed, any access to the the plane's 150 * ByteBuffer will fail.</p> 151 * 152 * @see #getFormat 153 */ 154 public static abstract class Plane { 155 /** 156 * @hide 157 */ 158 protected Plane() { 159 } 160 161 /** 162 * <p>The row stride for this color plane, in bytes.</p> 163 * 164 * <p>This is the distance between the start of two consecutive rows of 165 * pixels in the image. The row stride is always greater than 0.</p> 166 */ 167 public abstract int getRowStride(); 168 /** 169 * <p>The distance between adjacent pixel samples, in bytes.</p> 170 * 171 * <p>This is the distance between two consecutive pixel values in a row 172 * of pixels. It may be larger than the size of a single pixel to 173 * account for interleaved image data or padded formats. 174 * The pixel stride is always greater than 0.</p> 175 */ 176 public abstract int getPixelStride(); 177 /** 178 * <p>Get a direct {@link java.nio.ByteBuffer ByteBuffer} 179 * containing the frame data.</p> 180 * 181 * <p>In particular, the buffer returned will always have 182 * {@link java.nio.ByteBuffer#isDirect isDirect} return {@code true}, so 183 * the underlying data could be mapped as a pointer in JNI without doing 184 * any copies with {@code GetDirectBufferAddress}.</p> 185 * 186 * @return the byte buffer containing the image data for this plane. 187 */ 188 public abstract ByteBuffer getBuffer(); 189 } 190 191} 192