Java Code Examples for android.graphics.Bitmap#recycle()

The following examples show how to use android.graphics.Bitmap#recycle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BlurTransformation.java    From android-tutorials-glide with MIT License 6 votes vote down vote up
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
    Bitmap blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true);

    // Allocate memory for Renderscript to work with
    Allocation input = Allocation.createFromBitmap(rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED);
    Allocation output = Allocation.createTyped(rs, input.getType());

    // Load up an instance of the specific script that we want to use.
    ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
    script.setInput(input);

    // Set the blur radius
    script.setRadius(10);

    // Start the ScriptIntrinisicBlur
    script.forEach(output);

    // Copy the output to the blurred bitmap
    output.copyTo(blurredBitmap);

    toTransform.recycle();

    return blurredBitmap;
}
 
Example 2
Source File: FilesUtils.java    From PocketEOS-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 将图片按照某个角度进行旋转
 *
 * @param bm     需要旋转的图片
 * @param degree 旋转角度
 * @return 旋转后的图片 bitmap
 */
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
    Bitmap returnBm = null;

    // 根据旋转角度,生成旋转矩阵
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    try {
        // 将原始图片按照旋转矩阵进行旋转,并得到新的图片
        returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
    }
    if (returnBm == null) {
        returnBm = bm;
    }
    if (bm != returnBm) {
        bm.recycle();
    }
    return returnBm;
}
 
Example 3
Source File: MyBitmapUtils.java    From MyImageUtil with Apache License 2.0 6 votes vote down vote up
/**
 * 生成bitmap缩略图
 * @param bitmap
 * @param needRecycle 是否释放bitmap原图
 * @param newHeight 目标宽度
 * @param newWidth 目标高度
 * @return
 */
public static Bitmap createBitmapThumbnail(Bitmap bitmap, boolean needRecycle, int newHeight, int newWidth) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    // 计算缩放比例
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // 取得想要缩放的matrix参数
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    // 得到新的图片
    Bitmap newBitMap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    if (needRecycle)
        bitmap.recycle();
    return newBitMap;
}
 
Example 4
Source File: FragmentiGapMap.java    From iGap-Android with GNU Affero General Public License v3.0 6 votes vote down vote up
protected static Bitmap addBorderToCircularBitmap(Bitmap srcBitmap, int borderWidth, int borderColor) {
    int dstBitmapWidth = srcBitmap.getWidth() + borderWidth * 2;
    Bitmap dstBitmap = Bitmap.createBitmap(dstBitmapWidth, dstBitmapWidth, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dstBitmap);
    canvas.drawBitmap(srcBitmap, borderWidth, borderWidth, null);
    Paint paint = new Paint();
    paint.setColor(borderColor);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(borderWidth);
    paint.setAntiAlias(true);
    canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, canvas.getWidth() / 2 - (borderWidth / 2 + G.context.getResources().getDimension(R.dimen.dp1)), paint);
    if (!srcBitmap.isRecycled()) {
        srcBitmap.recycle();
        srcBitmap = null;
    }
    return dstBitmap;
}
 
Example 5
Source File: BitmapUtils.java    From cloudinary_android with MIT License 6 votes vote down vote up
private static Bitmap getScaledBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    if (reqWidth > 0 && reqHeight > 0) {
        Bitmap resized;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float scale = Math.max(width / (float) reqWidth, height / (float) reqHeight);

        resized = Bitmap.createScaledBitmap(bitmap, (int) (width / scale), (int) (height / scale), false);
        if (resized != null) {
            if (resized != bitmap) {
                bitmap.recycle();
            }
            return resized;
        }
    }

    return bitmap;
}
 
Example 6
Source File: BitmapUtil.java    From MyBookshelf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 按新的宽高缩放图片
 */
public static Bitmap scaleImage(Bitmap bm, int newWidth, int newHeight) {
    if (bm == null) {
        return null;
    }
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
            true);
    if (!bm.isRecycled()) {
        bm.recycle();
    }
    return newbm;
}
 
Example 7
Source File: VideoExtractFrameAsyncUtils.java    From SimpleVideoEdit with Apache License 2.0 6 votes vote down vote up
/**
 * 设置固定的宽度,高度随之变化,使图片不会变形
 *
 * @param bm Bitmap
 * @return Bitmap
 */
