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
public static Bitmap resizeBitmapToImageView(String path) { Bitmap resizedBitmap = null;/*from w w w . j av a 2 s . co m*/ try { int inWidth; int inHeight; int dstWidth = 1000; int dstHeight = 1000; InputStream in = new FileInputStream(path); // decode image size (decode metadata only, not the whole image) BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); // save width and height inWidth = options.outWidth; inHeight = options.outHeight; // decode full image pre-resized in = new FileInputStream(path); options = new BitmapFactory.Options(); // calc rought re-size (this is no exact resize) options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight); // decode full image Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); // calc exact destination size Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); // resize bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); // save image // try { // FileOutputStream out = new FileOutputStream(path); // resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); // } catch (Exception e) { //// Log.e("Image", e.getMessage(), e); // } } catch (IOException e) { // Log.e("Image", e.getMessage(), e); } return resizedBitmap; }
From source file:Main.java
public static boolean decodeImageBounds(InputStream stream, int[] outSize, BitmapFactory.Options options) { BitmapFactory.decodeStream(stream, null, options); if ((options.outHeight > 0) && (options.outWidth > 0)) { outSize[0] = options.outWidth;//from ww w. j ava2 s.c o m outSize[1] = options.outHeight; return true; } return false; }
From source file:Main.java
public static Bitmap getThumbnail(Context context, Uri uri, int size) throws FileNotFoundException, IOException { InputStream input = context.getContentResolver().openInputStream(uri); BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds = true; onlyBoundsOptions.inDither = true;//optional onlyBoundsOptions.inPreferredConfig = Config.ARGB_8888;//optional BitmapFactory.decodeStream(input, null, onlyBoundsOptions); input.close();// w ww .j av a 2 s .c o m if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) return null; int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth; double ratio = (originalSize > size) ? (originalSize / size) : 1.0; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); bitmapOptions.inDither = true;//optional bitmapOptions.inPreferredConfig = Config.ARGB_8888;//optional input = context.getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); input.close(); return bitmap; }
From source file:Main.java
private static void saveSmallerImage(String pathOfInputImage, int dstWidth, int dstHeight) { try {/*w w w .j av a 2 s . c o m*/ int inWidth = 0; int inHeight = 0; InputStream in = new FileInputStream(pathOfInputImage); // decode image size (decode metadata only, not the whole image) BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); in = null; // save width and height inWidth = options.outWidth; inHeight = options.outHeight; // decode full image pre-resized in = new FileInputStream(pathOfInputImage); options = new BitmapFactory.Options(); // calc rought re-size (this is no exact resize) options.inSampleSize = Math.max(inWidth / dstWidth, inHeight / dstHeight); // decode full image Bitmap roughBitmap = BitmapFactory.decodeStream(in, null, options); // calc exact destination size Matrix m = new Matrix(); RectF inRect = new RectF(0, 0, roughBitmap.getWidth(), roughBitmap.getHeight()); RectF outRect = new RectF(0, 0, dstWidth, dstHeight); m.setRectToRect(inRect, outRect, Matrix.ScaleToFit.CENTER); float[] values = new float[9]; m.getValues(values); // resize bitmap Bitmap resizedBitmap = Bitmap.createScaledBitmap(roughBitmap, (int) (roughBitmap.getWidth() * values[0]), (int) (roughBitmap.getHeight() * values[4]), true); // save image try { FileOutputStream out = new FileOutputStream(pathOfInputImage); resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out); } catch (Exception e) { Log.e("Image", e.getMessage(), e); } } catch (IOException e) { Log.e("Image", e.getMessage(), e); } catch (Exception e) { Log.e("Image", e.getMessage()); } }
From source file:Main.java
public static Bitmap revitionImageSize(String path, int width, int height) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//from w w w . j a v a2s. co m BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; Bitmap bitmap = null; while (true) { if ((options.outWidth >> i <= width) && (options.outHeight >> i <= height)) { in = new BufferedInputStream(new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } return bitmap; }
From source file:Main.java
/** * decode the image bitmap according to specified size * // w w w . jav a 2 s . co m * @param f * @param maxSize * @return */ public static Bitmap decodeFile(File f, int maxSize) { Bitmap b = null; try { // Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; FileInputStream fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); fis.close(); int scale = 1; if (o.outHeight > maxSize || o.outWidth > maxSize) { scale = (int) Math.pow(2, (int) Math .round(Math.log(maxSize / (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(f); b = BitmapFactory.decodeStream(fis, null, o2); fis.close(); } catch (IOException e) { } return b; }
From source file:Main.java
public static Bitmap getSampledBitmap(Context context, String imagePath, boolean isDefault, int reqWidth, int reqHeight) { Bitmap sampledBitmap = null;/*from w w w. j a va 2s .c o m*/ InputStream inputStream = null; try { if (isDefault) { inputStream = context.getAssets().open(imagePath); } else { inputStream = new FileInputStream(imagePath); } BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; if (!isDefault) { inputStream.close(); inputStream = new FileInputStream(imagePath); } sampledBitmap = BitmapFactory.decodeStream(inputStream, null, options); // inputStream.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return sampledBitmap; }
From source file:Main.java
/** * Tells the decoder to sub sample the image, loading a smaller version into memory. * * @param inputStream The stream containing the image to decode * @param reqWidth Required width of the final bitmap * @param reqHeight Required height of the final bitmap * @return Bitmap scaled to the required width and height *//*ww w.jav a 2 s. co m*/ public static Bitmap decodeSampledBitmapFromStream(InputStream inputStream, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(inputStream, null, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeStream(inputStream, null, options); }
From source file:Main.java
public static Bitmap getCompressedBitmap(InputStream is, int sampleSize) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = sampleSize;/*from w ww. j a v a 2 s .c om*/ try { return BitmapFactory.decodeStream(is, null, options); } catch (OutOfMemoryError e) { Log.e("talktab.error", "OutOfMemoryError"); options.inSampleSize = sampleSize * 2; return BitmapFactory.decodeStream(is, null, options); } }
From source file:Main.java
/** Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size, * while staying as least as large as the width and height parameters. *//*from w ww . j a va2 s. c o m*/ public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height) throws FileNotFoundException { BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI); options.inJustDecodeBounds = false; float wratio = 1.0f * options.outWidth / width; float hratio = 1.0f * options.outHeight / height; options.inSampleSize = (int) Math.min(wratio, hratio); return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options); }