YuvImagepublic class YuvImage extends Object YuvImage contains YUV data and provides a method that compresses a region of
the YUV data to a Jpeg. The YUV data should be provided as a single byte
array irrespective of the number of image planes in it.
Currently only ImageFormat.NV21 and ImageFormat.YUY2 are supported.
To compress a rectangle region in the YUV data, users have to specify the
region by left, top, width and height. |
Fields Summary |
---|
private static final int | WORKING_COMPRESS_STORAGENumber of bytes of temp storage we use for communicating between the
native compressor and the java OutputStream. | private int | mFormatThe YUV format as defined in {@link ImageFormat}. | private byte[] | mDataThe raw YUV data.
In the case of more than one image plane, the image planes must be
concatenated into a single byte array. | private int[] | mStridesThe number of row bytes in each image plane. | private int | mWidthThe width of the image. | private int | mHeightThe height of the the image. |
Constructors Summary |
---|
public YuvImage(byte[] yuv, int format, int width, int height, int[] strides)Construct an YuvImage.
if (format != ImageFormat.NV21 &&
format != ImageFormat.YUY2) {
throw new IllegalArgumentException(
"only support ImageFormat.NV21 " +
"and ImageFormat.YUY2 for now");
}
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException(
"width and height must large than 0");
}
if (yuv == null) {
throw new IllegalArgumentException("yuv cannot be null");
}
if (strides == null) {
mStrides = calculateStrides(width, format);
} else {
mStrides = strides;
}
mData = yuv;
mFormat = format;
mWidth = width;
mHeight = height;
|
Methods Summary |
---|
private void | adjustRectangle(Rect rect)
int width = rect.width();
int height = rect.height();
if (mFormat == ImageFormat.NV21) {
// Make sure left, top, width and height are all even.
width &= ~1;
height &= ~1;
rect.left &= ~1;
rect.top &= ~1;
rect.right = rect.left + width;
rect.bottom = rect.top + height;
}
if (mFormat == ImageFormat.YUY2) {
// Make sure left and width are both even.
width &= ~1;
rect.left &= ~1;
rect.right = rect.left + width;
}
| int[] | calculateOffsets(int left, int top)
int[] offsets = null;
if (mFormat == ImageFormat.NV21) {
offsets = new int[] {top * mStrides[0] + left,
mHeight * mStrides[0] + top / 2 * mStrides[1]
+ left / 2 * 2 };
return offsets;
}
if (mFormat == ImageFormat.YUY2) {
offsets = new int[] {top * mStrides[0] + left / 2 * 4};
return offsets;
}
return offsets;
| private int[] | calculateStrides(int width, int format)
int[] strides = null;
if (format == ImageFormat.NV21) {
strides = new int[] {width, width};
return strides;
}
if (format == ImageFormat.YUY2) {
strides = new int[] {width * 2};
return strides;
}
return strides;
| public boolean | compressToJpeg(Rect rectangle, int quality, java.io.OutputStream stream)Compress a rectangle region in the YuvImage to a jpeg.
Only ImageFormat.NV21 and ImageFormat.YUY2
are supported for now.
Rect wholeImage = new Rect(0, 0, mWidth, mHeight);
if (!wholeImage.contains(rectangle)) {
throw new IllegalArgumentException(
"rectangle is not inside the image");
}
if (quality < 0 || quality > 100) {
throw new IllegalArgumentException("quality must be 0..100");
}
if (stream == null) {
throw new IllegalArgumentException("stream cannot be null");
}
adjustRectangle(rectangle);
int[] offsets = calculateOffsets(rectangle.left, rectangle.top);
return nativeCompressToJpeg(mData, mFormat, rectangle.width(),
rectangle.height(), offsets, mStrides, quality, stream,
new byte[WORKING_COMPRESS_STORAGE]);
| public int | getHeight()
return mHeight;
| public int[] | getStrides()
return mStrides;
| public int | getWidth()
return mWidth;
| public byte[] | getYuvData()
return mData;
| public int | getYuvFormat()
return mFormat;
| private static native boolean | nativeCompressToJpeg(byte[] oriYuv, int format, int width, int height, int[] offsets, int[] strides, int quality, java.io.OutputStream stream, byte[] tempStorage)
|
|