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:li.zeitgeist.android.worker.ThumbnailWorker.java

/**
 * Draw overlay on a bitmap./* ww  w .  j a v a 2  s  .  c  o m*/
 * 
 * @param bitmap
 * @param overlay
 * @return new bitmap
 */
private Bitmap drawBitmapOverlay(Bitmap bitmap, Bitmap overlay) {
    // create plain empty bitmap
    Bitmap plainBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());

    // canvas to draw on it
    Canvas canvas = new Canvas(plainBitmap);

    // draw the bitmap then the overlay
    canvas.drawBitmap(bitmap, new Matrix(), null);
    canvas.drawBitmap(overlay, new Matrix(), null);

    // recycle the original bitmap
    bitmap.recycle();

    return plainBitmap;
}

From source file:angel.zhuoxiu.picker.utils.ImageCache.java

/**
 * @param candidate - Bitmap to check/* w  ww .  j  a  va  2 s  . c  o m*/
 * @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)
}

From source file:Main.java

/**
 * Crop image bitmap from given bitmap using the given points in the original bitmap and the given rotation.<br>
 * if the rotation is not 0,90,180 or 270 degrees then we must first crop a larger area of the image that
 * contains the requires rectangle, rotate and then crop again a sub rectangle.
 *
 * @param scale how much to scale the cropped image part, use 0.5 to lower the image by half (OOM handling)
 *///from ww  w .  j  a v a 2 s. c  o m
private static Bitmap cropBitmapObjectWithScale(Bitmap bitmap, float[] points, int degreesRotated,
        boolean fixAspectRatio, int aspectRatioX, int aspectRatioY, float scale, boolean flipHorizontally,
        boolean flipVertically) {

    // get the rectangle in original image that contains the required cropped area (larger for non rectangular crop)
    Rect rect = getRectFromPoints(points, bitmap.getWidth(), bitmap.getHeight(), fixAspectRatio, aspectRatioX,
            aspectRatioY);

    if (degreesRotated == 90 || degreesRotated == 270) {
        if (flipHorizontally != flipVertically) {
            boolean temp = flipHorizontally;
            flipHorizontally = flipVertically;
            flipVertically = temp;
        }
    }

    // crop and rotate the cropped image in one operation
    float scaleX = flipHorizontally ? -scale : scale;
    float scaleY = flipVertically ? -scale : scale;

    Matrix matrix = new Matrix();
    matrix.setScale(scaleX, scaleY);
    matrix.postRotate(degreesRotated, bitmap.getWidth() / 2, bitmap.getHeight() / 2);
    Bitmap result = Bitmap.createBitmap(bitmap, rect.left, rect.top, rect.width(), rect.height(), matrix, true);

    if (result == bitmap) {
        // corner case when all bitmap is selected, no worth optimizing for it
        result = bitmap.copy(bitmap.getConfig(), false);
    }

    // rotating by 0, 90, 180 or 270 degrees doesn't require extra cropping
    if (degreesRotated % 90 != 0) {

        // extra crop because non rectangular crop cannot be done directly on the image without rotating first
        result = cropForRotatedImage(result, points, rect, degreesRotated, fixAspectRatio, aspectRatioX,
                aspectRatioY);
    }

    return result;
}

From source file:ar.com.lapotoca.resiliencia.gallery.util.ImageCache.java

/**
 * @param candidate - Bitmap to check/*from  w  w  w  . java2  s .  c  om*/
 * @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 (Build.VERSION.SDK_INT < VERSION_CODES.KITKAT) {
        // 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)
}

From source file:com.norika.android.library.bitmapfun.util.ImageCache.java

/**
 * @param candidate - Bitmap to check/*w ww  .j a  v a 2  s  . co  m*/
 * @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) {

    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();
}

From source file:com.slack.slackteam.cache.ImageCache.java

/**
 * @param candidate - Bitmap to check//from   w  w w . ja va  2 s .c  o m
 * @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 (!SLUtils.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)
}

From source file:com.ccz.viewimages.displayingbitmaps.util.ImageCache.java

/**
 * @param candidate - Bitmap to check/*from   w ww  .ja va 2 s .  c o  m*/
 * @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 (!FlavorUtils.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)
}

From source file:com.yooiistudios.newskit.core.cache.volley.ImageCache.java

/**
 * @param candidate - Bitmap to check/*from  w  w  w . j  a va 2s. co  m*/
 * @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 (!Device.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)
}

From source file:com.android.gallery3d.data.UriImage.java

public Bitmap drawTextToBitmap(Context gContext, String gText, Bitmap bitmap) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//  www. j av  a2 s.  c om
    // resource bitmaps are imutable, 
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (25 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.setTextAlign(Align.CENTER);

    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;

    canvas.drawText(gText, x * scale, y * scale, paint);

    return bitmap;
}

From source file:com.common.library.bitmap.ImageCache.java

/**
 * @param candidate - Bitmap to check/* w  ww .ja  va2 s. c o m*/
 * @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 (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        // 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)
}