Example usage for android.graphics ImageFormat NV21

List of usage examples for android.graphics ImageFormat NV21

Introduction

In this page you can find the example usage for android.graphics ImageFormat NV21.

Prototype

int NV21

To view the source code for android.graphics ImageFormat NV21.

Click Source Link

Document

YCrCb format used for images, which uses the NV21 encoding format.

Usage

From source file:Main.java

public static byte[] swapColors(byte[] data, int w, int h, int pictureFormat) {
    switch (pictureFormat) {
    case ImageFormat.YV12:
        return swapYUV420Planar(data);
    case ImageFormat.NV21:
        return swapYUV420SemiPlanar(data, w, h);
    default:/*  w ww.j a va 2s.c  o m*/
        Log.w("Util", "No color format to swap");
    }
    return data;
}

From source file:Main.java

public static String translatePreviewFormat(int supportedPreviewFormat) {
    switch (supportedPreviewFormat) {
    case ImageFormat.JPEG:
        return "ImageFormat.JPEG";
    case ImageFormat.NV16:
        return "ImageFormat.NV16";
    case ImageFormat.NV21:
        return "ImageFormat.NV21";
    case ImageFormat.RAW10:
        return "ImageFormat.RAW10";
    case ImageFormat.RAW_SENSOR:
        return "ImageFormat.RAW_SENSOR";
    case ImageFormat.RGB_565:
        return "ImageFormat.RGB_565";
    case ImageFormat.UNKNOWN:
        return "ImageFormat.UNKNOWN";
    case ImageFormat.YUV_420_888:
        return "ImageFormat.YUV_420_888";
    case ImageFormat.YUY2:
        return "ImageFormat.YUY2";
    case ImageFormat.YV12:
        return "ImageFormat.YV12";
    default://from  w  ww  .  j  av  a2  s  .  co  m
        return "xxxxxxxxdefault";
    }
}

From source file:Main.java

public static int getEncoderColorFormat(int previewFormat) {
    if (Build.VERSION.SDK_INT >= 21) {
        return MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Flexible;
    }/*from w ww  . ja  v  a  2  s  .co m*/
    switch (previewFormat) {
    case ImageFormat.NV21:
        return MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420SemiPlanar;
    case ImageFormat.YV12:
        return MediaCodecInfo.CodecCapabilities.COLOR_FormatYUV420Planar;
    }
    return -1;
}

From source file:Main.java

public static Bitmap yuv2bitmap(byte[] data, int width, int height) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    yuvImage.compressToJpeg(new android.graphics.Rect(0, 0, width, height), 100, out);
    byte[] imageBytes = out.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

    // rotate//from   w w  w . ja v a  2  s. com
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Bitmap dst = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    return dst;
}

From source file:Main.java

public static Bitmap createBitmapFromByteArray(byte[] data, Size previewSize) {
    YuvImage yuvimage = new YuvImage(data, ImageFormat.NV21, previewSize.width, previewSize.height, null);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    yuvimage.compressToJpeg(new Rect(0, 0, previewSize.width, previewSize.height), 80, baos);
    byte[] jdata = baos.toByteArray();
    BitmapFactory.Options opt = new BitmapFactory.Options();
    opt.inMutable = true;//from   w  w w.  j ava  2s .  c  om
    Bitmap bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length, opt);

    Matrix matrix = new Matrix();
    matrix.postRotate(-90);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

From source file:Main.java

public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation,
        final Rect croppingRect) throws IOException {
    byte[] rotated = rotateNV21(data, width, height, rotation);
    final int rotatedWidth = rotation % 180 > 0 ? height : width;
    final int rotatedHeight = rotation % 180 > 0 ? width : height;
    YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    previewImage.compressToJpeg(croppingRect, 80, outputStream);
    byte[] bytes = outputStream.toByteArray();
    outputStream.close();//from   w  ww  . ja  v a 2s .  c o m
    return bytes;
}

From source file:Main.java

public static byte[] createFromNV21(@NonNull final byte[] data, final int width, final int height, int rotation,
        final Rect croppingRect, final boolean flipHorizontal) throws IOException {
    byte[] rotated = rotateNV21(data, width, height, rotation, flipHorizontal);
    final int rotatedWidth = rotation % 180 > 0 ? height : width;
    final int rotatedHeight = rotation % 180 > 0 ? width : height;
    YuvImage previewImage = new YuvImage(rotated, ImageFormat.NV21, rotatedWidth, rotatedHeight, null);

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    previewImage.compressToJpeg(croppingRect, 80, outputStream);
    byte[] bytes = outputStream.toByteArray();
    outputStream.close();//from w  w w  .  j  av a 2  s  . co m
    return bytes;
}

From source file:Main.java

public static Bitmap yuv2Bitmap(byte[] data, int width, int height) {
    final YuvImage image = new YuvImage(data, ImageFormat.NV21, width, height, null);
    ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
    if (!image.compressToJpeg(new Rect(0, 0, width, height), 100, os)) {
        return null;
    }//from w  w w.j av  a  2s  .  c  o  m
    byte[] tmp = os.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
    return bitmap;
}

From source file:Main.java

private static boolean checkAndroidImageFormat(Image image) {
    int format = image.getFormat();
    Plane[] planes = image.getPlanes();/*w w  w.j  a  va 2  s . com*/
    switch (format) {
    case ImageFormat.YUV_420_888:
    case ImageFormat.NV21:
    case ImageFormat.YV12:
        return 3 == planes.length;
    case ImageFormat.RAW_SENSOR:
    case ImageFormat.RAW10:
    case ImageFormat.JPEG:
        return 1 == planes.length;
    default:
        return false;
    }
}

From source file:Main.java

private static boolean checkAndroidImageFormat(Image image) {
    int format = image.getFormat();
    Plane[] planes = image.getPlanes();//from   w  w  w  .ja  v  a  2  s  .  c  o m
    switch (format) {
    case ImageFormat.YUV_420_888:
    case ImageFormat.NV21:
    case ImageFormat.YV12:
        return 3 == planes.length;
    case ImageFormat.RAW_SENSOR:
    case ImageFormat.RAW10:
    case ImageFormat.RAW12:
    case ImageFormat.JPEG:
        return 1 == planes.length;
    default:
        return false;
    }
}