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

/**
 * Converts Byte array into Bitmap//from   w  w w .j  av a  2  s  .  co  m
 * @param bytes byte[]
 * @return Bitmap
 */
public static Bitmap convertByteArrayToBitmap(byte[] bytes) {
    if (bytes != null) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    }
    return null;
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] bitmapData, int size) {
    if (bitmapData == null)
        return null;
    Options options = new Options();
    options.inJustDecodeBounds = true;/*from   w ww .ja va2s . com*/
    BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    int w = options.outWidth;
    int h = options.outHeight;
    int sc = 0;
    if (w > size || h > size)
        if (w > h) {
            sc = Math.round((float) w / (float) size);
        } else {
            sc = Math.round((float) h / (float) size);
        }
    options.inJustDecodeBounds = false;
    options.inSampleSize = sc;
    try {
        return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap makeBitmap(byte[] jpegData, int maxNumOfPixels) {
    try {/*from w w  w.j  a va2  s  . c o m*/
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);
        if (options.mCancel || options.outWidth == -1 || options.outHeight == -1) {
            return null;
        }
        options.inSampleSize = computeSampleSize(options, -1, maxNumOfPixels);
        options.inJustDecodeBounds = false;

        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);
    } catch (OutOfMemoryError ex) {
        Log.e(TAG, "Got oom exception ", ex);
        return null;
    }
}

From source file:Main.java

public static Bitmap decodeSamplerBitmap(byte[] data, int reqWidth, int reqHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from  w w  w. ja va 2s  .  com
    BitmapFactory.decodeByteArray(data, 0, data.length, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}

From source file:Main.java

public static Bitmap decode(byte[] data, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  ww  w  . j a  v  a  2s . c o m
    BitmapFactory.decodeByteArray(data, 0, data.length, options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
}

From source file:Main.java

/**
 * Decodes the bitmap with the given sample size
 *///from  w  w  w  .j a  v  a2s .co  m
public static Bitmap decodeBitmapFromBytes(byte[] bytes, int sampleSize) {
    final BitmapFactory.Options options;
    if (sampleSize <= 1) {
        options = null;
    } else {
        options = new BitmapFactory.Options();
        options.inSampleSize = sampleSize;
    }
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
}

From source file:Main.java

/**
 * Returns Width or Height of the picture, depending on which size is smaller. Doesn't actually
 * decode the picture, so it is pretty efficient to run.
 *///from ww w . ja v  a 2s  .c om
public static int getSmallerExtentFromBytes(byte[] bytes) {
    final BitmapFactory.Options options = new BitmapFactory.Options();

    // don't actually decode the picture, just return its bounds
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);

    // test what the best sample size is
    return Math.min(options.outWidth, options.outHeight);
}

From source file:Main.java

public static Bitmap decodeByteArray(byte[] buffer, int reqWidth, int reqHeight) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    // Determine insample size
    opts.inJustDecodeBounds = true;//from w ww.ja  v a  2s  . c om
    BitmapFactory.decodeByteArray(buffer, 0, buffer.length, opts);
    opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight);

    // Decode the bitmap, regionally if necessary
    Bitmap bitmap = null;
    opts.inJustDecodeBounds = false;
    Rect rect = getCropRectIfNecessary(opts, reqWidth, reqHeight);
    try {
        if (rect != null) {
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(buffer, 0, buffer.length, false);
            bitmap = decoder.decodeRegion(rect, opts);
        } else {
            bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length, opts);
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to decode bitmap from stream", e);
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeByteArray(byte[] bytes, int offset, int length, BitmapFactory.Options options) {
    if (bytes.length <= 0) {
        throw new IllegalArgumentException("bytes.length " + bytes.length + " must be a positive number");
    }//  www.  j a  va 2s  .  c  o m

    Bitmap bitmap = null;
    try {
        bitmap = BitmapFactory.decodeByteArray(bytes, offset, length, options);
    } catch (OutOfMemoryError e) {
        Log.e(LOGTAG, "decodeByteArray(bytes.length=" + bytes.length + ", options= " + options + ") OOM!", e);
        return null;
    }

    if (bitmap == null) {
        Log.w(LOGTAG, "decodeByteArray() returning null because BitmapFactory returned null");
        return null;
    }

    if (bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) {
        Log.w(LOGTAG, "decodeByteArray() returning null because BitmapFactory returned "
                + "a bitmap with dimensions " + bitmap.getWidth() + "x" + bitmap.getHeight());
        return null;
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap loadBitmap(String url) {
    Log.d(TAG, "Start Load Url : " + url);
    //Bitmap bitmap = null;
    InputStream in = null;/*w  ww  . j  a  va 2  s. c  o m*/
    BufferedOutputStream out = null;
    try {
        in = new BufferedInputStream(new URL(url).openStream());
        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];

        while ((nRead = in.read(data, 0, data.length)) != -1) {
            dataStream.write(data, 0, nRead);
        }
        dataStream.flush();
        final byte[] newData = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        bitmapResult = BitmapFactory.decodeByteArray(newData, 0, newData.length, options);
    } catch (IOException e) {
        Log.e("My fault", "Could not load Bitmap from: " + url);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return bitmapResult;
}