Example usage for android.graphics Bitmap getConfig

List of usage examples for android.graphics Bitmap getConfig

Introduction

In this page you can find the example usage for android.graphics Bitmap getConfig.

Prototype

public final Config getConfig() 

Source Link

Document

If the bitmap's internal config is in one of the public formats, return that config, otherwise return null.

Usage

From source file:com.rsegismont.androlife.common.utils.ImageCacher.java

/**
 * @param candidate/* w  w  w  .j ava  2  s  .  c  o  m*/
 *            - Bitmap to check
 * @param targetOptions
 *            - Options that have the out* value populated
 * @return true if <code>candidate</code> can be used for inBitmap re-use
 *         with <code>targetOptions</code>
 */
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {

    if (Build.VERSION.SDK_INT >= 19) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size
        // of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        int width = targetOptions.outWidth / targetOptions.inSampleSize;
        int height = targetOptions.outHeight / targetOptions.inSampleSize;
        int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= AndrolifeApi19.getAllocationByteCount(candidate);
    }

    // On earlier versions, the dimensions must match exactly and the
    // inSampleSize must be 1
    return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight
            && targetOptions.inSampleSize == 1;

}

From source file:de.s2hmobile.bitmaps.ImageCache.java

/**
 * @param candidate/* w  w  w .  ja  v a  2s .c o m*/
 *            - Bitmap to check
 * @param targetOptions
 *            - Options that have the out* value populated
 * @return true if <code>candidate</code> can be used for inBitmap re-use
 *         with <code>targetOptions</code>
 */
// private static boolean canUseForInBitmap(final Bitmap candidate,
// final BitmapFactory.Options targetOptions) {
// final int width = targetOptions.outWidth / targetOptions.inSampleSize;
// final int height = targetOptions.outHeight / targetOptions.inSampleSize;
//
// return candidate.getWidth() == width && candidate.getHeight() == height;
// }
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    final int outWidth = targetOptions.outWidth;
    final int outHeight = targetOptions.outHeight;
    final int inSampleSize = targetOptions.inSampleSize;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

        // From Android 4.4 (KitKat) onward we can re-use if the byte size
        // of the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        final int width = outWidth / inSampleSize;
        final int height = outHeight / inSampleSize;
        final int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= candidate.getAllocationByteCount();
    }

    // On earlier versions, the dimensions must match exactly and the
    // inSampleSize must be 1
    // TODO in our implementation, the sample size is never one
    return candidate.getWidth() == outWidth && candidate.getHeight() == outHeight && inSampleSize == 1;
}

From source file:Main.java

