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:Main.java

public static Bitmap convertToMutable(Bitmap srcBitmap, String cacheDirPath, String tempFileName) {
    try {/*  w w  w . ja  v  a  2 s  .c o m*/
        // 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.
        int index = tempFileName.lastIndexOf(".");
        if (index != -1)
            tempFileName = tempFileName.substring(0, index);
        File file = new File(cacheDirPath + File.separator + tempFileName + ".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 = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();
        Config type = srcBitmap.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, srcBitmap.getRowBytes() * height);
        srcBitmap.copyPixelsToBuffer(map);
        // recycle the source bitmap, this will be no longer used.
        srcBitmap.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.
        srcBitmap = Bitmap.createBitmap(width, height, type);
        map.position(0);
        // load it back from temporary
        srcBitmap.copyPixelsFromBuffer(map);
        // close the temporary file and channel , then delete that also
        channel.close();
        randomAccessFile.close();

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

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

    return srcBitmap;
}

From source file:com.forrestguice.suntimeswidget.SuntimesUtils.java

/**
 * Creates a tinted copy of the supplied bitmap.
 * @param b a bitmap image//from w  w  w.  ja v a  2  s.c o  m
 * @param color a color
 * @return a bitmap tinted to color
 */
public static Bitmap tintBitmap(Bitmap b, int color) {
    Bitmap tinted = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
    Canvas c = new Canvas(tinted);
    Paint p = new Paint();
    p.setColorFilter(new LightingColorFilter(color, 0));
    c.drawBitmap(b, 0, 0, p);
    return tinted;
}

From source file:jahirfiquitiva.iconshowcase.utilities.Utils.java

@SuppressWarnings("ConstantConditions")
public static Bitmap getBitmapWithReplacedColor(@NonNull Bitmap bitmap, @ColorInt int colorToReplace,
        @ColorInt int replaceWith) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

    int minX = width;
    int minY = height;
    int maxX = -1;
    int maxY = -1;

    Bitmap newBitmap = Bitmap.createBitmap(width, height, bitmap.getConfig());
    int pixel;/*from  ww w  .j  av a 2  s . c o m*/

    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            int index = y * width + x;
            pixel = pixels[index];
            if (pixel == colorToReplace) {
                pixels[index] = replaceWith;
            }
            if (pixels[index] != replaceWith) {
                if (x < minX)
                    minX = x;
                if (x > maxX)
                    maxX = x;
                if (y < minY)
                    minY = y;
                if (y > maxY)
                    maxY = y;
            }
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);

    return Bitmap.createBitmap(newBitmap, minX, minY, (maxX - minX) + 1, (maxY - minY) + 1);
}

From source file:com.google.android.libraries.cast.companionlibrary.utils.Utils.java

/**
 * Scale and center-crop a bitmap to fit the given dimensions.
 *///w  w w. ja  v  a  2s  . c  om
public static Bitmap scaleAndCenterCropBitmap(Bitmap source, int newHeight, int newWidth) {
    if (source == null) {
        return null;
    }
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    Bitmap destination = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(destination);
    canvas.drawBitmap(source, null, targetRect, null);

    return destination;
}

From source file:com.bluetech.gallery5.util.ImageCache.java

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

    // 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.getByteCount();
    //END_INCLUDE(can_use_for_inbitmap)
}

From source file:com.sqkj.engine.image.ImageCache.java

private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    //BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!SdkUtils.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   w w  w.  j  a va  2  s .  c  om
    // 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.chanlytech.unicorn.cache.ImageCache.java

/**
 * @param candidate/*from ww  w  . ja  va 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>
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    //BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!AndroidVersion.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.higgses.griffin.cache.ImageCache.java

/**
 * @param candidate//w  w w. j a  va2  s.c om
 *         - 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(Build.VERSION_CODES.KITKAT)
private static boolean canUseForInBitmap(Bitmap candidate, BitmapFactory.Options targetOptions) {
    //BEGIN_INCLUDE(can_use_for_inbitmap)
    if (!GriUAndroidVersion.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.seun.gallery.util.ImageCache.java

/**
 * @param candidate     - 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>
 *//* w w w . jav a  2s  .c om*/
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.getRowBytes() * candidate.getHeight());
    //END_INCLUDE(can_use_for_inbitmap)
}

From source file:io.indy.seni.util.ImageCache.java

/**
 * @param candidate - Bitmap to check//from   w  w  w . j  av a 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) {

    if (!AppConfig.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();
}