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:Main.java
/** * Returns a BitmapFactory.Options object containing the size of the image at the given URI, * without actually loading the image.//from w ww . ja v a 2 s . com */ public static BitmapFactory.Options computeBitmapSizeFromURI(Context context, Uri imageURI) throws FileNotFoundException { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); if (bitmap != null) { bitmap.recycle(); } return options; }
From source file:Main.java
public static Point getImageDimensions(File f) throws FileNotFoundException { BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true;/*from w w w . j a v a2 s .c o m*/ BitmapFactory.decodeStream(new FileInputStream(f), null, o); return new Point(o.outWidth, o.outHeight); }
From source file:Main.java
/** * Returns a mutable bitmap from a uri.// w w w. j av a 2 s . c o m * * @param uri Uri to the file * @return A mutable copy of the decoded {@link android.graphics.Bitmap}; null if failed. * @throws java.io.IOException if unable to make it mutable */ public static Bitmap decodeUri(final ContentResolver resolver, final Uri uri) throws IOException { BitmapFactory.Options opt = new BitmapFactory.Options(); opt.inJustDecodeBounds = false; opt.inMutable = true; return BitmapFactory.decodeStream(resolver.openInputStream(uri), null, opt); }
From source file:Main.java
/** * Returns the bitmap from the given uri loaded using the given options. * Returns null on failure./* www.j a v a 2 s .c o m*/ */ public static Bitmap loadBitmap(Context context, InputStream is, BitmapFactory.Options o) { try { return BitmapFactory.decodeStream(is, null, o); } finally { closeSilently(is); } }
From source file:Main.java
public static Bitmap loadBitmapDirectStream(String url, BitmapFactory.Options opt) { Bitmap bitmap = null;//from www . j av a2 s .co m InputStream is = null; try { is = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE); bitmap = BitmapFactory.decodeStream(is, null, opt); } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, "IOException in loadBitmap"); e.printStackTrace(); } catch (OutOfMemoryError e) { // TODO Auto-generated catch block Log.e(TAG, "OutOfMemoryError in loadBitmap"); e.printStackTrace(); } finally { closeStream(is); } return bitmap; }
From source file:Main.java
public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; while (baos.toByteArray().length / 1024 > 100) { options -= 10;/*from w ww .j a va 2 s. c o m*/ if (options > 0) { baos.reset(); image.compress(Bitmap.CompressFormat.JPEG, options, baos); } } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); return BitmapFactory.decodeStream(isBm, null, null); }
From source file:Main.java
/** * @param url//from w w w.j a v a 2 s . c o m * @param sampleSize * @return * @throws FileNotFoundException * @throws IOException */ public static Bitmap downsampleBitmap(URL url, int sampleSize) throws FileNotFoundException, IOException { Bitmap resizedBitmap; BitmapFactory.Options outBitmap = new BitmapFactory.Options(); outBitmap.inJustDecodeBounds = false; // the decoder will return a bitmap outBitmap.inSampleSize = sampleSize; InputStream is = url.openStream(); resizedBitmap = BitmapFactory.decodeStream(is, null, outBitmap); is.close(); return resizedBitmap; }
From source file:Main.java
/** * Converts an inputstream into a bitmap using the BITMAP_SAMPLE_SIZE; * @param in the inputstream containing a bitmap * @return the bitmap// w w w . j av a 2 s. c o m */ public static Bitmap convertBitmap(InputStream in, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize; return BitmapFactory.decodeStream(in, null, options); }
From source file:Main.java
public static Bitmap compressBmpFromBmp(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); int options = 100; image.compress(Bitmap.CompressFormat.JPEG, 100, baos); while (baos.toByteArray().length / 1024 > 100) { baos.reset();/*from ww w. j a v a2s .co m*/ options -= 10; image.compress(Bitmap.CompressFormat.JPEG, options, baos); } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; }
From source file:Main.java
public static boolean rotateBitmap(File inFile, File outFile, int angle) throws FileNotFoundException, IOException { // Declare//from w w w . ja v a 2 s . c om FileInputStream inStream = null; FileOutputStream outStream = null; // Create options BitmapFactory.Options options = new BitmapFactory.Options(); // Create transform matrix Matrix matrix = new Matrix(); matrix.postRotate(angle); // Increment inSampleSize progressively to reduce image resolution and size. If // the program is properly managing memory, and you don't have other large images // loaded in memory, this loop will generally not need to go through more than 3 // iterations. To be safe though, we stop looping after a certain amount of tries // to avoid infinite loops for (options.inSampleSize = 1; options.inSampleSize <= 32; options.inSampleSize++) { try { // Load the bitmap from file inStream = new FileInputStream(inFile); Bitmap originalBitmap = BitmapFactory.decodeStream(inStream, null, options); // Rotate the bitmap Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); // Save the rotated bitmap outStream = new FileOutputStream(outFile); rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream); outStream.close(); // Recycle the bitmaps to immediately free memory originalBitmap.recycle(); originalBitmap = null; rotatedBitmap.recycle(); rotatedBitmap = null; // Return return true; } catch (OutOfMemoryError e) { // If an OutOfMemoryError occurred, we continue with for loop and next inSampleSize value } finally { // Clean-up if we failed on save if (outStream != null) { try { outStream.close(); } catch (IOException e) { } } } } // Failed return false; }