public static Bitmap createPOT(Bitmap bmp) {
    Bitmap potBmp = null;//from w ww  . j  a  va2s. c  o m
    int potWidth = (int) Math.ceil(Math.log(bmp.getWidth()) / Math.log(2));
    int potHeight = (int) Math.ceil(Math.log(bmp.getHeight()) / Math.log(2));
    potHeight = (int) Math.pow(2, potHeight);
    potWidth = (int) Math.pow(2, potWidth);

    if (potWidth == 1) {
        potWidth = 2;
    }
    if (potHeight == 1) {
        potHeight = 2;
    }

    if (potHeight != bmp.getHeight() || potWidth != bmp.getWidth()) {
        int[] colors = new int[potWidth * potHeight];
        int index = 0;

        int offset = potHeight - bmp.getHeight();
        for (int i = 0; i < potHeight; i++) {
            for (int j = 0; j < potWidth; j++) {
                if (i > offset - 1) {
                    if (j < bmp.getWidth() && i < bmp.getHeight()) {
                        colors[index] = bmp.getPixel(j, i);
                    } else {
                        colors[index] = 0;
                    }
                }
                index++;
            }
        }

        potBmp = Bitmap.createBitmap(colors, potWidth, potHeight, bmp.getConfig());
    } else {
        potBmp = bmp;
    }

    System.gc();

    return potBmp;
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

private static void sendNotification(final NotificationState notificationState, final Bitmap avatarIcon,
        final Bitmap avatarHiRes) {
    final Context context = Factory.get().getApplicationContext();
    if (notificationState.mCanceled) {
        if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
            LogUtil.d(TAG, "sendNotification: Notification already cancelled; dropping it");
        }//from w  ww .j a v  a2s  .  com
        return;
    }

    synchronized (sPendingNotifications) {
        if (sPendingNotifications.contains(notificationState)) {
            sPendingNotifications.remove(notificationState);
        }
    }

    notificationState.mNotificationBuilder.setSmallIcon(notificationState.getIcon())
            .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
            .setColor(context.getResources().getColor(R.color.notification_accent_color))
            //            .setPublicVersion(null)    // TODO: when/if we ever support different
            // text on the lockscreen, instead of "contents hidden"
            .setCategory(CATEGORY_MESSAGE);

    if (avatarIcon != null) {
        notificationState.mNotificationBuilder.setLargeIcon(avatarIcon);
    }

    if (notificationState.mParticipantContactUris != null
            && notificationState.mParticipantContactUris.size() > 0) {
        for (final Uri contactUri : notificationState.mParticipantContactUris) {
            notificationState.mNotificationBuilder.addPerson(contactUri.toString());
        }
    }

    final Uri attachmentUri = notificationState.getAttachmentUri();
    final String attachmentType = notificationState.getAttachmentType();
    Bitmap attachmentBitmap = null;

    // For messages with photo/video attachment, request an image to show in the notification.
    if (attachmentUri != null && notificationState.mNotificationStyle != null
            && (notificationState.mNotificationStyle instanceof NotificationCompat.BigPictureStyle)
            && (ContentType.isImageType(attachmentType) || ContentType.isVideoType(attachmentType))) {
        final boolean isVideo = ContentType.isVideoType(attachmentType);

        MediaRequest<ImageResource> imageRequest;
        if (isVideo) {
            Assert.isTrue(VideoThumbnailRequest.shouldShowIncomingVideoThumbnails());
            final MessagePartVideoThumbnailRequestDescriptor videoDescriptor = new MessagePartVideoThumbnailRequestDescriptor(
                    attachmentUri);
            imageRequest = videoDescriptor.buildSyncMediaRequest(context);
        } else {
            final UriImageRequestDescriptor imageDescriptor = new UriImageRequestDescriptor(attachmentUri,
                    sWearableImageWidth, sWearableImageHeight, false /* allowCompression */,
                    true /* isStatic */, false /* cropToCircle */,
                    ImageUtils.DEFAULT_CIRCLE_BACKGROUND_COLOR /* circleBackgroundColor */,
                    ImageUtils.DEFAULT_CIRCLE_STROKE_COLOR /* circleStrokeColor */);
            imageRequest = imageDescriptor.buildSyncMediaRequest(context);
        }
        final ImageResource imageResource = MediaResourceManager.get().requestMediaResourceSync(imageRequest);
        if (imageResource != null) {
            try {
                // Copy the bitmap, because the one in the ImageResource is managed by
                // MediaResourceManager.
                Bitmap imageResourceBitmap = imageResource.getBitmap();
                Config config = imageResourceBitmap.getConfig();

                // Make sure our bitmap has a valid format.
                if (config == null) {
                    config = Bitmap.Config.ARGB_8888;
                }
                attachmentBitmap = imageResourceBitmap.copy(config, true);
            } finally {
                imageResource.release();
            }
        }
    }

    fireOffNotification(notificationState, attachmentBitmap, avatarIcon, avatarHiRes);
}

From source file:com.itsherpa.andg.imageloader.ImageCache.java

/**
 * @param candidate/*ww  w.j  av  a  2 s . co  m*/
 *            - Bitmap to check
 * @param targetOptions
 *            - Options that have the out* value populated
 * @return true if <code>candidate</code> can be used for inBitmap re-use
 *         with <code>targetOptions</code>
 */
