List of usage examples for android.graphics Bitmap recycle
public void recycle()
From source file:Main.java
/** * cut a circle from a bitmap//from w ww. j a v a2 s . c om * * @param picturePath complete path name for the file. * @param destCube the cube dimension of dest bitmap * @return the bitmap */ public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inDither = false; //Disable Dithering mode opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future opts.inTempStorage = new byte[32 * 1024]; Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts); int cube = destCube; if (bitmapImg == null) return null; int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight()); Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5); int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5); final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest); final Rect rectDest = new Rect(0, 0, cube, cube); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint); bitmapImg.recycle(); return output; }
From source file:edu.stanford.mobisocial.dungbeetle.feed.objects.PictureObj.java
public static DbObject from(Context context, Uri imageUri) throws IOException { // Query gallery for camera picture via // Android ContentResolver interface ContentResolver cr = context.getContentResolver(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/*from ww w . j av a 2 s . c o m*/ BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int targetSize = 200; int xScale = (options.outWidth + targetSize - 1) / targetSize; int yScale = (options.outHeight + targetSize - 1) / targetSize; int scale = xScale < yScale ? xScale : yScale; //uncomment this to get faster power of two scaling //for(int i = 0; i < 32; ++i) { // int mushed = scale & ~(1 << i); // if(mushed != 0) // scale = mushed; //} options.inJustDecodeBounds = false; options.inSampleSize = scale; Bitmap sourceBitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri), null, options); int width = sourceBitmap.getWidth(); int height = sourceBitmap.getHeight(); int cropSize = Math.min(width, height); float scaleSize = ((float) targetSize) / cropSize; Matrix matrix = new Matrix(); matrix.postScale(scaleSize, scaleSize); float rotation = PhotoTaker.rotationForImage(context, imageUri); if (rotation != 0f) { matrix.preRotate(rotation); } Bitmap resizedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, width, height, matrix, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, baos); byte[] data = baos.toByteArray(); sourceBitmap.recycle(); sourceBitmap = null; resizedBitmap.recycle(); resizedBitmap = null; System.gc(); // TODO: gross. JSONObject base = new JSONObject(); if (ContentCorral.CONTENT_CORRAL_ENABLED) { try { String type = cr.getType(imageUri); if (type == null) { type = "image/jpeg"; } base.put(CorralClient.OBJ_LOCAL_URI, imageUri.toString()); base.put(CorralClient.OBJ_MIME_TYPE, type); String localIp = ContentCorral.getLocalIpAddress(); if (localIp != null) { base.put(Contact.ATTR_LAN_IP, localIp); } } catch (JSONException e) { Log.e(TAG, "impossible json error possible!"); } } return from(base, data); }
From source file:com.almalence.googsharing.Thumbnail.java
private static Bitmap rotateImage(Bitmap bitmap, int orientation) { if (orientation != 0) { // We only rotate the thumbnail once even if we get OOM. Matrix m = new Matrix(); m.setRotate(orientation, bitmap.getWidth() * 0.5f, bitmap.getHeight() * 0.5f); try {// w w w .j av a 2 s . c o m Bitmap rotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true); // If the rotated bitmap is the original bitmap, then it // should not be recycled. if (rotated != bitmap) bitmap.recycle(); return rotated; } catch (Exception t) { Log.w(TAG, "Failed to rotate thumbnail", t); } } return bitmap; }
From source file:Main.java
public static Bitmap rotateAndMirror(Bitmap b, int degrees, boolean mirror) { if ((degrees != 0 || mirror) && b != null) { Matrix m = new Matrix(); // Mirror first. // horizontal flip + rotation = -rotation + horizontal flip if (mirror) { m.postScale(-1, 1);//ww w . j a v a 2 s .co m degrees = (degrees + 360) % 360; if (degrees == 0 || degrees == 180) { m.postTranslate((float) b.getWidth(), 0); } else if (degrees == 90 || degrees == 270) { m.postTranslate((float) b.getHeight(), 0); } else { throw new IllegalArgumentException("Invalid degrees=" + degrees); } } if (degrees != 0) { // clockwise m.postRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2); } try { Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true); if (b != b2) { b.recycle(); b = b2; } } catch (OutOfMemoryError ex) { // We have no memory to rotate. Return the original bitmap. } } return b; }
From source file:Main.java
public static Bitmap blurBitmap(Bitmap bitmap, Context context) { //Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); //Instantiate a new Renderscript RenderScript rs = RenderScript.create(context); //Create an Intrinsic Blur Script using the Renderscript ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps Allocation allIn = Allocation.createFromBitmap(rs, bitmap); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //Set the radius of the blur blurScript.setRadius(25.f);/*from w w w.j a v a 2 s . c o m*/ //Perform the Renderscript blurScript.setInput(allIn); blurScript.forEach(allOut); //Copy the final bitmap created by the out Allocation to the outBitmap allOut.copyTo(outBitmap); //recycle the original bitmap bitmap.recycle(); //After finishing everything, we destroy the Renderscript. rs.destroy(); return outBitmap; }
From source file:Main.java
public static Bitmap blurBitmap(Context context, Bitmap bitmap, float blur) { Bitmap bitmapCopy = bitmap.copy(bitmap.getConfig(), false); //Let's create an empty bitmap with the same size of the bitmap we want to blur Bitmap outBitmap = (Bitmap.createBitmap(bitmapCopy.getWidth(), bitmapCopy.getHeight(), Bitmap.Config.ARGB_8888));//from w w w .java 2 s.c o m //Instantiate a new Renderscript RenderScript rs = RenderScript.create(context); //Create an Intrinsic Blur Script using the Renderscript ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //Create the in/out Allocations with the Renderscript and the in/out bitmaps Allocation allIn = Allocation.createFromBitmap(rs, bitmapCopy); Allocation allOut = Allocation.createFromBitmap(rs, outBitmap); //Set the radius of the blur blurScript.setRadius(blur); //Perform the Renderscript blurScript.setInput(allIn); blurScript.forEach(allOut); //Copy the final bitmap created by the out Allocation to the outBitmap allOut.copyTo(outBitmap); bitmapCopy.recycle(); //After finishing everything, we destroy the Renderscript. rs.destroy(); return outBitmap; }
From source file:Main.java
public static Bitmap getCircularBitmap(Bitmap srcBitmap) { // Calculate the circular bitmap width with border int squareBitmapWidth = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight()); //int squareBitmapWidth = 300; // Initialize a new instance of Bitmap Bitmap dstBitmap = Bitmap.createBitmap(squareBitmapWidth, // Width squareBitmapWidth, // Height Bitmap.Config.ARGB_8888 // Config );/*from w w w . ja v a 2s .c o m*/ Canvas canvas = new Canvas(dstBitmap); // Initialize a new Paint instance Paint paint = new Paint(); paint.setAntiAlias(true); Rect rect = new Rect(0, 0, squareBitmapWidth, squareBitmapWidth); RectF rectF = new RectF(rect); canvas.drawOval(rectF, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); // Calculate the left and top of copied bitmap float left = (squareBitmapWidth - srcBitmap.getWidth()) / 2; float top = (squareBitmapWidth - srcBitmap.getHeight()) / 2; canvas.drawBitmap(srcBitmap, left, top, paint); srcBitmap.recycle(); return dstBitmap; }
From source file:com.lightbox.android.bitmap.BitmapUtils.java
public static Bitmap createUserPhotoThumbnail(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); Bitmap scaledBitmap; //TODO get thumb size constant from somewhere, but where? if (width > height) { scaledBitmap = Bitmap.createScaledBitmap(bitmap, (int) (BitmapSource.THM_SIZE_PX * ((float) width / height)), BitmapSource.THM_SIZE_PX, true); } else {//from w w w.ja v a2 s . c o m scaledBitmap = Bitmap.createScaledBitmap(bitmap, BitmapSource.THM_SIZE_PX, (int) (BitmapSource.THM_SIZE_PX * ((float) height / width)), true); } Bitmap thumb = Bitmap.createBitmap(scaledBitmap, (scaledBitmap.getWidth() - BitmapSource.THM_SIZE_PX) / 2, (scaledBitmap.getHeight() - BitmapSource.THM_SIZE_PX) / 2, BitmapSource.THM_SIZE_PX, BitmapSource.THM_SIZE_PX); scaledBitmap.recycle(); return thumb; }
From source file:Main.java
private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) { boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0; boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0; int deltaX = source.getWidth() - targetWidth; int deltaY = source.getHeight() - targetHeight; if (!scaleUp && (deltaX < 0 || deltaY < 0)) { Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b2); int deltaXHalf = Math.max(0, deltaX / 2); int deltaYHalf = Math.max(0, deltaY / 2); Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()), deltaYHalf + Math.min(targetHeight, source.getHeight())); int dstX = (targetWidth - src.width()) / 2; int dstY = (targetHeight - src.height()) / 2; Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY); c.drawBitmap(source, src, dst, null); if (recycle) source.recycle(); return b2; }/* www. j a va2s . c o m*/ float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; float scale = bitmapAspect > viewAspect ? targetHeight / bitmapHeightF : targetWidth / bitmapWidthF; if (scale < .9F || scale > 1F) scaler.setScale(scale, scale); else scaler = null; Bitmap b1; if (scaler != null) b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); else b1 = source; if (recycle && b1 != source) source.recycle(); int dx1 = Math.max(0, b1.getWidth() - targetWidth); int dy1 = Math.max(0, b1.getHeight() - targetHeight); Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight); if (b2 != b1 && (recycle || b1 != source)) b1.recycle(); return b2; }
From source file:Main.java
public static Bitmap scaleTo(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean fitInScreen) { float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; float scale = 0; float scaleH = targetHeight / bitmapHeightF; float scaleW = targetWidth / bitmapWidthF; if (fitInScreen && scaleH < scaleW) { scale = scaleH;//from ww w . j a v a2 s .co m } else if (fitInScreen && scaleH >= scaleW) { scale = scaleW; } else if (bitmapAspect > viewAspect) { scale = scaleH; } else { scale = scaleW; } if (scale != 0 && (scale < .9F || scale > 1F)) { scaler.setScale(scale, scale); } else { scaler = null; } Bitmap b1; if (scaler != null) { // this is used for minithumb and crop, so we want to filter here. b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true); source.recycle(); } else { b1 = source; } return b1; }