Example usage for android.graphics BitmapFactory decodeResource

List of usage examples for android.graphics BitmapFactory decodeResource

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeResource.

Prototype

public static Bitmap decodeResource(Resources res, int id, Options opts) 

Source Link

Document

Synonym for opening the given resource and calling #decodeResourceStream .

Usage

From source file:Main.java

public static Bitmap loadImage(int bitmapId, Resources resources, int tries) {
    Bitmap bitmap;// www  .  ja v  a2  s  . c o m

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inDither = true;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        bitmap = BitmapFactory.decodeResource(resources, bitmapId, options);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();

        System.gc();

        if (--tries > 0) {
            return loadImage(bitmapId, resources, tries);
        } else {
            return null;
        }
    }

    return bitmap;
}

From source file:Main.java

public static int loadTexture(final Context context, final int resourceId) {
    final int[] textureHandle = new int[1];

    GLES20.glGenTextures(1, textureHandle, 0);

    if (textureHandle[0] != 0) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false; // No pre-scaling

        // Read in the resource
        final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);

        // Bind to the texture in OpenGL
        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);

        // Set filtering
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
        GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);

        // Load the bitmap into the bound texture.
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);

        // Recycle the bitmap, since its data has been loaded into OpenGL.
        bitmap.recycle();/*w  ww  . j a v  a  2s. co m*/
    }

    if (textureHandle[0] == 0) {
        throw new RuntimeException("Error loading texture.");
    }

    return textureHandle[0];
}

From source file:Main.java

public static Bitmap getBitmapFromResource(int resourceId, Resources res, Integer sampleSize) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = sampleSize == null ? 1 : sampleSize;
    options.outMimeType = "image/jpeg";
    options.inDither = false;/*w  ww .java 2 s . co m*/
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    options.inTempStorage = new byte[256];

    return BitmapFactory.decodeResource(res, resourceId, options);
}

From source file:Main.java

public static Bitmap decodeResource(Context paramContext, Resources paramResources, int paramInt,
        BitmapFactory.Options paramOptions) {
    try {/*from  w  w w . ja v a 2 s .c o  m*/
        Bitmap localBitmap = BitmapFactory.decodeResource(paramResources, paramInt, paramOptions);
        return localBitmap;
    } catch (OutOfMemoryError localOutOfMemoryError) {
    }
    return null;
}

From source file:Main.java

/**
 * Cut Bitmap/*from ww w .java  2s  . c o m*/
 * Bitmap memory size is adjusted depending on input dimensions
 * decodeCropBitmapFromResource
 */
public static Bitmap decodeCropBitmapFromResource(Resources res, int resId, int x, int y, int width,
        int height) {

    Bitmap bitmap;
    Bitmap croppedBitmap;
    final BitmapFactory.Options options = new BitmapFactory.Options();

    // First decode with inJustDecodeBounds=true to check dimensions
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    // Enables bitmap re-usage
    options.inMutable = true;

    /* EFFICIENT Crop ? - Creates new Bitmap */// <- Best option?
    bitmap = BitmapFactory.decodeResource(res, resId, options);
    croppedBitmap = Bitmap.createBitmap(bitmap, x, y, width, height);
    bitmap.recycle();

    /* EFFICIENT Crop ? - Reuses bitmap, but it is slow */
    //        croppedBitmap = BitmapFactory.decodeResource(res, resId, options);
    //        int[] pixels = getPixelsFromBitmap(croppedBitmap,
    //                x, y,
    //                width, height);
    //        croppedBitmap.setPixels(pixels, 0, width,
    //                0, 0,
    //                width, height);

    return croppedBitmap;
}

From source file:Main.java

/**
 * DETERIORATE QUALITY Bitmap//  w w  w .ja va 2 s .  c  o m
 * Bitmap memory size is adjusted depending on input dimensions
 * Bitmap memory size is cut to half
 * decodePixelDeteriorationBitmapFromResource
 */
public static Bitmap decodePixelDeteriorationBitmapFromResource(Resources res, int resId, int reqWidth,
        int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565; // 2 bytes per pixel, not 4
    options.inDither = true;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static Bitmap decodeBitmap(Context context, int resId) {
    checkParam(context);//from  ww w .  j  a v a 2 s .c  om
    return BitmapFactory.decodeResource(context.getResources(), resId, getBitmapOptions(context));
}

From source file:Main.java

/**
 * This will return a bitmap that is loaded and appropriately scaled from
 * the application resources./*  ww  w .j a  va 2s .com*/
 */
@SuppressWarnings("unused")
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int width, int height) {
    if (resId == 0) {
        return null;
    }

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // If either width or height is passed in as 0, then use the actual
    // stored image dimension.
    if (width == 0) {
        width = options.outWidth;
    }
    if (height == 0) {
        height = options.outHeight;
    }

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, width, height);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

From source file:Main.java

public static Bitmap loadEmptyBitmap(Context context, int resId) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// w  w w  .j a  va  2 s .  co m
    BitmapFactory.decodeResource(context.getResources(), resId, options);
    Bitmap empty_bitmap = Bitmap.createBitmap(options.outWidth, options.outHeight, Config.ALPHA_8);
    return empty_bitmap;
}

From source file:Main.java

/**
 * Decodes the resource to full image or it's sub-sampled version.
 * @param res Application resources.//  ww w.  j a va  2 s.  c  o m
 * @param resId Resource ID being loaded.
 * @param reqWidth Width used to sub-sample image.
 * @param reqHeight Height used to sub-sample image.
 * @return
 */
public static Bitmap decodeSampledBitmapFromPath(Resources res, int resId, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}