@TargetApi(19)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // From Android 4.4 (KitKat) onward we can re-use if the byte size
        // of
        // the new bitmap is smaller than the reusable bitmap candidate
        // allocation byte count.
        int width = targetOptions.outWidth / targetOptions.inSampleSize;
        int height = targetOptions.outHeight / targetOptions.inSampleSize;
        int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
        return byteCount <= candidate.getAllocationByteCount();
    }

    // On earlier versions, the dimensions must match exactly and the
    // inSampleSize must be 1
    return candidate.getWidth() == targetOptions.outWidth && candidate.getHeight() == targetOptions.outHeight
            && targetOptions.inSampleSize == 1;
}

From source file:com.gsbabil.antitaintdroid.UtilityFunctions.java

/**
 * Source://from   w  w  w  . ja v  a 2  s.  c  o m
 * http://stackoverflow.com/questions/4349075/bitmapfactory-decoderesource
 * -returns-a-mutable-bitmap-in-android-2-2-and-an-immu
 * 
 * Converts a immutable bitmap to a mutable bitmap. This operation doesn't
 * allocates more memory that there is already allocated.
 * 
 * @param imgIn
 *            - Source image. It will be released, and should not be used
 *            more
 * @return a copy of imgIn, but immutable.
 */
public static Bitmap convertBitmapToMutable(Bitmap imgIn) {
    try {
        // this is the file going to use temporally to save the bytes.
        // This file will not be a image, it will store the raw image data.
        File file = new File(MyApp.context.getFilesDir() + File.separator + "temp.tmp");

        // Open an RandomAccessFile
        // Make sure you have added uses-permission
        // android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        // into AndroidManifest.xml file
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

        // get the width and height of the source bitmap.
        int width = imgIn.getWidth();
        int height = imgIn.getHeight();
        Config type = imgIn.getConfig();

        // Copy the byte to the file
        // Assume source bitmap loaded using options.inPreferredConfig =
        // Config.ARGB_8888;
        FileChannel channel = randomAccessFile.getChannel();
        MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, imgIn.getRowBytes() * height);
        imgIn.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        imgIn.recycle();
        System.gc();// try to force the bytes from the imgIn to be released

        // Create a new bitmap to load the bitmap again. Probably the memory
        // will be available.
        imgIn = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        imgIn.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

        // delete the temporary file
        file.delete();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return imgIn;
}

From source file:Main.java

/**
 * A potentially expensive operation to crop the given Bitmap so that it fills the given dimensions. This operation
 * is significantly less expensive in terms of memory if a mutable Bitmap with the given dimensions is passed in
 * as well./*from   w w w . ja  v a  2  s.c o  m*/
 *
 * @param recycled A mutable Bitmap with dimensions width and height that we can load the cropped portion of toCrop
 *                 into
 * @param toCrop The Bitmap to resize
 * @param width The width of the final Bitmap
 * @param height The height of the final Bitmap
 * @return The resized Bitmap (will be recycled if recycled is not null)
 */
public static Bitmap centerCrop(Bitmap recycled, Bitmap toCrop, int width, int height) {
    if (toCrop == null) {
        return null;
    } else if (toCrop.getWidth() == width && toCrop.getHeight() == height) {
        return toCrop;
    }
    //from ImageView/Bitmap.createScaledBitmap
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/widget/ImageView.java
    //https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/graphics/java/android/graphics/Bitmap.java
    final float scale;
    float dx = 0, dy = 0;
    Matrix m = new Matrix();
    if (toCrop.getWidth() * height > width * toCrop.getHeight()) {
        scale = (float) height / (float) toCrop.getHeight();
        dx = (width - toCrop.getWidth() * scale) * 0.5f;
    } else {
        scale = (float) width / (float) toCrop.getWidth();
        dy = (height - toCrop.getHeight() * scale) * 0.5f;
    }

    m.setScale(scale, scale);
    m.postTranslate((int) dx + 0.5f, (int) dy + 0.5f);
    final Bitmap result;
    if (recycled != null) {
        result = recycled;
    } else {
        result = Bitmap.createBitmap(width, height,
                toCrop.getConfig() == null ? Bitmap.Config.ARGB_8888 : toCrop.getConfig());
    }
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint(PAINT_FLAGS);
    canvas.drawBitmap(toCrop, m, paint);
    return result;
}

