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 Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp,
        boolean fitInScreen) {
    if (fitInScreen) {
        source = scaleTo(scaler, source, targetWidth, targetHeight, true);
    }//ww w  . ja va2s  .c  om
    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if ((!scaleUp || fitInScreen) && (deltaX < 0 || deltaY < 0)) {
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b2);

        int deltaXHalf = Math.max(0, deltaX / 2);
        int deltaYHalf = Math.max(0, deltaY / 2);
        Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()),
                deltaYHalf + Math.min(targetHeight, source.getHeight()));
        int dstX = (targetWidth - src.width()) / 2;
        int dstY = (targetHeight - src.height()) / 2;
        Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
        c.drawBitmap(source, src, dst, null);
        source.recycle();
        return b2;
    }

    Bitmap b1 = scaleTo(scaler, source, targetWidth, targetHeight, false);

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);
    b1.recycle();

    return b2;
}

From source file:Main.java

/**
 * Returns semi-rounded bitmap. This is used for displaying atn promotion images.
 * /*from www  .  java  2s .  com*/
 * @param context
 * @param input
 * @return
 */
public static Bitmap getSemiRoundedBitmap(Context context, Bitmap input) {
    Bitmap output = Bitmap.createBitmap(input.getWidth(), input.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, input.getWidth(), input.getHeight());
    final RectF rectF = new RectF(rect);
    //make sure that our rounded corner is scaled appropriately
    final float roundPx = densityMultiplier * 10;
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    //draw rectangles over the corners we want to be square
    canvas.drawRect(input.getWidth() / 2, 0, input.getWidth(), input.getHeight() / 2, paint);
    canvas.drawRect(input.getWidth() / 2, input.getHeight() / 2, input.getWidth(), input.getHeight(), paint);
    paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, 0, 0, paint);
    input.recycle();

    return output;
}

From source file:Main.java

/**
 * Transform source Bitmap to targeted width and height.
 *//* w w  w .  ja v a2s  .com*/
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight,
        boolean recycle) {

    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    if (bitmapAspect > viewAspect) {
        float scale = targetHeight / bitmapHeightF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    } else {
        float scale = targetWidth / bitmapWidthF;
        if (scale < .9F || scale > 1F) {
            scaler.setScale(scale, scale);
        } else {
            scaler = null;
        }
    }

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
    } else {
        b1 = source;
    }

    if (recycle && b1 != source) {
        source.recycle();
    }

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight, null, true);

    if (b2 != b1) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:Main.java

public static Bitmap scaleBitmapForDevice(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/* ww  w . j  av  a 2 s.  com*/

    float density = Resources.getSystem().getDisplayMetrics().density;
    int newWidth = (int) (bitmap.getWidth() * density);
    int newHeight = (int) (bitmap.getHeight() * density);
    /*
    Bitmap resizeBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
     */
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));
    bitmap.recycle();

    return scaledBitmap;
}

From source file:biz.varkon.shelvesom.util.ImageUtilities.java

private static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, float offset,
        boolean clipShadow, Paint paint) {
    Matrix m;/*from  ww  w .  j  a v a2  s .  co  m*/
    synchronized (Bitmap.class) {
        m = sScaleMatrix;
        sScaleMatrix = null;
    }

    if (m == null) {
        m = new Matrix();
    }

    final int width = src.getWidth();
    final int height = src.getHeight();
    final float sx = dstWidth / (float) width;
    final float sy = dstHeight / (float) height;
    m.setScale(sx, sy);

    Bitmap b = createBitmap(src, 0, 0, width, height, m, offset, clipShadow, paint);

    synchronized (Bitmap.class) {
        sScaleMatrix = m;
    }

    src.recycle();
    return b;
}

From source file:com.bamobile.fdtks.util.Tools.java

/**
 * Create a new bitmap based on a dummy empty image.
 * This method avoid the expensive Bitmap method Bitmap.createBitmap
 *
 * @param width//from  www  .ja  v a  2  s  .  c o m
 * @param height
 * @return
 */
public static Bitmap createBitmap(int width, int height) {
    Bitmap bmp = BitmapFactory.decodeResource(MainActivity.getInstance().getResources(), R.drawable.empty_image,
            null);
    Bitmap resultBitmap = Bitmap.createScaledBitmap(bmp, width, height, false);
    bmp.recycle();
    bmp = null;
    return resultBitmap;
}