private Bitmap scaleImage(Bitmap bm) {
    if (bm == null) {
        return null;
    }
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = extractW * 1.0f / width;
    //float scaleHeight =extractH*1.0f / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleWidth);
    Bitmap newBm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
            true);
    if (!bm.isRecycled()) {
        bm.recycle();
        bm = null;
    }
    return newBm;
}
 
Example 8
Source File: BitmapUtils.java    From WeGit with Apache License 2.0 6 votes vote down vote up
public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
    if (needRecycle) {
        bmp.recycle();
    }

    byte[] result = output.toByteArray();
    try {
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}
 
Example 9
Source File: FloatBallService.java    From AndroidDemo with MIT License 6 votes vote down vote up
private void getScreen(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();

    Bitmap bitmap = view.getDrawingCache();
    Bitmap _b = Bitmap.createBitmap(bitmap, 0, 0, Tools.getScreenWidth(this), Tools.getScreenHeight(this));
    view.destroyDrawingCache();
    try {
        File file = new File(Environment.getExternalStorageDirectory() + "/zhou/screenshot/" + format.format(new Date()) + ".png");
        if (!file.getParentFile().exists())
            file.getParentFile().mkdir();
        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream fos = new FileOutputStream(file);
        _b.compress(Bitmap.CompressFormat.PNG, 90, fos);
        fos.flush();
        fos.close();
        _b.recycle();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: MP4Encoder.java    From Bitmp4 with Apache License 2.0 5 votes vote down vote up
private byte[] getNV12(int inputWidth, int inputHeight, Bitmap scaled) {
  int[] argb = new int[(inputWidth * inputHeight)];
  scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
  byte[] yuv = new byte[(((inputWidth * inputHeight) * 3) / 2)];
  encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
  scaled.recycle();
  return yuv;
}
 
Example 11
Source File: LUTImage.java    From easyLUT with Apache License 2.0 5 votes vote down vote up
public static LUTImage createLutImage(Bitmap lutBitmap,
                                      CoordinateToColor.Type coordinateToColorType,
                                      LutAlignment.Mode lutAlignmentMode) {
    final int lutWidth = lutBitmap.getWidth();
    int lutColors[] = new int[lutWidth * lutBitmap.getHeight()];
    lutBitmap.getPixels(lutColors, 0, lutWidth, 0, 0, lutWidth, lutBitmap.getHeight());
    LUTImage lutImage = new LUTImage(lutWidth, lutBitmap.getHeight(), lutColors,
            coordinateToColorType, lutAlignmentMode);

    lutBitmap.recycle();
    return lutImage;
}
 
Example 12
Source File: DragView.java    From timecat with Apache License 2.0 5 votes vote down vote up
private void recycleBitmap() {
    Drawable drawable = getDrawable();
    if (drawable != null && drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        Bitmap bitmap = bitmapDrawable.getBitmap();
        if (bitmap != null && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }
}
 
Example 13
Source File: DisplayUtils.java    From NewXmPluginSDK with Apache License 2.0 5 votes vote down vote up
public static Bitmap getBlurBmp(Context context, Bitmap srcBmp, float scale) {
        try {
            Bitmap dstBmp = Bitmap.createScaledBitmap(srcBmp, (int) (srcBmp.getWidth() * scale), (int) (srcBmp.getHeight() * scale), false);

            Class<?> cScreenShotUtils = Class.forName("miui.util.ScreenshotUtils");
            Method mGetBlurBackground = cScreenShotUtils.getMethod("getBlurBackground", Bitmap.class, Bitmap.class);
            Bitmap bluredBmp = (Bitmap) mGetBlurBackground.invoke(cScreenShotUtils, dstBmp, null);
//            srcBmp.recycle();
            dstBmp.recycle();
            return bluredBmp;
        } catch (Exception e) {
        }

        return null;
    }
 
Example 14
Source File: Pixelate.java    From android-close-pixelate with MIT License 5 votes vote down vote up
public static Bitmap fromInputStream(@NonNull InputStream inputStream, @NonNull PixelateLayer... layers) throws IOException {
    Bitmap in = BitmapFactory.decodeStream(inputStream);
    inputStream.close();
    Bitmap out = fromBitmap(in, layers);
    in.recycle();
    return out;
}
 
Example 15
Source File: CropView.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
private void setImageRotateBitmap(RotateBitmap bitmap)
{
    Bitmap old = mBitmapDisplayed.getBitmap();

    mBitmapDisplayed = bitmap;
    setImageBitmap(bitmap.getBitmap());

    if (old != null)
    {
        old.recycle();
    }

    updateBaseMatrix();
}
 
Example 16
Source File: BitmapUtils.java    From zone-sdk with MIT License 5 votes vote down vote up
public static void recycledBitmap(Bitmap bitmap) {
    if (bitmap != null && !bitmap.isRecycled()) {
        bitmap.recycle();
        bitmap = null;
    }
    // 这个再说把 系统多了会自动调用把~
    // System.gc();
}
 
Example 17
Source File: ImageUtils.java    From Android-utils with Apache License 2.0 5 votes vote down vote up
public static Bitmap clip(final Bitmap src,
                          final int x,
                          final int y,
                          final int width,
                          final int height,
                          final boolean recycle) {
    if (isEmptyBitmap(src)) return null;
    Bitmap ret = Bitmap.createBitmap(src, x, y, width, height);
    if (recycle && !src.isRecycled()) src.recycle();
    return ret;
}
 
Example 18
Source File: BitmapUtils.java    From AcDisplay with GNU General Public License v2.0 4 votes vote down vote up
public static int getAverageColor(@NonNull Bitmap bitmap) {
    Bitmap onePixelBitmap = Bitmap.createScaledBitmap(bitmap, 1, 1, true);
    int color = onePixelBitmap.getPixel(0, 0);
    onePixelBitmap.recycle();
    return color;
}
 
Example 19
Source File: BaseImageDecoder.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
protected Bitmap considerExactScaleAndOrientatiton(Bitmap bitmap, ImageDecodingInfo imagedecodinginfo, int i, boolean flag)
{
    Matrix matrix = new Matrix();
    ImageScaleType imagescaletype = imagedecodinginfo.getImageScaleType();
    if (imagescaletype == ImageScaleType.EXACTLY || imagescaletype == ImageScaleType.EXACTLY_STRETCHED)
    {
        ImageSize imagesize = new ImageSize(bitmap.getWidth(), bitmap.getHeight(), i);
        ImageSize imagesize1 = imagedecodinginfo.getTargetSize();
        com.nostra13.universalimageloader.core.assist.ViewScaleType viewscaletype = imagedecodinginfo.getViewScaleType();
        boolean flag1;
        float f;
        Bitmap bitmap1;
        Object aobj[];
        Object aobj1[];
        if (imagescaletype == ImageScaleType.EXACTLY_STRETCHED)
        {
            flag1 = true;
        } else
        {
            flag1 = false;
        }
        f = ImageSizeUtils.computeImageScale(imagesize, imagesize1, viewscaletype, flag1);
        if (Float.compare(f, 1.0F) != 0)
        {
            matrix.setScale(f, f);
            if (loggingEnabled)
            {
                Object aobj2[] = new Object[4];
                aobj2[0] = imagesize;
                aobj2[1] = imagesize.scale(f);
                aobj2[2] = Float.valueOf(f);
                aobj2[3] = imagedecodinginfo.getImageKey();
                L.d("Scale subsampled image (%1$s) to %2$s (scale = %3$.5f) [%4$s]", aobj2);
            }
        }
    }
    if (flag)
    {
        matrix.postScale(-1F, 1.0F);
        if (loggingEnabled)
        {
            aobj1 = new Object[1];
            aobj1[0] = imagedecodinginfo.getImageKey();
            L.d("Flip image horizontally [%s]", aobj1);
        }
    }
    if (i != 0)
    {
        matrix.postRotate(i);
        if (loggingEnabled)
        {
            aobj = new Object[2];
            aobj[0] = Integer.valueOf(i);
            aobj[1] = imagedecodinginfo.getImageKey();
            L.d("Rotate image on %1$d\260 [%2$s]", aobj);
        }
    }
    bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    if (bitmap1 != bitmap)
    {
        bitmap.recycle();
    }
    return bitmap1;
}
 
Example 20
Source File: Util.java    From DMView2 with Apache License 2.0 4 votes vote down vote up
public static void restoreBitmap(Bitmap bitmap) {
    if (bitmap == null || bitmap.isRecycled()) return;
    bitmap.recycle();
}