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 getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap bitmap1 = null;//from  w w w. ja  v  a  2  s . com
    if (bitmap != null) {
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectf = new RectF(rect);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(0xffff0000);
        canvas.drawRoundRect(rectf, 15F, 15F, paint);
        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        bitmap.recycle();
    }
    return bitmap1;
}

From source file:Main.java

public static Bitmap circleBitmap(final Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*  ww  w  .ja  va2 s .c  o  m*/
    paint.setColor(Color.WHITE);

    Bitmap clipped = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(clipped);
    final float radius = width > height ? height / 2 : width / 2;
    canvas.drawCircle(width / 2, height / 2, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));

    Bitmap rounded = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    canvas = new Canvas(rounded);
    canvas.drawBitmap(source, 0, 0, null);
    canvas.drawBitmap(clipped, 0, 0, paint);

    source.recycle();
    clipped.recycle();

    return rounded;
}

From source file:Main.java

static Bitmap decodeBitmap(Context context, Uri uri, BitmapFactory.Options options, int maxW, int maxH,
        int orientation, int pass) {

    Bitmap bitmap = null;
    Bitmap newBitmap = null;//  w  w  w .j a  va  2 s .c  om

    if (pass > 20) {
        return null;
    }

    InputStream stream = openInputStream(context, uri);
    if (null == stream)
        return null;

    try {
        // decode the bitmap via android BitmapFactory
        bitmap = BitmapFactory.decodeStream(stream, null, options);
        closeSilently(stream);

        if (bitmap != null) {
            newBitmap = resizeBitmap(bitmap, maxW, maxH, orientation);
            if (bitmap != newBitmap) {
                bitmap.recycle();
            }
            bitmap = newBitmap;
        }

    } catch (OutOfMemoryError error) {
        closeSilently(stream);
        if (null != bitmap) {
            bitmap.recycle();
        }
        options.inSampleSize += 1;
        bitmap = decodeBitmap(context, uri, options, maxW, maxH, orientation, pass + 1);
    }
    return bitmap;

}

From source file:Main.java

public static Bitmap createCroppedScaleBitmap(Bitmap src, int reqWidth, int reqHeight) {
    final int bWidth = src.getWidth();
    final int bHeight = src.getHeight();
    Matrix matrix = new Matrix();
    int maxSize = Math.max(reqHeight, reqWidth);
    float scaleX;
    if (bWidth * bHeight < reqWidth * reqHeight)
        scaleX = 0;//from  ww  w .j  av a2 s  .  co m
    else {
        if (bWidth > bHeight) {
            scaleX = (float) maxSize / bWidth;
        } else
            scaleX = (float) maxSize / bHeight;
    }
    Bitmap sourceBitmap;
    if (scaleX > 0 && scaleX != 1) {
        matrix.setScale(scaleX, scaleX);
        sourceBitmap = Bitmap.createBitmap(src, 0, 0, bWidth, bHeight, matrix, true);
        if (sourceBitmap != src && !src.isRecycled())
            src.recycle();
    } else
        sourceBitmap = src;
    return sourceBitmap;
}

From source file:Main.java

public static int saveToSdCard(String fileName, Bitmap bitmap) {
    int ret = 0;//from ww  w  .  j ava2s . c  om
    PrintStream out = null;
    if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        return -1;
    }
    File file = new File(Environment.getExternalStorageDirectory().toString() + File.separator + fileName);
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdir();
    }
    try {
        out = new PrintStream(new FileOutputStream(file));
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        ret = -2;
    } finally {
        out.flush();
        out.close();
        if (!bitmap.isRecycled())
            bitmap.recycle();
    }

    return ret;
}

From source file:com.balieiro.facebook.FriendItem.java

/**
  * The credit of the image processing performed by this method belongs
  * to the author of this site://from  w w w. j  av  a  2  s.com
  * http://www.piwai.info/transparent-jpegs-done-right/
  * There is an amazing explanation on how to perform this kind of 
  * images transformations.
  */
private static Bitmap getRoundedBitmap(Bitmap source, int pictureMask) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Starting with Honeycomb, we can load the bitmap as mutable.
        options.inMutable = true;
    }
    // We could also use ARGB_4444, but not RGB_565 (we need an alpha layer).
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap;
    if (source.isMutable()) {
        bitmap = source;
    } else {
        bitmap = source.copy(Bitmap.Config.ARGB_8888, true);
        source.recycle();
    }
    // The bitmap is opaque, we need to enable alpha compositing.
    bitmap.setHasAlpha(true);

    Canvas canvas = new Canvas(bitmap);
    Bitmap mask = BitmapFactory.decodeResource(MyFacebookApp.getContext().getResources(), pictureMask);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(mask, 0, 0, paint);
    // We do not need the mask bitmap anymore.
    mask.recycle();

    return bitmap;
}

