InputImage

public class InputImage extends Object

Represents an image object.

Nested Class Summary

@interface InputImage.ImageFormat Accepted image format of vision APIs. 

Constant Summary

int IMAGE_FORMAT_NV21 See NV21.
int IMAGE_FORMAT_YUV_420_888 See YUV_420_888.
int IMAGE_FORMAT_YV12 See YV12.

Public Method Summary

static InputImage
fromBitmap(Bitmap bitmap, int rotationDegrees)
Creates an InputImage from a Bitmap.
static InputImage
fromByteArray(byte[] byteArray, int width, int height, int rotationDegrees, int format)
Creates an InputImage from a byte array, e.g., what you get from Camera callback.
static InputImage
fromByteBuffer(ByteBuffer byteBuffer, int width, int height, int rotationDegrees, int format)
Creates an InputImage from a ByteBuffer.
static InputImage
fromFilePath(Context context, Uri imageUri)
Creates an InputImage from a local image file Uri.
static InputImage
fromMediaImage(Image image, int rotationDegrees)
Creates an InputImage from an Image object, e.g., what you obtained from android.hardware.camera2 API.

Inherited Method Summary

Constants

public static final int IMAGE_FORMAT_NV21

See NV21.

Constant Value: 17

public static final int IMAGE_FORMAT_YUV_420_888

See YUV_420_888.

Note that although this field is accessible in pre-Kitkat versions, the format is only supported starting from Android API level 19.

Constant Value: 35

public static final int IMAGE_FORMAT_YV12

See YV12.

Constant Value: 842094169

Public Methods

public static InputImage fromBitmap (Bitmap bitmap, int rotationDegrees)

Creates an InputImage from a Bitmap.

Parameters
bitmap the input Bitmap.
rotationDegrees the image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. IllegalArgumentException will be thrown if the input degree is not in the list.

public static InputImage fromByteArray (byte[] byteArray, int width, int height, int rotationDegrees, int format)

Creates an InputImage from a byte array, e.g., what you get from Camera callback.

Parameters
byteArray the content of the image
width the width of the image
height the height of the image
rotationDegrees the image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. IllegalArgumentException will be thrown if the input degree is not in the list.
format the image format. The supported formats are IMAGE_FORMAT_NV21 and IMAGE_FORMAT_YV12. For IMAGE_FORMAT_YUV_420_888, please use fromMediaImage(Image, int).

public static InputImage fromByteBuffer (ByteBuffer byteBuffer, int width, int height, int rotationDegrees, int format)

Creates an InputImage from a ByteBuffer.

We assume the entire buffer from position zero to its limit is all image data.

Parameters
byteBuffer the content of the image
width the width of the image
height the height of the image
rotationDegrees the image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. IllegalArgumentException will be thrown if the input degree is not in the list.
format the image format. The supported formats are IMAGE_FORMAT_NV21 and IMAGE_FORMAT_YV12. For IMAGE_FORMAT_YUV_420_888, please use fromMediaImage(Image, int).

public static InputImage fromFilePath (Context context, Uri imageUri)

Creates an InputImage from a local image file Uri.

Throws
IOException if the image could not be retrieved from the specified imageUri. This could happen even if the Uri came from content chooser, e.g., some users might have content providers for remote resources.

public static InputImage fromMediaImage (Image image, int rotationDegrees)

Creates an InputImage from an Image object, e.g., what you obtained from android.hardware.camera2 API.

After you get back an InputImage, it's safe to close the input Image.

Parameters
image the android.media.Image, where only JPEG / YUV_420_888 formats are supported at this moment and YUV_420_888 is more efficient for all our APIs.
rotationDegrees the image's counter-clockwise orientation degrees. Only 0, 90, 180, 270 are supported. IllegalArgumentException will be thrown if the input degree is not in the list.