List of usage examples for android.graphics Bitmap recycle
public void recycle()
From source file:Main.java
/** * Transform source Bitmap to targeted width and height. *//*from w ww. j a v a 2s. com*/ 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)) { /* * In this case the bitmap is smaller, at least in one dimension, * than the target. Transform it by placing as much of the image * as possible into the target and leaving the top/bottom or * left/right (or both) black. */ 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; } float bitmapWidthF = source.getWidth(); float bitmapHeightF = source.getHeight(); float bitmapAspect = bitmapWidthF / bitmapHeightF; float viewAspect = (float) targetWidth / targetHeight; if (bitmapAspect > viewAspect) { float scale = targetHeight / bitmapHeightF; if (scale < .9F || scale > 1F) { scaler.setScale(scale, scale); } else { scaler = null; } } else { float scale = targetWidth / bitmapWidthF; if (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); } 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) { if (recycle || b1 != source) { b1.recycle(); } } return b2; }
From source file:com.polyvi.xface.extension.capture.MediaType.java
/** * Image .//from w w w . j a va 2 s .c om * * @param fileAbsPath ? * @param obj ? * @return ? JSONObject * @throws JSONException */ private JSONObject getImageData(String fileAbsPath, JSONObject obj) throws JSONException { Bitmap bitmap = BitmapFactory.decodeFile(fileAbsPath); if (bitmap != null) { obj.put(PROP_HEIGHT, bitmap.getHeight()); obj.put(PROP_WIDTH, bitmap.getWidth()); bitmap.recycle(); } return obj; }
From source file:Main.java
public static Bitmap rotateBitmapFromExif(String filePath, Bitmap bitmap) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;/*from w w w . j a va 2s . c om*/ BitmapFactory.decodeFile(filePath, options); String orientString; try { ExifInterface exif = new ExifInterface(filePath); orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION); } catch (IOException e) { e.printStackTrace(); return bitmap; } int orientation = (orientString != null) ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL; int rotationAngle = 0, outHeight = 0, outWidth = 0; switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotationAngle = 90; outHeight = bitmap.getWidth(); outWidth = bitmap.getHeight(); break; case ExifInterface.ORIENTATION_ROTATE_180: rotationAngle = 180; outHeight = bitmap.getHeight(); outWidth = bitmap.getWidth(); break; case ExifInterface.ORIENTATION_ROTATE_270: rotationAngle = 270; outHeight = bitmap.getWidth(); outWidth = bitmap.getHeight(); break; } if (rotationAngle == 0) { return bitmap; } Matrix matrix = new Matrix(); matrix.setRotate(rotationAngle, (float) bitmap.getWidth() / 2, (float) bitmap.getHeight() / 2); Bitmap rotateBitmap = Bitmap.createBitmap(bitmap, 0, 0, outWidth, outHeight, matrix, true); if (bitmap != rotateBitmap) { bitmap.recycle(); } return rotateBitmap; }
From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java
/** * Sets the downloaded avatar.// w w w . ja va 2 s.c o m * * @param rawBitmap bitmap to round corners */ public static Bitmap publishAvatar(Bitmap rawBitmap, int roundK) { if (rawBitmap == null) return null; try { int size = 0; if (rawBitmap.getHeight() > rawBitmap.getWidth()) { size = rawBitmap.getWidth(); } else { size = rawBitmap.getHeight(); } Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, size, size); final RectF rectF = new RectF(rect); final float roundPx = roundK; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); canvas.drawBitmap(rawBitmap, rect, rect, paint); rawBitmap.recycle(); return output; } catch (Exception e) { } return null; }
From source file:com.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java
private static Bitmap rotate(String path, Bitmap bm) { if (bm == null) { return null; }/* w w w . j av a2 s. c o m*/ Bitmap result = bm; int ori = ExifInterface.ORIENTATION_NORMAL; try { ExifInterface ei = new ExifInterface(path); ori = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); } catch (Exception e) { //simply fallback to normal orientation AQUtility.debug(e); } if (ori > 0) { Matrix matrix = getRotateMatrix(ori); result = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true); AQUtility.debug("before", bm.getWidth() + ":" + bm.getHeight()); AQUtility.debug("after", result.getWidth() + ":" + result.getHeight()); if (bm != result) { bm.recycle(); } } return result; }
From source file:com.actionbarsherlock.sample.hcgallery.MainActivity.java
void showNotification(boolean custom) { final Resources res = getResources(); final NotificationManager notificationManager = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);/* w ww .java 2s . com*/ Notification.Builder builder = new Notification.Builder(this) .setSmallIcon(R.drawable.ic_stat_notify_example).setAutoCancel(true) .setTicker(getString(R.string.notification_text)) .setContentIntent(getDialogPendingIntent("Tapped the notification entry.")); if (custom) { // Sets a custom content view for the notification, including an image button. RemoteViews layout = new RemoteViews(getPackageName(), R.layout.notification); layout.setTextViewText(R.id.notification_title, getString(R.string.app_name)); layout.setOnClickPendingIntent(R.id.notification_button, getDialogPendingIntent("Tapped the 'dialog' button in the notification.")); builder.setContent(layout); // Notifications in Android 3.0 now have a standard mechanism for displaying large // bitmaps such as contact avatars. Here, we load an example image and resize it to the // appropriate size for large bitmaps in notifications. Bitmap largeIconTemp = BitmapFactory.decodeResource(res, R.drawable.notification_default_largeicon); Bitmap largeIcon = Bitmap.createScaledBitmap(largeIconTemp, res.getDimensionPixelSize(android.R.dimen.notification_large_icon_width), res.getDimensionPixelSize(android.R.dimen.notification_large_icon_height), false); largeIconTemp.recycle(); builder.setLargeIcon(largeIcon); } else { builder.setNumber(7) // An example number. .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.notification_text)); } notificationManager.notify(NOTIFICATION_DEFAULT, builder.getNotification()); }
From source file:Main.java
public static Bitmap toRoundBitmap(Bitmap bitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; if (width <= height) { roundPx = width / 2;//from w ww.j a v a 2s. c o m top = 0; bottom = width; left = 0; right = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); paint.setAntiAlias(true); 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, src, dst, paint); if (bitmap != null && !bitmap.isRecycled()) { bitmap.recycle(); } return output; }
From source file:no.ntnu.idi.socialhitchhiking.myAccount.MyAccountCar.java
/** * Gets and resizes a {@link Bitmap} from a {@link Uri} using width and height. * @param uri//from w w w. ja v a 2s .c om * @param width * @param height * @return */ private Bitmap getBitmap(Uri uri, int width, int height) { InputStream in = null; try { int IMAGE_MAX_SIZE = Math.max(width, height); in = getContentResolver().openInputStream(uri); //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, o); in.close(); int scale = 1; if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { scale = (int) Math.pow(2, (int) Math.round( Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } //adjust sample size such that the image is bigger than the result scale -= 1; BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; in = getContentResolver().openInputStream(uri); Bitmap b = BitmapFactory.decodeStream(in, null, o2); in.close(); //scale bitmap to desired size Bitmap scaledBitmap = Bitmap.createScaledBitmap(b, width, height, false); //free memory b.recycle(); return scaledBitmap; } catch (FileNotFoundException e) { } catch (IOException e) { } return null; }
From source file:com.polyvi.xface.extension.camera.XCameraExt.java
/** * ??./*w w w. j a v a 2 s . c om*/ */ private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) { if (bitmap != null) { bitmap.recycle(); bitmap = null; } // Clean up initial camera-written image file. String filePath = oldImage.toString(); if (filePath.startsWith("file://")) { filePath = filePath.substring(7); } (new File(filePath)).delete(); checkForDuplicateImage(imageType); System.gc(); }
From source file:br.com.viniciuscr.notification2android.mediaPlayer.MusicUtils.java
private static Bitmap getArtworkQuick(Context context, long album_id, int w, int h) { // NOTE: There is in fact a 1 pixel border on the right side in the ImageView // used to display this drawable. Take it into account now, so we don't have to // scale later. w -= 1;//w w w. java 2 s . c o m ContentResolver res = context.getContentResolver(); Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id); if (uri != null) { ParcelFileDescriptor fd = null; try { fd = res.openFileDescriptor(uri, "r"); int sampleSize = 1; // Compute the closest power-of-two scale factor // and pass that to sBitmapOptionsCache.inSampleSize, which will // result in faster decoding and better quality sBitmapOptionsCache.inJustDecodeBounds = true; BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache); int nextWidth = sBitmapOptionsCache.outWidth >> 1; int nextHeight = sBitmapOptionsCache.outHeight >> 1; while (nextWidth > w && nextHeight > h) { sampleSize <<= 1; nextWidth >>= 1; nextHeight >>= 1; } sBitmapOptionsCache.inSampleSize = sampleSize; sBitmapOptionsCache.inJustDecodeBounds = false; Bitmap b = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor(), null, sBitmapOptionsCache); if (b != null) { // finally rescale to exactly the size we need if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) { Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true); // Bitmap.createScaledBitmap() can return the same bitmap if (tmp != b) b.recycle(); b = tmp; } } return b; } catch (FileNotFoundException e) { } finally { try { if (fd != null) fd.close(); } catch (IOException e) { } } } return null; }