Example usage for android.graphics BitmapFactory decodeStream

List of usage examples for android.graphics BitmapFactory decodeStream

Introduction

In this page you can find the example usage for android.graphics BitmapFactory decodeStream.

Prototype

@Nullable
public static Bitmap decodeStream(@Nullable InputStream is, @Nullable Rect outPadding, @Nullable Options opts) 

Source Link

Document

Decode an input stream into a bitmap.

Usage

From source file:Main.java

private static Bitmap loadAssetBitmap(Context context, String assetPath) {
    InputStream is = null;/*from   w  w w  .  j  a  v a2s  . c  o  m*/
    try {
        Resources resources = context.getResources();
        Options options = new Options();
        options.inDensity = DisplayMetrics.DENSITY_HIGH;
        options.inScreenDensity = resources.getDisplayMetrics().densityDpi;
        options.inTargetDensity = resources.getDisplayMetrics().densityDpi;
        is = context.getAssets().open(assetPath);
        Bitmap bitmap = BitmapFactory.decodeStream(is, new Rect(), options);
        if (bitmap != null) {
            drawableCache.put(assetPath, bitmap);
        }
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return null;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(File file, int reqWidth, int reqHeight) throws IOException {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  ww  w  .j a  va 2 s .  co m
    InputStream stream = new FileInputStream(file);
    BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    stream = new FileInputStream(file);
    Bitmap bmp = BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    return bmp;
}

From source file:Main.java

public static Bitmap decodeFileImg(File f, int requiredSize) {
    try {//www  .ja  v a2 s  . com
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);
        // The new size we want to scale to
        final int REQUIRED_SIZE = requiredSize;
        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;//from w  w w.  j av  a2s. c  om

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(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;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromSteam(InputStream stream, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// www  .jav a 2s . c  om
    BitmapFactory.decodeStream(stream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    try {
        stream.reset();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(stream, null, options);
}

From source file:Main.java

public static Bitmap loadSizeLimitedBitmapFromUri(Uri imageUri, ContentResolver contentResolver) {
    try {//from  w  w w . j a  va  2  s. co m
        // Load the image into InputStream.
        InputStream imageInputStream = contentResolver.openInputStream(imageUri);

        // For saving memory, only decode the image meta and get the side length.
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Rect outPadding = new Rect();
        BitmapFactory.decodeStream(imageInputStream, outPadding, options);

        // Calculate shrink rate when loading the image into memory.
        int maxSideLength = options.outWidth > options.outHeight ? options.outWidth : options.outHeight;
        options.inSampleSize = 1;
        options.inSampleSize = calculateSampleSize(maxSideLength, IMAGE_MAX_SIDE_LENGTH);
        options.inJustDecodeBounds = false;
        imageInputStream.close();

        // Load the bitmap and resize it to the expected size length
        imageInputStream = contentResolver.openInputStream(imageUri);
        Bitmap bitmap = BitmapFactory.decodeStream(imageInputStream, outPadding, options);
        maxSideLength = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() : bitmap.getHeight();
        double ratio = IMAGE_MAX_SIDE_LENGTH / (double) maxSideLength;
        if (ratio < 1) {
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * ratio),
                    (int) (bitmap.getHeight() * ratio), false);
        }

        return rotateBitmap(bitmap, getImageRotationAngle(imageUri, contentResolver));
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap loadSizeLimitedBitmapFromUri(Uri imageUri, ContentResolver contentResolver) {
    try {/*  w ww. j  a  v a  2s . c om*/
        // Load the image into InputStream.
        InputStream imageInputStream = contentResolver.openInputStream(imageUri);

        // For saving memory, only decode the image meta and get the side length.
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Rect outPadding = new Rect();
        BitmapFactory.decodeStream(imageInputStream, outPadding, options);

        // Calculate shrink rate when loading the image into memory.
        int maxSideLength = options.outWidth > options.outHeight ? options.outWidth : options.outHeight;
        options.inSampleSize = 1;
        options.inSampleSize = calculateSampleSize(maxSideLength, IMAGE_MAX_SIDE_LENGTH);
        options.inJustDecodeBounds = false;
        if (imageInputStream != null) {
            imageInputStream.close();
        }

        // Load the bitmap and resize it to the expected size length
        imageInputStream = contentResolver.openInputStream(imageUri);
        Bitmap bitmap = BitmapFactory.decodeStream(imageInputStream, outPadding, options);
        maxSideLength = bitmap.getWidth() > bitmap.getHeight() ? bitmap.getWidth() : bitmap.getHeight();
        double ratio = IMAGE_MAX_SIDE_LENGTH / (double) maxSideLength;
        if (ratio < 1) {
            bitmap = Bitmap.createScaledBitmap(bitmap, (int) (bitmap.getWidth() * ratio),
                    (int) (bitmap.getHeight() * ratio), false);
        }

        return rotateBitmap(bitmap, getImageRotationAngle(imageUri, contentResolver));
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/**
 * @param file// www .jav a2  s  .  c o  m
 * @param image_width
 * @param image_height
 * @return
 * @hide
 */
@SuppressWarnings("unused")
private static Bitmap decodeFile(File file, int image_width, int image_height) {
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, o);

        int REQUIRED_HEIGHT = image_height;
        int REQUIRED_WIDTH = image_width;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;

        //         Log.w("===", (width_tmp + "  " + height_tmp));

        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_WIDTH && height_tmp / 2 < REQUIRED_HEIGHT)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale++;

            //            Log.w("===", scale + "''" + width_tmp + "  " + height_tmp);
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(file), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

From source file:Main.java

public static int getDesirableBitmapSampleSize(String imgPath, int desiredSize) {
    int scale = 1;
    try {//from w  w w.j av a 2 s.  c  o  m
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(imgPath), null, options);
        //Find the correct scale value. It should be the power of 2.
        int width_tmp = options.outWidth, height_tmp = options.outHeight;
        while (true) {
            if (width_tmp / 2 < desiredSize || height_tmp / 2 < desiredSize)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }
        return scale;

    } catch (Exception e) {
        return scale;
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromStream(Context context, Uri uri, int reqWidth, int reqHeight)
        throws IOException {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// w w w  .j  a v  a2s . co m
    InputStream stream = context.getContentResolver().openInputStream(uri);
    BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    stream = context.getContentResolver().openInputStream(uri);
    Bitmap bmp = BitmapFactory.decodeStream(stream, null, options);
    stream.close();
    return bmp;
}