Example usage for android.graphics BitmapFactory.Options BitmapFactory.Options

List of usage examples for android.graphics BitmapFactory.Options BitmapFactory.Options

Introduction

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

Prototype

BitmapFactory.Options

Source Link

Usage

From source file:Main.java

/**
 * Decodes <tt>Bitmap</tt> identified by given <tt>resId</tt> scaled to
 * requested width and height.//from  w  ww.  j av a  2s .c om
 * @param res the <tt>Resources</tt> object.
 * @param resId bitmap resource id.
 * @param reqWidth requested width.
 * @param reqHeight requested height.
 * @return  <tt>Bitmap</tt> identified by given <tt>resId</tt> scaled to
 *          requested width and height.
 */
public static Bitmap scaledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

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  ww . ja v  a  2 s.  co  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.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded avatar./*from ww w .  j a v  a 2 s.c o  m*/
 *
 * @param filePath file path to avatar image
 */
public static Bitmap publishAvatar(String filePath) {
    Bitmap bitmap = null;

    if (!TextUtils.isEmpty(filePath)) {
        try {
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inSampleSize = 1;
            bitmap = BitmapFactory.decodeFile(filePath, opts);
            int size = 0;
            if (bitmap.getHeight() > bitmap.getWidth()) {
                size = bitmap.getWidth();
            } else {
                size = bitmap.getHeight();
            }
            Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            final int color = 0xff424242;
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, size, size);
            final RectF rectF = new RectF(rect);
            final float roundPx = 12;

            paint.setAntiAlias(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(color);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, rect, rect, paint);

            bitmap.recycle();

            return output;
        } catch (Exception e) {

        }
    }
    return null;
}

From source file:com.gmail.altakey.lucene.AsyncImageLoader.java

protected BitmapDrawable doInBackground(Void... args) {
    final Context context = this.view.getContext();
    final Resources res = context.getResources();

    try {//from  www .  ja v  a2s .  c o m
        InputStream in = this.read(new ProgressReportingInputStream.ProgressListener() {
            public void onAdvance(long at, long size) {
                publishProgress(at, size);
            }
        });
        BitmapFactory.Options bfo = new BitmapFactory.Options();
        bfo.inDither = true;
        bfo.inPreferredConfig = Bitmap.Config.RGB_565;
        try {
            bfo.inPreferQualityOverSpeed = true;
        } catch (NoSuchFieldError e) {
        }
        Bitmap bitmap = BitmapFactory.decodeStream(in, new Rect(-1, -1, -1, -1), bfo);
        return new BitmapDrawable(res, this.scale(bitmap));
    } catch (FileNotFoundException e) {
        return null;
    } catch (OutOfMemoryError e) {
        this.oomMessage.show();
        return null;
    }
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Sets the downloaded attached image./*from w w  w.  j a v a  2  s . c o  m*/
 *
 * @param fileName picture file path
 */
public static Bitmap publishPicture(String fileName) {
    Bitmap bitmap = null;
    try {

        if (!TextUtils.isEmpty(fileName)) {
            try {
                BitmapFactory.Options opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(fileName, opts);

                //Find the correct scale value. It should be the power of 2.
                int width = opts.outWidth, height = opts.outHeight;
                int scale = 1;
                while (true) {
                    if (width / 2 <= 150 || height / 2 <= 150) {
                        break;
                    }
                    width /= 2;
                    height /= 2;
                    scale *= 2;
                }
                BitmapFactory.Options opt = new BitmapFactory.Options();
                opt.inSampleSize = scale;

                bitmap = BitmapFactory.decodeFile(fileName, opt);
                int size = 0;
                if (bitmap.getHeight() > bitmap.getWidth()) {
                    size = bitmap.getWidth();
                } else {
                    size = bitmap.getHeight();
                }
                Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.RGB_565);
                Canvas canvas = new Canvas(output);

                final int color = 0xff424242;
                final Paint paint = new Paint();
                final Rect rect = new Rect(0, 0, size, size);
                final RectF rectF = new RectF(rect);
                final float roundPx = 0;

                paint.setAntiAlias(true);
                canvas.drawARGB(0, 0, 0, 0);
                paint.setColor(color);
                canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
                canvas.drawBitmap(bitmap, rect, rect, paint);

                bitmap.recycle();

                return output;
            } catch (Exception e) {
                Log.w("", "");
            }
        }
    } catch (Exception ex) {

    }

    return null;
}

From source file:com.amaze.filemanager.ui.icons.IconHolder.java

