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:com.xiaomi.account.utils.SysHelper.java

public static Bitmap createPhoto(Activity activity, String avatarFileName) {
    Bitmap origin = null;
    if (avatarFileName != null) {
        origin = SnsUtils.getUserAvatarByAbsPath(activity, avatarFileName);
    }//w w  w  .ja  v a2s .c  om
    if (origin == null) {
        origin = android.graphics.BitmapFactory.decodeResource(activity.getResources(),
                R.drawable.default_avatar_in_settings);
    }
    if (origin == null) {
        return null;
    }
    Bitmap avatar = BitmapFactory.createPhoto(activity, origin);
    origin.recycle();
    return avatar;
}

From source file:android.support.v7.testutils.TestUtilsMatchers.java

/**
 * Returns a matcher that matches <code>View</code>s whose combined background starting
 * from the view and up its ancestor chain matches the specified color.
 *///ww w .  j  a  v  a2 s  .c  om
public static Matcher isCombinedBackground(@ColorInt final int color) {
    return new BoundedMatcher<View, View>(View.class) {
        private String failedComparisonDescription;

        @Override
        public void describeTo(final Description description) {
            description.appendText("with ascendant background of color: ");

            description.appendText(failedComparisonDescription);
        }

        @Override
        public boolean matchesSafely(View view) {
            // Create a bitmap with combined backgrounds of the view and its ancestors.
            Bitmap combinedBackgroundBitmap = TestUtils.getCombinedBackgroundBitmap(view);
            try {
                TestUtils.assertAllPixelsOfColor("", combinedBackgroundBitmap,
                        combinedBackgroundBitmap.getWidth(), combinedBackgroundBitmap.getHeight(), color, 0,
                        true);
                // If we are here, the color comparison has passed.
                failedComparisonDescription = null;
                return true;
            } catch (Throwable t) {
                failedComparisonDescription = t.getMessage();
                return false;
            } finally {
                combinedBackgroundBitmap.recycle();
            }
        }
    };
}

From source file:Main.java

public static Bitmap compressPixel1(String imgPath, float pixelW, float pixelH) {
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inJustDecodeBounds = true;/*from  w w  w  .  jav a2 s.  co m*/
    newOpts.inPreferredConfig = Config.RGB_565;
    Bitmap bitmap = BitmapFactory.decodeFile(imgPath, newOpts);
    newOpts.inJustDecodeBounds = false;
    int w = newOpts.outWidth;
    int h = newOpts.outHeight;
    float hh = pixelH;
    float ww = pixelW;
    int height = h;
    int width = w;
    int inSampleSize = 1;
    if (h > hh || w > ww) {
        final int halfHeight = h / 2;
        final int halfWidth = w / 2;
        while ((halfHeight / inSampleSize) > hh && (halfWidth / inSampleSize) > ww) {
            inSampleSize *= 2;
        }

        if (h > w) {
            height = 1280;
            width = (1280 * w) / h;
        } else {
            width = 1280;
            height = (1280 * h) / w;
        }
    }
    newOpts.inSampleSize = inSampleSize;
    bitmap = BitmapFactory.decodeFile(imgPath, newOpts);

    Bitmap dst = Bitmap.createScaledBitmap(bitmap, width, height, false);
    if (bitmap != dst) {
        bitmap.recycle();
    }

    return dst;
}

From source file:Main.java

public static int loadTexture(final Bitmap img, final int usedTexId, final boolean recycle) {
    int textures[] = new int[1];
    if (usedTexId == NO_TEXTURE) {
        GLES20.glGenTextures(1, textures, 0);
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textures[0]);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
        GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, img, 0);
    } else {//from w  ww .j av  a2  s  . com
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, usedTexId);
        GLUtils.texSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, img);
        textures[0] = usedTexId;
    }
    if (recycle) {
        img.recycle();
    }
    return textures[0];
}

From source file:Main.java

/**
 * Loads a bitmap into memory then discards the bitmap.
 * @param bitmap/*  w w  w .  j  a va2s.  c  om*/
 * @param oldHandle   Handle to use.
 * @return
 */
public static int loadBitmap(Bitmap bitmap, int oldHandle, boolean recycle) {
    int h;

    int handle[] = new int[1];
    GLES20.glGenTextures(1, handle, 0);
    h = handle[0];
    //      
    //      if(oldHandle <= 0) {
    //         int handle[] = new int[1];
    //         GLES20.glGenTextures(1, handle, 0);
    //         h = handle[0];
    //      } else {
    //         h = oldHandle;
    //      }

    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, h);

    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.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);

    GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

    if (recycle)
        bitmap.recycle();

    return h;
}

