Example usage for android.graphics BitmapFactory decodeByteArray

List of usage examples for android.graphics BitmapFactory decodeByteArray

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeByteArray.

Prototype

public static Bitmap decodeByteArray(byte[] data, int offset, int length, Options opts) 

Source Link

Document

Decode an immutable bitmap from the specified byte array.

Usage

From source file:Main.java

public static Bitmap decodeCropBitmapFromByteArray(byte[] byteArray, int x, int y, int width, int height) {

    Bitmap bitmap;/*from w  ww  . j  av  a 2s  .  co m*/
    Bitmap croppedBitmap;
    final BitmapFactory.Options options = new BitmapFactory.Options();

    // First decode with inJustDecodeBounds=true to check dimensions
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    // Enables bitmap re-usage
    options.inMutable = true;

    /* EFFICIENT Crop ? - Creates new Bitmap */// <- Best option?
    bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
    croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
    bitmap.recycle();

    /* EFFICIENT Crop ? - Reuses bitmap, but it is slow */
    //        croppedBitmap = BitmapFactory.decodeResource(res, resId, options);
    //        int[] pixels = getPixelsFromBitmap(croppedBitmap,
    //                x, y,
    //                width, height);
    //        croppedBitmap.setPixels(pixels, 0, width,
    //                0, 0,
    //                width, height);

    return croppedBitmap;
}

From source file:Main.java

/**
 * Gets a bitmap from data.//from   w w w .j  a  va2  s. c  o m
 *
 * @param data data
 * @return Bitmap or null on error
 */
public static Bitmap getBitmap(final byte[] data) {
    if (data != null) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable = true;
            return BitmapFactory.decodeByteArray(data, 0, data.length, options);
        } catch (OutOfMemoryError e) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * <pre>//from w w w . j a  va  2 s.  c  o m
 * Get width and height of bitmap from byte array.
 * </pre>
 * @param bmData bitmap binary data
 * @return integer array with 2 elements: width and height
 */
public static int[] getBitmapSizes(byte[] bmData) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(bmData, 0, bmData.length, bmOptions);
    return new int[] { bmOptions.outWidth, bmOptions.outHeight };
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] bitmapData) {
    if (bitmapData == null)
        return null;
    Options options = new Options();
    options.inJustDecodeBounds = true;//  w  ww  .  j a  v a2  s.c om
    int srcWidth = options.outWidth;
    options.inJustDecodeBounds = false;
    int be = 0;
    be = (int) Math.round(((double) srcWidth) / ((double) 80));
    options = new Options();
    options.inSampleSize = be;
    try {
        return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

/**
 * Checks whether data is valid.//  w  w  w. j  a va2  s.  com
 *
 * @param data image data
 * @return true if data is valid, false otherwise
 */
public static boolean checkBitmap(byte[] data) {
    if (data != null) {
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeByteArray(data, 0, data.length, options);
            if (options.outWidth > MAX_SIZE || options.outHeight > MAX_SIZE) {
                return false;
            } else {
                return options.outWidth > 0 && options.outHeight > 0;
            }
        } catch (Throwable t) {
            // format error if an error has occurred
            return false;
        }
    }
    return false;
}

From source file:Main.java

/**
 * bitmaps.set(/*from   www  . ja v a 2 s .com*/
 * position,
 * BitmapUtils.decodeSampledBitmapFromByteArray(
 * imageBytes,
 * context.getResources().getDimensionPixelSize(R.dimen.width),
 * context.getResources().getDimensionPixelSize(R.dimen.height)));
 */
public static Bitmap decodeSampledBitmapFromByteArray(byte[] byteArray, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options);
}

From source file:Main.java

public static boolean StoreByteImage(Context mContext, byte[] imageData, int quality, String expName) {

    File sdImageMainDirectory = new File("/sdcard/myImages");
    FileOutputStream fileOutputStream = null;
    String nameFile = "";
    try {//from w  w  w.j  ava  2 s . co  m

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 5;

        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);

        fileOutputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/" + nameFile + ".jpg");

        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return true;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    // Calculate inSampleSize
    final Options options = new Options();
    options.inSampleSize = calculateInSampleSize(bitmap.getWidth(), bitmap.getHeight(), reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;//  w  ww  .j  a v a  2 s. c  om
    byte[] data = bitmap2Bytes(bitmap);
    if (data != null) {
        bitmap.recycle();
        return BitmapFactory.decodeByteArray(data, 0, data.length, options);
    } else {
        return bitmap;
    }
}

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  ww .  j a va  2  s  .c  o m*/
    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

/**
 * Decode a byte array to a Bitmap with a process requiring a low amount of memory
 * @param bt image as bytes/*  w w  w .  j a v  a 2 s. c  o  m*/
 * @return image as bitmap
 */
public static Bitmap LowMemBitmapDecoder(byte[] bt) {
    Bitmap bm = null;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeByteArray(bt, 0, bt.length, options);

    return bm;
}