From source file:Main.java

public static Bitmap makeSquare(File file, int cameraID) {
    int width;/* w  w w.java 2  s  .  co m*/
    int height;
    Matrix matrix = new Matrix();
    Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraID, info);
    // Convert ByteArray to Bitmap

    Bitmap bitPic = decodeSampledBitmapFromFile(destinationFile.getAbsolutePath(), 800, 800);//BitmapFactory.decodeByteArray(data, 0, data.length);
    width = bitPic.getWidth();
    height = bitPic.getHeight();

    // Perform matrix rotations/mirrors depending on camera that took the photo
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1 };
        Matrix matrixMirrorY = new Matrix();
        matrixMirrorY.setValues(mirrorY);

        matrix.postConcat(matrixMirrorY);
    }

    matrix.postRotate(90);

    // Create new Bitmap out of the old one
    Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height, matrix, true);
    bitPic.recycle();
    int desWidth;
    int desHeight;
    desWidth = bitPicFinal.getWidth();
    desHeight = desWidth;
    Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,
            bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2, desWidth, desHeight);
    croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
    return croppedBitmap;
}

From source file:Main.java

/**
 * clip source bitmap into a circle bitmap
 *
 * @param src     the source bitmap/* w  w w  . ja v  a 2  s.c om*/
 * @param recycle whether recycle the source bitmap
 * @param config  bitmap config
 * @return clipped  circle bitmap
 */
public static Bitmap createCircleBitmap(Bitmap src, boolean recycle, Bitmap.Config config) {
    if (src == null) {
        return null;
    }
    if (config == null) {
        config = Bitmap.Config.ARGB_8888;
    }
    Bitmap out = Bitmap.createBitmap(src.getWidth(), src.getHeight(), config);
    final Rect rect = new Rect(0, 0, Math.min(src.getWidth(), src.getHeight()),
            Math.min(src.getWidth(), src.getHeight()));
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    Canvas canvas = new Canvas(out);
    canvas.drawARGB(0, 0, 0, 0);
    RectF rectF = new RectF(rect);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(src, rect, rect, paint);

    if (recycle) {
        src.recycle();
    }

    return out;

}

From source file:Main.java

public static Bitmap getBitmapByFixingRotationForFile(String filePath, Bitmap sourceBitmap,
        Activity activityForScreenOrientation, boolean freeSourceBitmap) {
    try {/*from   w  w w. j ava  2s. c  o  m*/
        ExifInterface exif = new ExifInterface(filePath);
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        if (orientation == ExifInterface.ORIENTATION_UNDEFINED
                && Build.MANUFACTURER.toLowerCase(Locale.ENGLISH).contains("htc"))
            return null;

        boolean flippedHorizontally = false, flippedVertically = false;

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle += 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle += 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle += 270;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_HORIZONTAL) {
            flippedHorizontally = true;
        } else if (orientation == ExifInterface.ORIENTATION_FLIP_VERTICAL) {
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSPOSE) {
            angle += 90;
            flippedVertically = true;
        } else if (orientation == ExifInterface.ORIENTATION_TRANSVERSE) {
            angle -= 90;
            flippedVertically = true;
        }

        if (activityForScreenOrientation != null) {
            orientation = getScreenOrientation(activityForScreenOrientation);
            if (orientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                angle += 90;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                angle += 180;
            } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
                angle += 270;
            }
        }

        Bitmap bmp = sourceBitmap;
        if (bmp == null) {
            bmp = BitmapFactory.decodeFile(filePath, null);
        }
        if (angle != 0) {
            Matrix mat = new Matrix();
            mat.postRotate(angle);

            if (flippedHorizontally) {
                mat.postScale(-1.f, 1.f);
            }
            if (flippedVertically) {
                mat.postScale(1.f, -1.f);
            }

            Bitmap rotated = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
            if (freeSourceBitmap || bmp != sourceBitmap) {
                bmp.recycle();
            }
            bmp = rotated;
        }

        return bmp;

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }

    return null;
}

From source file:Main.java

public static Bitmap circleBitmap(Bitmap source, boolean recycle) {
    if (source == null) {
        return null;
    }//from  w  w w .  ja  va 2s.  co  m

    // Create custom bitmap
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // Compute sizes
    final int color = Color.RED;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(source, rect, rect, paint);

    if (recycle) {
        source.recycle();
    }

    return output;
}