From source file:Main.java

public static String saveBitmapToFile(File dir, String name, Bitmap bitmap, String picKind) {
    File f = new File(dir, name + picKind);
    try {//ww  w .j  a v  a  2s  .  c  om
        f.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    CompressFormat format = CompressFormat.JPEG;
    if (picKind != null && picKind.equalsIgnoreCase(".png")) {
        format = CompressFormat.PNG;
    }
    FileOutputStream fOut = null;
    try {
        fOut = new FileOutputStream(f);
        bitmap.compress(format, 100, fOut);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        if (fOut != null) {
            fOut.flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        if (fOut != null) {
            fOut.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    bitmap.recycle();
    bitmap = null;
    return f.getAbsolutePath();
}

From source file:Main.java

public static Bitmap getFocusedBitmap(Context context, Camera camera, Bitmap bmp, Rect box) {
    Point CamRes = getCameraResolution(context, camera);
    Point ScrRes = getScreenResolution(context);

    int SW = ScrRes.x;
    int SH = ScrRes.y;

    int RW = box.width();
    int RH = box.height();
    int RL = box.left;
    int RT = box.top;

    float RSW = (float) (RW * Math.pow(SW, -1));
    float RSH = (float) (RH * Math.pow(SH, -1));

    float RSL = (float) (RL * Math.pow(SW, -1));
    float RST = (float) (RT * Math.pow(SH, -1));

    int CW = CamRes.x;
    int CH = CamRes.y;

    if (CW > CH)
        bmp = rotateBitmap(bmp, 90);/*from  w  ww.  jav  a 2  s.c om*/

    int BW = bmp.getWidth();
    int BH = bmp.getHeight();

    int RBL = (int) (RSL * BW);
    int RBT = (int) (RST * BH);

    int RBW = (int) (RSW * BW);
    int RBH = (int) (RSH * BH);

    Bitmap res = Bitmap.createBitmap(bmp, RBL, RBT, RBW, RBH);
    bmp.recycle();

    return res;
}

From source file:Main.java

public static Bitmap cutImg(Bitmap bitmap, int desiredWidth, int desiredHeight) {

    if (!checkBitmap(bitmap)) {
        return null;
    }//  w w w  .j a va  2 s  .c  om

    if (!checkSize(desiredWidth, desiredHeight)) {
        return null;
    }

    Bitmap resizeBmp = null;

    try {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        int offsetX = 0;
        int offsetY = 0;

        if (width > desiredWidth) {
            offsetX = (width - desiredWidth) / 2;
        } else {
            desiredWidth = width;
        }

        if (height > desiredHeight) {
            offsetY = (height - desiredHeight) / 2;
        } else {
            desiredHeight = height;
        }

        resizeBmp = Bitmap.createBitmap(bitmap, offsetX, offsetY, desiredWidth, desiredHeight);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (resizeBmp != bitmap) {
            bitmap.recycle();
        }
    }
    return resizeBmp;
}

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

/**
 * Create a book cover with the specified bitmap. This method applies
 * several lighting effects to the original bitmap and returns a new decored
 * bitmap./*from   ww  w .ja v a 2  s .  com*/
 * 
 * @param bitmap
 *            The bitmap to decor with lighting effects
 * @param width
 *            The target width of the decored bitmap
 * @param height
 *            The target height of the decored bitmap
 * 
 * @return A new Bitmap based on the original bitmap
 */
public static Bitmap createCover(Bitmap bitmap, int width, int height) {
    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    final float scale = Math.min((float) width / (float) bitmapWidth, (float) height / (float) bitmapHeight);

    final int scaledWidth = (int) (bitmapWidth * scale);
    final int scaledHeight = (int) (bitmapHeight * scale);

    final Bitmap decored = createScaledBitmap(bitmap, scaledWidth, scaledHeight, SHADOW_RADIUS, true,
            SHADOW_PAINT);
    bitmap.recycle();

    final Canvas canvas = new Canvas(decored);

    canvas.translate(SHADOW_RADIUS / 2.0f, SHADOW_RADIUS / 2.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, EDGE_PAINT);
    canvas.drawRect(FOLD_START, 0.0f, FOLD_END, scaledHeight, FOLD_PAINT);
    // noinspection PointlessArithmeticExpression
    canvas.translate(scaledWidth - (EDGE_END - EDGE_START), 0.0f);
    canvas.drawRect(EDGE_START, 0.0f, EDGE_END, scaledHeight, END_EDGE_PAINT);

    return decored;
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);//from  ww  w . j  ava  2  s  .  c o  m
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    }
}