From source file:com.ruesga.rview.misc.BitmapUtils.java

@SuppressWarnings("ResultOfMethodCallIgnored")
public static File optimizeImage(Context context, InputStream source, Bitmap.CompressFormat format, int quality)
        throws IOException {
    // Copy original source to a temp file
    File f = null;// ww w . j a  v a  2 s. c om
    OutputStream os = null;
    try {
        f = CacheHelper.createNewTemporaryFile(context, "." + format.name());
        if (f != null) {
            os = new BufferedOutputStream(new FileOutputStream(f), 4096);
            IOUtils.copy(source, os);
            IOUtils.closeQuietly(os);

            // Compress to webp format
            long start = System.currentTimeMillis();
            int[] size = decodeBitmapSize(f);
            if (size[0] != -1 && size[1] != -1) {
                Rect r = new Rect(0, 0, size[0], size[1]);
                adjustRectToMinimumSize(r, calculateMaxAvailableSize(context));
                Bitmap src = createUnscaledBitmap(f, r.width(), r.height());

                try {
                    long originalSize = f.length();
                    os = new BufferedOutputStream(new FileOutputStream(f), 4096);
                    src.compress(format, quality, os);
                    IOUtils.closeQuietly(os);

                    long end = System.currentTimeMillis();
                    Log.d(TAG,
                            "Compressed " + f + " to " + format.name() + " in " + (end - start) + "ms"
                                    + "; dimensions " + src.getWidth() + "x" + src.getHeight() + "; size: "
                                    + f.length() + "; original: " + originalSize);
                    return f;

                } finally {
                    src.recycle();
                }
            } else {
                f.delete();
            }
        }
    } catch (IOException ex) {
        IOUtils.closeQuietly(os);
        if (f != null) {
            f.delete();
        }
        throw ex;
    } finally {
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(source);
    }

    return null;
}

From source file:com.c4mprod.utils.ImageManager.java

public static Bitmap shrinkBitmap(InputStream stream, int width, int height) {
    try {/*from  www. j a v a2s .c  o  m*/
        // TODO check for a better solution for handling purgation
        BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeStream(stream, null, bmpFactoryOptions);
        computeRatio(width, height, bmpFactoryOptions);

        if (bitmap != null) {
            final int ratio = bmpFactoryOptions.inSampleSize;
            Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bmpFactoryOptions.outWidth / ratio,
                    bmpFactoryOptions.outHeight / ratio, false);
            if (scaledBitmap != bitmap) {
                bitmap.recycle();
            }
            return scaledBitmap;
        }
        return null;
    } catch (OutOfMemoryError e) {
        // Log.d("ImprovedImageDownloader.shrinkBitmap():", "memory !");
        return null;
    }
}

From source file:edu.stanford.mobisocial.dungbeetle.obj.action.ExportPhotoAction.java

@Override
public void onAct(Context context, DbEntryHandler objType, DbObj obj) {
    byte[] raw = obj.getRaw();
    if (raw == null) {
        String b64Bytes = obj.getJson().optString(PictureObj.DATA);
        raw = FastBase64.decode(b64Bytes);
    }/*from w w  w.j av a 2  s.co  m*/
    OutputStream outStream = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp_share.png");
    try {
        outStream = new FileOutputStream(file);

        BitmapManager mgr = new BitmapManager(1);
        Bitmap bitmap = mgr.getBitmap(raw.hashCode(), raw);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();

        bitmap.recycle();
        bitmap = null;
        System.gc();
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setType("image/png");
        Log.w("ResharePhotoAction",
                Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp_share.png");
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        context.startActivity(Intent.createChooser(intent, "Export image to"));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.charbgr.BlurNavigationDrawer.v4.BlurActionBarDrawerToggle.java

private void handleRecycle() {
    Drawable drawable = mBlurredImageView.getDrawable();

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
        Bitmap bitmap = bitmapDrawable.getBitmap();

        if (bitmap != null)
            bitmap.recycle();

        mBlurredImageView.setImageBitmap(null);
    }/*from w w  w  .j  a va2s  .c o  m*/

    prepareToRender = true;
}