List of usage examples for android.graphics BitmapFactory decodeResource
public static Bitmap decodeResource(Resources res, int id, Options opts)
From source file:Main.java
public static Bitmap imageResourceToBitmap(Context c, int res, int maxDim) { Bitmap bmp = null;/* ww w.j av a 2 s . c om*/ BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; // compute the smallest size bitmap we need to read BitmapFactory.decodeResource(c.getResources(), res, opts); int w = opts.outWidth; int h = opts.outHeight; int s = 1; while (true) { if ((w / 2 < maxDim) || (h / 2 < maxDim)) { break; } w /= 2; h /= 2; s++; } // scale and read the data opts.inJustDecodeBounds = false; opts.inSampleSize = s; bmp = BitmapFactory.decodeResource(c.getResources(), res, opts); return bmp; }
From source file:Main.java
/** * <pre>// w w w . j av a 2 s. c om * Get width and height of bitmap from resource. * </pre> * @param resId resource id of bitmap * @return integer array with 2 elements: width and height */ public static int[] getBitmapSizes(int resId) { BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeResource(getCurrentContext().getResources(), resId, bmOptions); return new int[] { bmOptions.outWidth, bmOptions.outHeight }; }
From source file:Main.java
/** * Decode and sample down a bitmap from resources to the requested width and height. * * @param res The resources object containing the image data * @param resId The resource id of the image data * @param reqWidth The requested width of the resulting bitmap * @param reqHeight The requested height of the resulting bitmap * @return A bitmap sampled down from the original with the same aspect ratio and dimensions * that are equal to or greater than the requested width and height *///from w w w . ja v a2 s . co m public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // BEGIN_INCLUDE (read_bitmap_dimensions) // 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); // END_INCLUDE (read_bitmap_dimensions) // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }
From source file:Main.java
public static Bitmap fix(Context context, int rid, int w, int h) { BitmapFactory.Options options = new Options(); options.inJustDecodeBounds = true;/*from w ww . j a v a 2 s .c o m*/ BitmapFactory.decodeResource(context.getResources(), rid, options); int imageH = options.outHeight; int imageW = options.outWidth; int scaleX = imageH / w; int scaleY = imageW / h; int scale = 1; if (scaleX >= scaleY & scaleY >= 1) { scale = scaleX; } if (scaleY >= scaleX & scaleX >= 1) { scale = scaleY; } options.inJustDecodeBounds = false; options.inSampleSize = scale; return BitmapFactory.decodeResource(context.getResources(), rid, options); }
From source file:Main.java
/** * Returns a mutable bitmap from a resource. * * @param context Everything needs a context =( * @return A mutable copy of the resource *///from w w w . j av a2 s .c om public static Bitmap decodeResource(final Context context, final String resourceName) { Resources resources = context.getResources(); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inMutable = true; return BitmapFactory.decodeResource(resources, getResourceIdentifierForDrawable(context, resourceName), opt); }
From source file:Main.java
public static Bitmap getDrawableByName(Context ctx, String name) { BitmapFactory.Options options = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), ctx.getResources().getIdentifier(name, "drawable", ctx.getPackageName()), options); return bitmap; }
From source file:Main.java
/** * Retrieve a dithered bitmap that can be used for comparison on any density * @param resources/*from w w w. jav a 2 s . c om*/ * @param config the preferred config for the returning bitmap * @return the {@link Bitmap} or <code>null</code> */ public static Bitmap getUnscaledAndDitheredBitmap(Resources resources, int resId, Bitmap.Config config) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inDither = true; options.inScaled = false; options.inPreferredConfig = config; return BitmapFactory.decodeResource(resources, resId, options); }
From source file:Main.java
public static int loadTexture(final Context context, final int resourceId, int[] size) { final int texId = genTexture(); if (texId != 0) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inScaled = false; // No pre-scaling options.inJustDecodeBounds = true; // Just decode bounds BitmapFactory.decodeResource(context.getResources(), resourceId, options); // Set return size size[0] = options.outWidth;/* w w w . j av a 2 s.c o m*/ size[1] = options.outHeight; // Decode options.inJustDecodeBounds = false; Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options); // 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(); } return texId; }
From source file:Main.java
public static Bitmap decodeResource(Resources res, int resId, int reqWidth, int reqHeight) { BitmapFactory.Options opts = new BitmapFactory.Options(); // Determine insample size opts.inJustDecodeBounds = true;/* w w w .ja v a 2s . c om*/ BitmapFactory.decodeResource(res, resId, opts); opts.inSampleSize = calculateInSampleSize(opts, reqWidth, reqHeight); // Decode the bitmap, regionally if necessary Bitmap bitmap = null; opts.inJustDecodeBounds = false; Rect rect = getCropRectIfNecessary(opts, reqWidth, reqHeight); InputStream stream = null; try { if (rect != null) { stream = res.openRawResource(resId, new TypedValue()); if (stream == null) return null; BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(stream, false); bitmap = decoder.decodeRegion(rect, opts); } else { bitmap = BitmapFactory.decodeResource(res, resId, opts); } } catch (IOException e) { Log.e(TAG, "Unable to open resource " + resId, e); } finally { closeSilently(stream); } return bitmap; }
From source file:Main.java
public static Bitmap getDefaultArtwork(Context context, int id, int w, int h) { BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options(); Bitmap b = null;/*from w w w . jav a2 s . c o m*/ int sampleSize = 1; sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.ARGB_8888; // 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.decodeResource(context.getResources(), id, 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; b = BitmapFactory.decodeResource(context.getResources(), id, 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; }