本文整理汇总了Java中android.media.Image.getFormat方法的典型用法代码示例。如果您正苦于以下问题:Java Image.getFormat方法的具体用法?Java Image.getFormat怎么用?Java Image.getFormat使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.media.Image
的用法示例。
在下文中一共展示了Image.getFormat方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extract
import android.media.Image; //导入方法依赖的package包/类
/**
* Extracts the Y-Plane from the YUV_420_8888 image to creates a IntensityPlane.
* The actual plane data will be copied into the new IntensityPlane object.
*
* @throws IllegalArgumentException if the provided images is not in the YUV_420_888 format
*/
@NonNull
public static IntensityPlane extract(@NonNull Image img) {
if (img.getFormat() != ImageFormat.YUV_420_888) {
throw new IllegalArgumentException("image format must be YUV_420_888");
}
Image.Plane[] planes = img.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
byte[] yPlane = new byte[buffer.remaining()];
buffer.get(yPlane);
int yRowStride = planes[0].getRowStride();
return new IntensityPlane(img.getWidth(), img.getHeight(), yPlane, yRowStride);
}
示例2: toByteArray
import android.media.Image; //导入方法依赖的package包/类
private static byte[] toByteArray(Image image) {
byte[] data = null;
if (image.getFormat() == ImageFormat.JPEG) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
data = new byte[buffer.capacity()];
buffer.get(data);
return data;
} else if (image.getFormat() == ImageFormat.YUV_420_888) {
data = NV21toJPEG(
YUV_420_888toNV21(image),
image.getWidth(), image.getHeight());
} else {
Log.w(TAG, "Unrecognized image format: " + image.getFormat());
}
return data;
}
示例3: onImageAvailable
import android.media.Image; //导入方法依赖的package包/类
@Override
public void onImageAvailable(ImageReader reader) {
Image image = reader.acquireLatestImage();
try {
if (image.getFormat() == ImageFormat.JPEG) {
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
Bitmap rotated = ImageUtils.rotateBitmap(bitmap, rotation, mCamera2Engine.getSensorOrientation());
mImageView.setImageBitmap(rotated);
}
} finally {
image.close();
}
}
示例4: _onSingleBarcode
import android.media.Image; //导入方法依赖的package包/类
private void _onSingleBarcode(String barcode, Point[] points, final Image source, final BarcodeDetectedListener listener, Handler callbackHandler) {
if (listener != null) {
final BarcodeInfo bc = new BarcodeInfo(barcode, points);
final byte[] serialized = (source == null) ? null : ImageDecoder.Serialize(source);
final int width = (source == null) ? 0 : source.getWidth();
final int height = (source == null) ? 0 : source.getHeight();
final int format = (source == null) ? ImageFormat.UNKNOWN : source.getFormat();
callbackHandler.post(
new Runnable() {
@Override
public void run() {
listener.onSingleBarcodeAvailable(bc, serialized, format, width, height);
}
}
);
}
}
示例5: _onMultipleBarcodes
import android.media.Image; //导入方法依赖的package包/类
private void _onMultipleBarcodes(final Barcode[] barcodes, final Image source, final MultipleBarcodesDetectedListener listener, Handler callbackHandler) {
if (listener != null) {
final byte[] serialized = (source == null) ? null : ImageDecoder.Serialize(source);
final int width = (source == null) ? 0 : source.getWidth();
final int height = (source == null) ? 0 : source.getHeight();
final int format = (source == null) ? ImageFormat.UNKNOWN : source.getFormat();
callbackHandler.post(
new Runnable() {
@Override
public void run() {
listener.onMultipleBarcodeAvailable(_convert(barcodes), serialized, format, width, height);
}
}
);
}
}
示例6: Serialize
import android.media.Image; //导入方法依赖的package包/类
public static byte[] Serialize(Image image)
{
if (image==null)
return null;
Image.Plane[] planes = image.getPlanes();
// NV21 expects planes in order YVU, not YUV:
if (image.getFormat() == ImageFormat.YUV_420_888)
planes = new Image.Plane[] {planes[0], planes[2], planes[1]};
byte[] serializeBytes = new byte[getSerializedSize(image)];
int nextFree = 0;
for (Image.Plane plane: planes)
{
ByteBuffer buffer = plane.getBuffer();
buffer.position(0);
int nBytes = buffer.remaining();
plane.getBuffer().get(serializeBytes, nextFree, nBytes);
nextFree += nBytes;
}
return serializeBytes;
}
示例7: renderHevcImageWithFormat
import android.media.Image; //导入方法依赖的package包/类
private static Bitmap renderHevcImageWithFormat(ByteBuffer bitstream, ImageInfo info, int imageFormat) throws FormatFallbackException {
try (ImageReader reader = ImageReader.newInstance(info.size.getWidth(), info.size.getHeight(), imageFormat, 1)) {
renderHevcImage(bitstream, info, reader.getSurface());
Image image = null;
try {
try {
image = reader.acquireNextImage();
} catch (UnsupportedOperationException ex) {
throw new FormatFallbackException(ex);
}
switch (image.getFormat()) {
case ImageFormat.YUV_420_888:
case ImageFormat.YV12:
return convertYuv420ToBitmap(image);
case ImageFormat.RGB_565:
return convertRgb565ToBitmap(image);
default:
throw new RuntimeException("unsupported image format(" + image.getFormat() + ")");
}
} finally {
if (image != null) {
image.close();
}
}
}
}
示例8: imageToByteArray
import android.media.Image; //导入方法依赖的package包/类
public static byte[] imageToByteArray(Image image) {
byte[] data = null;
if (image.getFormat() == ImageFormat.JPEG) {
Image.Plane[] planes = image.getPlanes();
ByteBuffer buffer = planes[0].getBuffer();
data = new byte[buffer.capacity()];
buffer.get(data);
return data;
} else if (image.getFormat() == ImageFormat.YUV_420_888) {
data = NV21toJPEG(
YUV_420_888toNV21(image),
image.getWidth(), image.getHeight());
}
return data;
}
示例9: acquireJpegBytesAndClose
import android.media.Image; //导入方法依赖的package包/类
/**
* Given an image reader, extracts the JPEG image bytes and then closes the
* reader.
*
* @param reader the reader to read the JPEG data from.
* @return The bytes of the JPEG image. Newly allocated.
*/
private static byte[] acquireJpegBytesAndClose(ImageReader reader) {
Image img = reader.acquireLatestImage();
ByteBuffer buffer;
if (img.getFormat() == ImageFormat.JPEG) {
Image.Plane plane0 = img.getPlanes()[0];
buffer = plane0.getBuffer();
} else if (img.getFormat() == ImageFormat.YUV_420_888) {
buffer = ByteBuffer.allocateDirect(img.getWidth() * img.getHeight() * 3);
Log.v(TAG, "Compressing JPEG with software encoder.");
int numBytes = JpegUtilNative.compressJpegFromYUV420Image(img, buffer, JPEG_QUALITY);
if (numBytes < 0) {
throw new RuntimeException("Error compressing jpeg.");
}
buffer.limit(numBytes);
} else {
throw new RuntimeException("Unsupported image format.");
}
byte[] imageBytes = new byte[buffer.remaining()];
buffer.get(imageBytes);
buffer.rewind();
img.close();
return imageBytes;
}
示例10: DecodeImage
import android.media.Image; //导入方法依赖的package包/类
public BinaryBitmap DecodeImage(Image source)
{
BinaryBitmap bitmap = null;
byte[] bytes = null;
int imageFormat = source.getFormat();
int w = source.getWidth();
int h = source.getHeight();
Image.Plane plane = source.getPlanes()[0];
ByteBuffer buffer = plane.getBuffer();
bytes = new byte[buffer.remaining()];
buffer.get(bytes);
if (LuminanceSourceFactory.isFormatSupported(imageFormat) == false)
throw new UnsupportedOperationException("ZXing cannot process images of type " + imageFormat);
// First we'll convert into a BinaryBitmap:
try {
LuminanceSource lumSource = LuminanceSourceFactory.getLuminanceSource(imageFormat, bytes, w, h);
bitmap = new BinaryBitmap(new HybridBinarizer(lumSource));
} catch (Exception e) {
Log.e(TAG, "failed reading captured image", e);
return null;
}
return bitmap;
}
示例11: compressJpegFromYUV420Image
import android.media.Image; //导入方法依赖的package包/类
/**
* Compresses the given image to jpeg. Note that only ImageFormat.YUV_420_888 is currently
* supported. Furthermore, all planes must use direct byte buffers.
*
* @param img the image to compress
* @param outBuf a direct byte buffer to hold the output jpeg.
* @return The number of bytes written to outBuf
*/
public static int compressJpegFromYUV420Image(Image img, ByteBuffer outBuf, int quality) {
if (img.getFormat() != ImageFormat.YUV_420_888) {
throw new RuntimeException("Unsupported Image Format.");
}
final int NUM_PLANES = 3;
if (img.getPlanes().length != NUM_PLANES) {
throw new RuntimeException("Output buffer must be direct.");
}
if (!outBuf.isDirect()) {
throw new RuntimeException("Output buffer must be direct.");
}
ByteBuffer[] planeBuf = new ByteBuffer[NUM_PLANES];
int[] pixelStride = new int[NUM_PLANES];
int[] rowStride = new int[NUM_PLANES];
for (int i = 0; i < NUM_PLANES; i++) {
Plane plane = img.getPlanes()[i];
if (!plane.getBuffer().isDirect()) {
return -1;
}
planeBuf[i] = plane.getBuffer();
pixelStride[i] = plane.getPixelStride();
rowStride[i] = plane.getRowStride();
}
outBuf.clear();
int numBytesWritten = compressJpegFromYUV420p(
img.getWidth(), img.getHeight(),
planeBuf[0], pixelStride[0], rowStride[0],
planeBuf[1], pixelStride[1], rowStride[1],
planeBuf[2], pixelStride[2], rowStride[2],
outBuf, quality);
outBuf.limit(numBytesWritten);
return numBytesWritten;
}