Example usage for android.graphics Bitmap recycle

List of usage examples for android.graphics Bitmap recycle

Introduction

In this page you can find the example usage for android.graphics Bitmap recycle.

Prototype

public void recycle() 

Source Link

Document

Free the native object associated with this bitmap, and clear the reference to the pixel data.

Usage

From source file:Main.java

public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }/* w  ww .ja  va  2s  . com*/

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static String openAsBase64(String imgPath) {
    Bitmap photo = BitmapFactory.decodeFile(imgPath);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 85, baos);
    byte[] b = baos.toByteArray();
    photo.recycle();
    return Base64.encodeToString(b, Base64.DEFAULT);

}

From source file:Main.java

private static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }//from w w w. j  a va2 s.c om

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
    }

    return result;
}

From source file:Main.java

public static int[] getImageSize(Context context, int drawableId) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), drawableId);
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (bitmap != null && bitmap.isRecycled()) {
        bitmap.recycle();
    }/*from  www  .j  a va  2s  . co  m*/
    int size[] = { width, height };
    return size;
}

From source file:Main.java

public static Bitmap crop(Bitmap bitmap, int x, int y, int width, int height, boolean recycle)
        throws OutOfMemoryError {
    Bitmap output = Bitmap.createBitmap(bitmap, x, y, width, height);
    if (recycle && bitmap != output) {
        bitmap.recycle();
    }/*w  w  w .  ja  va2  s .  c  o  m*/
    return output;
}

From source file:Main.java

public static byte[] bitmap2ByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }/* ww w  .j  a  v  a2  s  . c  om*/

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public static byte[] Bitmap2Bytes(Bitmap bm) {
    if (bm == null) {
        return null;
    }//  w  w w.j  a  v a2  s . c om
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(CompressFormat.PNG, 100, baos);

    if (bm != null) {
        bm.recycle();
        bm = null;
    }

    return baos.toByteArray();
}

From source file:Main.java

public static Bitmap createBitmap(String paramString, int paramInt1, int paramInt2) {

    Bitmap localBitmap1 = BitmapFactory.decodeFile(paramString);
    Bitmap localBitmap2 = null;/* w w w. java2 s  .  co  m*/

    if (localBitmap1 != null) {
        localBitmap2 = Bitmap.createScaledBitmap(localBitmap1, paramInt1, paramInt2, true);
        localBitmap1.recycle();
    }

    return localBitmap2;
}

From source file:Main.java

/**
 * Special crop of bitmap rotated by not stright angle, in this case the original crop bitmap contains parts
 * beyond the required crop area, this method crops the already cropped and rotated bitmap to the final
 * rectangle.<br>/*from ww  w  .  j a v  a 2  s.c o  m*/
 * Note: rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping.
 */
private static Bitmap cropForRotatedImage(Bitmap bitmap, float[] points, Rect rect, int degreesRotated,
        boolean fixAspectRatio, int aspectRatioX, int aspectRatioY) {
    if (degreesRotated % 90 != 0) {

        int adjLeft = 0, adjTop = 0, width = 0, height = 0;
        double rads = Math.toRadians(degreesRotated);
        int compareTo = degreesRotated < 90 || (degreesRotated > 180 && degreesRotated < 270) ? rect.left
                : rect.right;
        for (int i = 0; i < points.length; i += 2) {
            if (points[i] >= compareTo - 1 && points[i] <= compareTo + 1) {
                adjLeft = (int) Math.abs(Math.sin(rads) * (rect.bottom - points[i + 1]));
                adjTop = (int) Math.abs(Math.cos(rads) * (points[i + 1] - rect.top));
                width = (int) Math.abs((points[i + 1] - rect.top) / Math.sin(rads));
                height = (int) Math.abs((rect.bottom - points[i + 1]) / Math.cos(rads));
                break;
            }
        }

        rect.set(adjLeft, adjTop, adjLeft + width, adjTop + height);
        if (fixAspectRatio) {
            fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
        }

        Bitmap bitmapTmp = bitmap;
        bitmap = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height());
        if (bitmapTmp != bitmap) {
            bitmapTmp.recycle();
        }
    }
    return bitmap;
}

From source file:Main.java

public static byte[] bmpToByteArray(Bitmap bitmap, boolean flag) {
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, bytearrayoutputstream);
    if (flag)/*  w  w w  .ja va2  s.c om*/
        bitmap.recycle();
    byte abyte0[] = bytearrayoutputstream.toByteArray();
    try {
        bytearrayoutputstream.close();
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return abyte0;
}