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 getMatrixBitmap(Bitmap bm, int w, int h, boolean needRecycle) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    boolean isCompress = (width > w && height > h) && (w != 0 && h != 0);
    if (isCompress) {
        float scaleWidth = ((float) w) / width;
        float scaleHeight = ((float) h) / height;
        float scale = Math.max(scaleWidth, scaleHeight);
        Matrix matrix = new Matrix();
        matrix.postScale(scale, scale);//from w w w.j a v  a2 s.  com
        Bitmap bitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        if (needRecycle && bm != null && bm != bitmap) {
            bm.recycle();
        }
        return bitmap;
    } else {
        return bm;
    }

}

From source file:Main.java

public static void loadTexture(final Context context, int resId, GL10 gl, int[] textures) {

    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId);

    gl.glGenTextures(1, textures, 0);// www. j av a 2s.co m
    gl.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);

    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);
    bmp.recycle();

}

From source file:Main.java

private static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);//from  w w w  .  j  a v a2s  .  c o  m
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();
    }
    return target;
}

From source file:Main.java

private static byte[] getBitmapBytes(Bitmap bitmap, boolean paramBoolean, int width, int heigth) {
    Bitmap localBitmap = Bitmap.createBitmap(width, heigth, Bitmap.Config.RGB_565);
    Canvas localCanvas = new Canvas(localBitmap);
    int i;// w  w  w . j a  v  a2  s.  c o m
    int j;
    if (bitmap.getHeight() > bitmap.getWidth()) {
        i = bitmap.getWidth();
        j = bitmap.getWidth();
    } else {
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
    while (true) {
        localCanvas.drawBitmap(bitmap, new Rect(0, 0, i, j), new Rect(0, 0, width, heigth), null);
        if (paramBoolean)
            bitmap.recycle();
        ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
        localBitmap.compress(Bitmap.CompressFormat.JPEG, 100, localByteArrayOutputStream);
        localBitmap.recycle();
        byte[] arrayOfByte = localByteArrayOutputStream.toByteArray();
        try {
            localByteArrayOutputStream.close();
            return arrayOfByte;
        } catch (Exception e) {
            e.printStackTrace();
        }
        i = bitmap.getHeight();
        j = bitmap.getHeight();
    }
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(final Bitmap bitmap, final float scale, final boolean recycle) {
    final int width = Math.round(bitmap.getWidth() * scale);
    final int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    final Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    final Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);//from   ww w.j  a  v a2s . co  m
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle) {
        bitmap.recycle();
    }
    return target;
}

From source file:Main.java

public static Bitmap resize(final Bitmap b, final int newWidth, final int newHeight) {
    if (b == null) {
        return null;
    }/*from  ww  w  . j a va  2  s.co m*/

    int oldWidth = b.getWidth();
    int oldHeight = b.getHeight();
    if (oldWidth == newWidth && oldHeight == newHeight) {
        return b;
    }
    float scaleWidth = ((float) newWidth) / oldWidth;
    float scaleHeight = ((float) newHeight) / oldHeight;
    float scaleFactor = Math.min(scaleWidth, scaleHeight);
    Matrix scale = new Matrix();
    scale.postScale(scaleFactor, scaleFactor);

    Bitmap result = Bitmap.createBitmap(b, 0, 0, oldWidth, oldHeight, scale, false);
    b.recycle();
    return result;
}

From source file:Main.java

public static boolean rotateBitmap(File inFile, File outFile, int angle)
        throws FileNotFoundException, IOException {
    // Declare//from  ww w  .j a  v a  2  s  .  co m
    FileInputStream inStream = null;
    FileOutputStream outStream = null;
    // Create options
    BitmapFactory.Options options = new BitmapFactory.Options();
    // Create transform matrix
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    // Increment inSampleSize progressively to reduce image resolution and size. If
    // the program is properly managing memory, and you don't have other large images
    // loaded in memory, this loop will generally not need to go through more than 3
    // iterations. To be safe though, we stop looping after a certain amount of tries
    // to avoid infinite loops
    for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) {
        try {
            // Load the bitmap from file
            inStream = new FileInputStream(inFile);
            Bitmap originalBitmap = BitmapFactory.decodeStream(inStream, null, options);
            // Rotate the bitmap
            Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(),
                    originalBitmap.getHeight(), matrix, true);
            // Save the rotated bitmap
            outStream = new FileOutputStream(outFile);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
            outStream.close();
            // Recycle the bitmaps to immediately free memory
            originalBitmap.recycle();
            originalBitmap = null;
            rotatedBitmap.recycle();
            rotatedBitmap = null;
            // Return
            return true;
        } catch (OutOfMemoryError e) {
            // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value
        } finally {
            // Clean-up if we failed on save
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                }
            }
        }
    }
    // Failed
    return false;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.WHITE;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);// w w  w.ja va  2s .com
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

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

    int color = 0xff424242;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
    float roundPx = pixels;

    paint.setAntiAlias(true);//from   w  ww  .  j a  v  a 2s . c o  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    bitmap.recycle();
    return output;
}

From source file:Main.java

public static int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false; // No pre-scaling

        // Read in the resource
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();
    }//w w  w.  jav  a 2 s  . co  m

    if (textureHandle[0] == 0) {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}