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 BitmapFactory.Options getBounds(byte[] byteArray) {
    BitmapFactory.Options bounds = new BitmapFactory.Options();
    bounds.inJustDecodeBounds = true;/* ww w .  ja va  2  s  . c  om*/
    BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, bounds);
    return bounds;
}

From source file:Main.java

public static String writeBitmap(byte[] data, int cameraDegree, Rect rect, Rect willTransformRect)
        throws IOException {
    File file = new File(Environment.getExternalStorageDirectory() + "/bookclip/");
    file.mkdir();//from  ww w .  j  a va  2s.c om
    String bitmapPath = file.getPath() + "/" + System.currentTimeMillis() + ".png";

    // bitmap rotation, scaling, crop
    BitmapFactory.Options option = new BitmapFactory.Options();
    option.inSampleSize = 2;
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, option);
    Matrix bitmapMatrix = new Matrix();
    bitmapMatrix.setRotate(cameraDegree);
    int x = rect.left, y = rect.top, width = rect.right - rect.left, height = rect.bottom - rect.top;
    Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), bitmapMatrix,
            false);
    // bitmap recycle
    bitmap.recycle();

    Bitmap scaledBitmap = Bitmap.createScaledBitmap(rotateBitmap, willTransformRect.right,
            willTransformRect.bottom - willTransformRect.top, false);
    // rotatebitmap recycle
    rotateBitmap.recycle();

    Bitmap cropBitmap = Bitmap.createBitmap(scaledBitmap, x, y, width, height, null, false);
    // scaledBitmap recycle
    scaledBitmap.recycle();

    // file write
    FileOutputStream fos = new FileOutputStream(new File(bitmapPath));
    cropBitmap.compress(CompressFormat.PNG, 100, fos);
    fos.flush();
    fos.close();

    // recycle
    cropBitmap.recycle();

    return bitmapPath;
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {//  w ww. j av  a  2s.  c  o m
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:Main.java

/**
 * Creates a <tt>Bitmap</tt> from the given image byte array and scales
 * it to the given <tt>width</tt> and <tt>height</tt>.
 *
 * @param imageBytes the raw image data/*from   ww  w  .j  av a 2  s  . c  o  m*/
 * @param reqWidth the width to which to scale the image
 * @param reqHeight the height to which to scale the image
 * @return the newly created <tt>Bitmap</tt>
 */
static public Bitmap scaledBitmapFromBytes(byte[] imageBytes, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options);

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

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

From source file:Main.java

/**
 * Decodes an image byte[] to Bitmap resizing the image to be <code>inSampleSize</code> times smaller then the original.
 * @param imageBytes image content in byte[]
 * @param inSampleSize how much smaller that the image will be, for example, <code>inSampleSize</code> equals 2 will return an image 1/2 the size of the original
 * @return the resized Bitmap//from   w  ww .j  a  va2  s . c om
 */
public static Bitmap decodeSampledBitmapFromByteArray(byte[] imageBytes, int inSampleSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();

    options.inSampleSize = inSampleSize;

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

From source file:Main.java

public static Bitmap decodeInSampleSize(byte[] encodeByte, int reqWidth, int reqHeight) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// w ww. ja  v a  2  s.co  m
    BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length, options);
    options.inSampleSize = calculateSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length, options);
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromBytes(byte[] res, int reqWidth, int reqHeight) {
    if (reqWidth > MAX_WIDTH)
        reqWidth = MAX_WIDTH;//ww w  .  j  ava  2 s .  c o m
    if (reqHeight > MAX_HEIGHT)
        reqHeight = MAX_HEIGHT;

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

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

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

From source file:Main.java

/**
 * Decodes an image byte[] to Bitmap resizing the image to fit the <code>requestedWidth</code> and <code>requestedHeight</code> dimensions.
 * @param imageBytes image content in byte[]
 * @param requestedWidth the final image width will be close to the requestedWidth (value is in pixels)
 * @param requestedHeight the final image height will be close to the requestedHeight (value is in pixels)
 * @return the resized Bitmap/*  ww  w .  j  a  v a2  s.co  m*/
 */
public static Bitmap decodeSampledBitmapFromByteArray(byte[] imageBytes, int requestedWidth,
        int requestedHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options);

    int inSampleSize = calculateSampleSize(options, requestedWidth, requestedHeight);

    return decodeSampledBitmapFromByteArray(imageBytes, inSampleSize);
}

From source file:Main.java

/**
 * Looks through the byte array and calculates size of the picture inside it. Operation is very
 * fast, as no actual decoding will be done.
 *///w w  w .  jav a2 s  . com
public static Point extractSize(byte[] data) {
    final BitmapFactory.Options ops = new BitmapFactory.Options();
    ops.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(data, 0, data.length, ops);

    return new Point(ops.outWidth, ops.outHeight);
}

From source file:Main.java

public static Bitmap load(byte[] data, int inSampleSize) {
    try {/*from www . ja v a 2s.c om*/
        Options opts = new Options();
        opts.inSampleSize = inSampleSize;
        opts.inDither = false;
        opts.inPreferredConfig = Config.ARGB_8888;

        Bitmap result = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
        return result == null ? EMPTY_BITMAP : result;
    } catch (OutOfMemoryError e) {
        Log.e("LoadImage", e.getMessage(), e);
    }
    return EMPTY_BITMAP;
}