List of usage examples for android.graphics BitmapFactory decodeStream
@Nullable public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts)
From source file:com.klinker.android.dream.util.NetworkUtils.java
/** * Load a bitmap from the given location url * @param location the url of the bitmap * @return the bitmap from the url//from w ww . j av a 2 s.c o m * @throws Throwable */ public static Bitmap loadBitmap(String location) throws Throwable { if (location.equals("")) { return null; } else if (!(location.startsWith("http:") || location.startsWith("https:") || location.startsWith("www."))) { location = "http:" + location; } location = location.replace(" ", "%20"); URL url = new URL(location); // use a recycled bitmap BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferQualityOverSpeed = true; options.inBitmap = BitmapHelper.getCurrentBitmap(); return BitmapFactory.decodeStream(url.openConnection().getInputStream(), null, options); }
From source file:com.maskyn.fileeditorpro.util.AccessStorageApi.java
public static Bitmap loadPrescaledBitmap(String filename) throws IOException { // Facebook image size final int IMAGE_MAX_SIZE = 630; File file = null;//from w ww . j ava2 s . co m FileInputStream fis; BitmapFactory.Options opts; int resizeScale; Bitmap bmp; file = new File(filename); // This bit determines only the width/height of the bitmap without loading the contents opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; fis = new FileInputStream(file); BitmapFactory.decodeStream(fis, null, opts); fis.close(); // Find the correct scale value. It should be a power of 2 resizeScale = 1; if (opts.outHeight > IMAGE_MAX_SIZE || opts.outWidth > IMAGE_MAX_SIZE) { resizeScale = (int) Math.pow(2, (int) Math.round( Math.log(IMAGE_MAX_SIZE / (double) Math.max(opts.outHeight, opts.outWidth)) / Math.log(0.5))); } // Load pre-scaled bitmap opts = new BitmapFactory.Options(); opts.inSampleSize = resizeScale; fis = new FileInputStream(file); bmp = BitmapFactory.decodeStream(fis, null, opts); fis.close(); return bmp; }
From source file:Main.java
public static int findScaleFactor(int targetW, int targetH, InputStream is) { // Get the dimensions of the bitmap BitmapFactory.Options bmOptions = new BitmapFactory.Options(); bmOptions.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, bmOptions); int actualW = bmOptions.outWidth; int actualH = bmOptions.outHeight; // Determine how much to scale down the image return Math.min(actualW / targetW, actualH / targetH); }
From source file:Main.java
public static Bitmap decodeFile(File theFile, int IMAGE_MAX_SIZE) { Bitmap bmp = null;/*ww w . j av a2 s .c o m*/ try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(theFile); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { scale = (int) Math.pow(2, (int) Math.round( Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } // Decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; fis = new FileInputStream(theFile); bmp = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { } return bmp; }
From source file:Main.java
private static Bitmap createScaledBitmap(File f, int rotation, int scale) throws FileNotFoundException { BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale;// w ww .j a va2 s.co m Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(f), null, o2); if (0 < rotation) { Matrix matrix = new Matrix(); matrix.postRotate(rotation); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); if (rotatedBitmap != bitmap) { bitmap.recycle(); bitmap = null; } return rotatedBitmap; } return bitmap; }
From source file:Main.java
/** * Reads <tt>Bitmap</tt> from given <tt>uri</tt> using * <tt>ContentResolver</tt>. Output image is scaled to given * <tt>reqWidth</tt> and <tt>reqHeight</tt>. Output size is not guaranteed * to match exact given values, because only powers of 2 are used as scale * factor. Algorithm tries to scale image down as long as the output size * stays larger than requested value./* w w w .jav a 2s . c o m*/ * * @param ctx the context used to create <tt>ContentResolver</tt>. * @param uri the <tt>Uri</tt> that points to the image. * @param reqWidth requested width. * @param reqHeight requested height. * @return <tt>Bitmap</tt> from given <tt>uri</tt> retrieved using * <tt>ContentResolver</tt> and down sampled as close as possible to match * requested width and height. * @throws IOException */ public static Bitmap scaledBitmapFromContentUri(Context ctx, Uri uri, int reqWidth, int reqHeight) throws IOException { InputStream imageStream = null; try { // First decode with inJustDecodeBounds=true to check dimensions imageStream = ctx.getContentResolver().openInputStream(uri); if (imageStream == null) return null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(imageStream, null, options); imageStream.close(); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; imageStream = ctx.getContentResolver().openInputStream(uri); return BitmapFactory.decodeStream(imageStream, null, options); } finally { if (imageStream != null) { imageStream.close(); } } }
From source file:com.exzogeni.dk.graphics.Bitmaps.java
@Nullable public static Bitmap decodeStream(@NonNull InputStream stream, Rect outPadding, int hwSize) { if (hwSize > 0) { final InputStream localIn = new BufferPoolInputStream(stream); try {//from w w w .j ava2s . c o m final BitmapFactory.Options ops = new BitmapFactory.Options(); ops.inJustDecodeBounds = true; localIn.mark(BITMAP_HEAD); BitmapFactory.decodeStream(localIn, outPadding, ops); ops.inSampleSize = calculateInSampleSize(ops, hwSize); ops.inJustDecodeBounds = false; localIn.reset(); return BitmapFactory.decodeStream(localIn, outPadding, ops); } catch (IOException e) { Logger.error(e); } finally { IOUtils.closeQuietly(localIn); } return null; } return BitmapFactory.decodeStream(stream); }
From source file:Main.java
public static Bitmap compressBitmap(Context context, InputStream is, int maxWidth, int maxHeight) { checkParam(context);//from w w w.jav a 2s . co m checkParam(is); BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = true; BitmapFactory.decodeStream(is, null, opt); int height = opt.outHeight; int width = opt.outWidth; int sampleSize = computeSampleSize(width, height, maxWidth, maxHeight); BitmapFactory.Options options = getBitmapOptions(context); options.inSampleSize = sampleSize; return BitmapFactory.decodeStream(is, null, options); }
From source file:Main.java
public static Bitmap decodeFile(File file, int size, boolean square) { try {/*from w w w . j a v a 2 s . co m*/ //decode image size BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inJustDecodeBounds = true; Bitmap bitmapSize = BitmapFactory.decodeStream(new FileInputStream(file), null, bitmapOptions); //Find the correct scale value. It should be the power of 2. if (size > 0) { int width_tmp = bitmapOptions.outWidth, height_tmp = bitmapOptions.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < size || height_tmp / 2 < size) break; width_tmp /= 2; height_tmp /= 2; scale++; } //decode with inSampleSize bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = scale; bitmapOptions.inScaled = true; bitmapSize = null; if (square) { return cropToSquare(BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions)); } return BitmapFactory.decodeFile(file.getAbsolutePath(), bitmapOptions); } return null; } catch (FileNotFoundException e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * A safer decodeStream method/*from w w w . j a va 2 s. co m*/ * rather than the one of {@link BitmapFactory} which will be easy to get OutOfMemory Exception * while loading a big image file. * * @param uri * @param width * @param height * @return * @throws FileNotFoundException */ protected static Bitmap safeDecodeStream(Context context, Uri uri, int width, int height) throws FileNotFoundException { int scale = 1; // Decode image size without loading all data into memory BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; android.content.ContentResolver resolver = context.getContentResolver(); try { BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024), null, options); if (width > 0 || height > 0) { options.inJustDecodeBounds = true; int w = options.outWidth; int h = options.outHeight; while (true) { if ((width > 0 && w / 2 < width) || (height > 0 && h / 2 < height)) { break; } w /= 2; h /= 2; scale *= 2; } } // Decode with inSampleSize option options.inJustDecodeBounds = false; options.inSampleSize = scale; return BitmapFactory.decodeStream(new BufferedInputStream(resolver.openInputStream(uri), 4 * 1024), null, options); } catch (Exception e) { e.printStackTrace(); } catch (OutOfMemoryError e) { e.printStackTrace(); System.gc(); } return null; }