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

/**
 * Copy the red channel to a new Bitmap's alpha channel.  The old
 * one will be recycled.// w ww  .jav a 2s. com
 */
static Bitmap redToAlpha(Bitmap src, boolean recycle) {
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

    float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix)));

    Canvas canvas = new Canvas(dest);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    canvas.drawBitmap(src, 0, 0, paint);

    if (recycle)
        src.recycle();
    return dest;
}

From source file:Main.java

/**
 * save the bitmap to local,add give a white bg color
 * @param bitmap/*from w  ww.  ja v a2 s .c  o m*/
 * @param path
 * @return
 */
public static boolean saveBitmapNoBgToSdCard(Bitmap bitmap, String path) {
    BufferedOutputStream bos = null;
    try {
        File file = new File(path);
        if (file.exists())
            file.delete();
        bos = new BufferedOutputStream(new FileOutputStream(file));

        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        int w_new = w;
        int h_new = h;
        Bitmap resultBitmap = Bitmap.createBitmap(w_new, h_new, Bitmap.Config.ARGB_8888);
        //            Paint paint = new Paint();
        //            paint.setColor(Color.WHITE);
        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawColor(Color.WHITE);
        canvas.drawBitmap(bitmap, new Rect(0, 0, w, h), new Rect(0, 0, w_new, h_new), null);
        resultBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
        bos.flush();
        resultBitmap.recycle();
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

From source file:Main.java

public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    RenderScript rs = RenderScript.create(context);
    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
    Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
    blurScript.setRadius(8f);//from   ww w  .j a va 2  s .c  o  m
    blurScript.setInput(allIn);
    blurScript.forEach(allOut);
    allOut.copyTo(outBitmap);
    bitmap.recycle();
    rs.destroy();
    return outBitmap;
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)// www  .j  av a  2 s . c  o m
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap getHistogram(Bitmap bmpOriginal) {

    //Scale bmpOriginal to improve performance 
    final int dstWidth = 200;
    int dstHeight = dstWidth * bmpOriginal.getHeight() / bmpOriginal.getWidth();
    Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOriginal, dstWidth, dstHeight, false);
    int[] histogramValues = new int[256];

    int[] pixels = new int[dstWidth];
    int pxBrightness;
    for (int row = 0; row < dstHeight; row++) {
        bmpScaled.getPixels(pixels, 0, dstWidth, 0, row, dstWidth, 1);
        for (int col = 0; col < dstWidth; col++) {
            pxBrightness = rgbToGray(pixels[col]);
            histogramValues[pxBrightness]++;
        }//from w w w.  j  a v  a 2  s  . co m
    }
    bmpScaled.recycle();

    int histogramMax = max(histogramValues);
    Bitmap bmpHistogram = Bitmap.createBitmap(256, histogramMax, Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpHistogram);
    Paint paint = new Paint();
    paint.setColor(Color.CYAN);

    for (int i = 0; i < 256; i++)
        canvas.drawLine(i, histogramMax - histogramValues[i], i, histogramMax, paint);

    return bmpHistogram;
}

From source file:com.exzogeni.dk.graphics.Bitmaps.java

@Nullable
private static Bitmap applyExif(@NonNull Bitmap bitmap, String exifFilePath) {
    final int orientation = getExifOrientation(exifFilePath);
    if (orientation == ExifInterface.ORIENTATION_NORMAL || orientation == ExifInterface.ORIENTATION_UNDEFINED) {
        return bitmap;
    }/* w ww. j av  a2  s.  c  om*/
    try {
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(),
                getExifMatrix(orientation), true);
    } finally {
        bitmap.recycle();
    }
}

From source file:Main.java

public static boolean putBitmapToFile(Bitmap bitmap, File file) {
    if (bitmap == null || file == null)
        return false;
    FileOutputStream fos = null;/*from   w  w  w  .jav  a2 s .  c om*/
    try {
        fos = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        return true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (fos != null)
            try {
                fos.flush();
                fos.close();
                if (!bitmap.isRecycled())
                    bitmap.recycle();
            } catch (IOException ignored) {
            }
    }
    return false;
}

From source file:Main.java

public static Bitmap toCircleBitmap(Bitmap bitmap) {

    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int r = width < height ? width : height;

    Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(buffer);
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from   w w w .ja  v  a  2 s.  c om
    RectF rect = new RectF(0, 0, r, r);

    canvas.drawCircle(r / 2, r / 2, r / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    bitmap.recycle();
    return buffer;
}

From source file:com.allen.mediautil.ImageTakerHelper.java

/**
 * ?//from  w  ww.j  a va2  s.  co m
 * <p>
 * ??
 *
 * @param originalImagePath 
 * @param destDir           ?
 * @param saveName          ????? 
 * @return ???
 */
public static String saveAccountImg(String originalImagePath, File destDir, String saveName) {
    Bitmap.CompressFormat format = IMG_TEMP_FORMAT;
    int maxSize = MAX_IMAGE_SIZE;
    int thumbnailMaxSize = MAX_THUMB_IMAGE_SIZE;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(originalImagePath, options);
    int scale = 1;
    int size = Math.max(options.outWidth, options.outHeight);
    while (size / scale > maxSize) {
        scale *= 2;
    }
    int degree = readPictureDegree(originalImagePath);
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    Bitmap bm = BitmapFactory.decodeFile(originalImagePath, options);

    if (degree != 0) {
        Bitmap bp = rotateImageView(degree, bm);
        if (bp != bm) {
            bm.recycle();
        }
        bm = bp;
    }

    // ??
    File imageSavePos = new File(destDir, getImageSaveName(saveName, format, false));
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(imageSavePos);
        bm.compress(format, 100, fos);
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeSilent(fos);
    }
    bm.recycle();

    // ?
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(originalImagePath, options);
    scale = 1;
    size = Math.max(options.outWidth, options.outHeight);
    while (size / scale > thumbnailMaxSize) {
        scale *= 2;
    }
    options.inJustDecodeBounds = false;
    options.inSampleSize = scale;
    Bitmap thumb = BitmapFactory.decodeFile(originalImagePath, options);
    try {
        fos = new FileOutputStream(new File(destDir, getImageSaveName(saveName, format, true)));
        thumb.compress(format, 100, fos);
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        closeSilent(fos);
    }
    thumb.recycle();
    return imageSavePos.getAbsolutePath();
}

From source file:Main.java

public static Drawable get_scaled_drawable_from_uri_string_for_square_container(Context context, String uri,
        int max_size) {
    Drawable drawable = null;/*from w  w  w. ja va  2  s . c  om*/
    Uri img_uri = Uri.parse(uri);
    if (uri != null) {
        try {

            InputStream inputStream = context.getContentResolver().openInputStream(img_uri);
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            int sx = bitmap.getWidth();
            int sy = bitmap.getHeight();

            int fsx = max_size;
            int fsy = max_size;

            if (sy > sx)
                fsx = (int) ((float) max_size * ((float) sx / (float) sy));
            else if (sx > sy)
                fsy = (int) ((float) max_size * ((float) sy / (float) sx));

            Bitmap small_bitmap = Bitmap.createScaledBitmap(bitmap, fsx, fsy, false);
            bitmap.recycle();
            drawable = new BitmapDrawable(context.getResources(), small_bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    return drawable;
}