From source file:com.ticketmaster.servos.util.picasso.RoundedBitmapTransform.java

@Override
public Bitmap transform(Bitmap source) {
    RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(res, source);
    drawable.setCornerRadius(getCornerRadius(source));
    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
    Canvas canvas = new Canvas(output);
    drawable.setAntiAlias(true);//from w  w w  . ja  v  a2 s  .c o  m
    drawable.setBounds(0, 0, source.getWidth(), source.getHeight());
    drawable.draw(canvas);
    if (source != output) {
        source.recycle();
    }
    return output;
}

From source file:Main.java

/**
 * Returns a Bitmap representing the thumbnail of the specified Bitmap. The
 * size of the thumbnail is defined by the dimension
 * android.R.dimen.launcher_application_icon_size.
 * <p>/*from   w  ww  . ja  v  a 2s.  c o m*/
 * This method is not thread-safe and should be invoked on the UI thread
 * only.
 *
 * @param bitmap  The bitmap to get a thumbnail of.
 * @param context The application's context.
 * @return A thumbnail for the specified bitmap or the bitmap itself if the
 * thumbnail could not be created.
 */
public static Bitmap createThumbnailBitmap(Bitmap bitmap, Context context) {
    int sIconWidth = -1;
    int sIconHeight = -1;
    final Resources resources = context.getResources();
    sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);

    final Paint sPaint = new Paint();
    final Rect sBounds = new Rect();
    final Rect sOldBounds = new Rect();
    Canvas sCanvas = new Canvas();

    int width = sIconWidth;
    int height = sIconHeight;

    sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, Paint.FILTER_BITMAP_FLAG));

    final int bitmapWidth = bitmap.getWidth();
    final int bitmapHeight = bitmap.getHeight();

    if (width > 0 && height > 0) {
        if (width < bitmapWidth || height < bitmapHeight) {
            final float ratio = (float) bitmapWidth / bitmapHeight;

            if (bitmapWidth > bitmapHeight) {
                height = (int) (width / ratio);
            } else if (bitmapHeight > bitmapWidth) {
                width = (int) (height * ratio);
            }

            final Config c = (width == sIconWidth && height == sIconHeight) ? bitmap.getConfig()
                    : Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            sBounds.set((sIconWidth - width) / 2, (sIconHeight - height) / 2, width, height);
            sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
            canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
            return thumb;
        } else if (bitmapWidth < width || bitmapHeight < height) {
            final Config c = Config.ARGB_8888;
            final Bitmap thumb = Bitmap.createBitmap(sIconWidth, sIconHeight, c);
            final Canvas canvas = sCanvas;
            final Paint paint = sPaint;
            canvas.setBitmap(thumb);
            paint.setDither(false);
            paint.setFilterBitmap(true);
            canvas.drawBitmap(bitmap, (sIconWidth - bitmapWidth) / 2, (sIconHeight - bitmapHeight) / 2, paint);
            return thumb;
        }
    }

    return bitmap;
}

From source file:com.android.pc.ioc.image.ImageCache.java

/**
 * @param candidate//from  w w  w .ja  va 2s  . com
 *            - Bitmap to check
 * @param targetOptions
 *            - Options that have the out* value populated
 * @return true if <code>candidate</code> can be used for inBitmap re-use with <code>targetOptions</code>
 */
@TargetApi(VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    // BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!Utils.hasKitKat()) {
        // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
        return candidate.getWidth() == targetOptions.outWidth
                && candidate.getHeight() == targetOptions.outHeight && targetOptions.inSampleSize == 1;
    }

    // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
    // is smaller than the reusable bitmap candidate allocation byte count.
    int width = targetOptions.outWidth / targetOptions.inSampleSize;
    int height = targetOptions.outHeight / targetOptions.inSampleSize;
    int byteCount = width * height * getBytesPerPixel(candidate.getConfig());
    return byteCount <= candidate.getAllocationByteCount();
    // END_INCLUDE(can_use_for_inbitmap)
}