/** * Decodes a bitmap from a file containing it minimizing the memory use, known that the bitmap * will be drawn in a surface of reqWidth x reqHeight * * @param srcPath Absolute path to the file containing the image. * @param reqWidth Width of the surface where the Bitmap will be drawn on, in pixels. * @param reqHeight Height of the surface where the Bitmap will be drawn on, in pixels. * @return */ public static Bitmap decodeSampledBitmapFromFile(String srcPath, int reqWidth, int reqHeight) { // set desired options that will affect the size of the bitmap final Options options = new Options(); options.inScaled = true; options.inPurgeable = true; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) { options.inPreferQualityOverSpeed = false; } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { options.inMutable = false; } // make a false load of the bitmap to get its dimensions options.inJustDecodeBounds = true; BitmapFactory.decodeFile(srcPath, options); // calculate factor to subsample the bitmap options.inSampleSize = calculateSampleFactor(options, reqWidth, reqHeight); // decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(srcPath, options); }
/** * @param data 数据或路径 * @param width 目标宽 * @param height 目标高 * @return 图片 */ private Bitmap createBitmap(Object data, int width, int height) { Options options = new Options(); int scale = 1; if (width > 0 && height > 0) { // 创建目标大小的图片 options.inJustDecodeBounds = true; if (data instanceof String) { BitmapFactory.decodeFile((String) data, options); } else { BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options); } int dw = options.outWidth / width; int dh = options.outHeight / height; scale = Math.max(dw, dh); options = new Options(); } options.inDensity = DeviceInfo.getInstance().getDencity(); options.inScaled = true; options.inPurgeable = true; options.inSampleSize = scale; Bitmap bitmap = null; if (data instanceof String) { bitmap = BitmapFactory.decodeFile((String) data, options); } else { bitmap = BitmapFactory.decodeByteArray((byte[]) data, 0, ((byte[]) data).length, options); } return bitmap; }
/** * Load a picture from the resource. * * @param resources * @param resourceId * @return */ public static Bitmap loadBitmapFromResource(Resources resources, int resourceId) { // Not scale the bitmap. This will turn the bitmap in raw pixel unit. Options options = new BitmapFactory.Options(); options.inScaled = false; // Load the bitmap object. Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId, options); // Set the density to NONE. This is needed for the ImageView to not scale. bitmap.setDensity(Bitmap.DENSITY_NONE); return bitmap; }
private void copyOptions(Options srcOptions, Options destOptions) { destOptions.inDensity = srcOptions.inDensity; destOptions.inDither = srcOptions.inDither; destOptions.inInputShareable = srcOptions.inInputShareable; destOptions.inJustDecodeBounds = srcOptions.inJustDecodeBounds; destOptions.inPreferredConfig = srcOptions.inPreferredConfig; destOptions.inPurgeable = srcOptions.inPurgeable; destOptions.inSampleSize = srcOptions.inSampleSize; destOptions.inScaled = srcOptions.inScaled; destOptions.inScreenDensity = srcOptions.inScreenDensity; destOptions.inTargetDensity = srcOptions.inTargetDensity; destOptions.inTempStorage = srcOptions.inTempStorage; if (Build.VERSION.SDK_INT >= 10) copyOptions10(srcOptions, destOptions); if (Build.VERSION.SDK_INT >= 11) copyOptions11(srcOptions, destOptions); }
public void testActualStegImage() throws DecodingException { Options opts = new Options(); opts.inScaled = false; Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.steg, opts); PngStegoImage image = new PngStegoImage(); image.setImageBitmap(bitmap); image.decode(); assertTrue(image.hasEmbeddedData()); }
private TextureReferenceImpl createTextureFromResource(GL10 gl, int resourceID) { // The texture hasn't been loaded yet, so load it. TextureReferenceImpl tex = createTextureInternal(gl); Options opts = new Options(); opts.inScaled = false; Bitmap bmp = BitmapFactory.decodeResource(mRes, resourceID, opts); tex.bind(gl); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); bmp.recycle(); return tex; }