public Bitmap loadImage(String path) throws OutOfMemoryError {
    Bitmap bitsat;/*from  ww  w  .j  a  v  a2  s.  co m*/

    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        options.inJustDecodeBounds = true;
        Bitmap b = BitmapFactory.decodeFile(path, options);

        options.inSampleSize = futils.calculateInSampleSize(options, px, px);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        Bitmap bit;
        if (path.startsWith("smb:/"))
            bit = BitmapFactory.decodeStream(new SmbFileInputStream(path));
        else
            bit = BitmapFactory.decodeFile(path, options);

        bitsat = bit;// decodeFile(path);//.createScaledBitmap(bits,imageViewReference.get().getHeight(),imageViewReference.get().getWidth(),true);
    } catch (Exception e) {
        Drawable img = ContextCompat.getDrawable(mContext, R.drawable.ic_doc_image);
        Bitmap img1 = ((BitmapDrawable) img).getBitmap();
        bitsat = img1;
    }
    return bitsat;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Opens Bitmap from file/*  www  .  ja v a2  s.c om*/
 *
 * @param fileName - file path
 * @return
 */
public static Bitmap proccessBitmap(String fileName, Bitmap.Config config, int widthLimit) {

    Bitmap bitmap = null;
    File tempFile = null;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    try {
        // decode image with appropriate options
        tempFile = new File(fileName);
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
    } catch (Exception e) {
    }

    //Find the correct scale value. It should be the power of 2.
    int width = opts.outWidth, height = opts.outHeight;
    ;
    int scale = 1;
    while (true) {
        if (width / 2 <= widthLimit || height / 2 <= widthLimit) {
            break;
        }
        width /= 2;
        height /= 2;
        scale *= 2;
    }

    opts = new BitmapFactory.Options();
    opts.inSampleSize = scale;
    opts.inPreferredConfig = config;

    try {
        System.gc();
        bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
        if (bitmap != null) {
            return bitmap;
        }
    } catch (Exception ex) {
    } catch (OutOfMemoryError e) {
    }

    try {
        Thread.sleep(300);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    try {
        System.gc();
        bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
        if (bitmap != null) {
            return bitmap;
        }
    } catch (Exception ex) {
    } catch (OutOfMemoryError ex) {
    }

    try {
        Thread.sleep(300);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    try {
        System.gc();
        bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
    } catch (Exception ex) {
    } catch (OutOfMemoryError ex) {
    }

    return bitmap;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * Opens Bitmap from stream/* www  .j  av  a 2s  .  com*/
 *
 * @param stream - input stream
 * @return
 */
public static Bitmap proccessBitmap(InputStream stream, Bitmap.Config config) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inPreferredConfig = config;
    Bitmap bitmap = null;
    try {
        // decode image with appropriate options
        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(stream, null, opts);
        } catch (Exception ex) {
            Log.d("", "");
        } catch (OutOfMemoryError e) {
            Log.d("", "");
            System.gc();
            try {
                bitmap = BitmapFactory.decodeStream(stream, null, opts);
            } catch (Exception ex) {
                Log.d("", "");
            } catch (OutOfMemoryError ex) {
                Log.e("decodeImageFile", "OutOfMemoryError");
            }
        }
    } catch (Exception e) {
        Log.d("", "");
        return null;
    }

    return bitmap;
}

From source file:com.ibuildapp.romanblack.FanWallPlugin.data.Statics.java

/**
 * resize image with proportions/*ww w . j  a va  2  s.c  o m*/
 *
 * @param fileName   - file path
 * @param widthLimit - max width limit
 */
public static Bitmap resizeBitmapToMaxSize(String fileName, int widthLimit) {
    try {
        File tempFile = new File(fileName);
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);

        //Find the correct scale value. It should be the power of 2.
        int width = opts.outWidth;
        int scale = 1;
        while (true) {
            if (width / 2 <= widthLimit) {
                break;
            }
            width /= 2;
            scale *= 2;
        }

        opts = new BitmapFactory.Options();
        opts.inSampleSize = scale;

        // decode image with appropriate options
        Bitmap bitmap = null;
        try {
            System.gc();
            bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
        } catch (Exception ex) {
            Log.d("", "");
        } catch (OutOfMemoryError e) {
            Log.d("", "");
            System.gc();
            try {
                bitmap = BitmapFactory.decodeStream(new FileInputStream(tempFile), null, opts);
            } catch (Exception ex) {
                Log.d("", "");
            } catch (OutOfMemoryError ex) {
                Log.e("decodeImageFile", "OutOfMemoryError");
            }
        }
        return bitmap;

    } catch (FileNotFoundException e) {
        Log.e("", "");
        return null;
    }
}