Example usage for android.graphics BitmapFactory decodeFile

List of usage examples for android.graphics BitmapFactory decodeFile

Introduction

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

Prototype

public static Bitmap decodeFile(String pathName, Options opts) 

Source Link

Document

Decode a file path into a bitmap.

Usage

From source file:Main.java

public static Bitmap decodeFile(String path, float width, float height) {
    int inSampleSize = getInSampleSize(path, width, height);
    BitmapFactory.Options newOpts = new BitmapFactory.Options();
    newOpts.inSampleSize = inSampleSize;
    newOpts.inJustDecodeBounds = false;/*from  www  .  java 2 s. c  o m*/
    newOpts.inPreferredConfig = Bitmap.Config.RGB_565;
    return BitmapFactory.decodeFile(path, newOpts);
}

From source file:Main.java

public static BitmapFactory.Options getBitmapSize(String strImagePath, boolean checkOrientation) {
    //==========================================
    // Loaded the temporary bitmap for getting size.
    //==========================================
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*w  w  w  . j  a  v  a 2  s. co m*/
    //Bitmap photo = BitmapFactory.decodeFile(strPath, options);
    BitmapFactory.decodeFile(strImagePath, options);

    if (checkOrientation) {
        int degree = getExifDegree(strImagePath);
        if (degree == 90 || degree == 270) {
            int temp = options.outWidth;
            options.outWidth = options.outHeight;
            options.outHeight = temp;
        }
    }

    return options;
}

From source file:Main.java

/**
 * @param imgName/*from   w  w w  . j av  a  2  s  .c  o  m*/
 * @return
 */
public static Bitmap getBitmapFromPath(String imgName) {
    String sExtendSdcardDir = "";
    String sAppRoot = "";

    String sdcardRoot = Environment.getExternalStorageDirectory() == null ? ""
            : Environment.getExternalStorageDirectory().getAbsolutePath();
    Log.d("Tiny", "sdcardRoot --" + sdcardRoot);

    if (sdcardRoot != null) {
        sExtendSdcardDir = sdcardRoot;
        sAppRoot = sExtendSdcardDir.concat("/TINY");
    }

    String sCache = sAppRoot.concat("/cache");

    File file = new File(sCache);
    if (!file.exists()) {
        file.mkdirs();
    }
    Log.d("Tiny", "file path --" + file.getAbsolutePath());

    File image = new File(file.getAbsolutePath(), imgName);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(), bmOptions);
    return bitmap;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*  w  w  w .  java  2 s.  c  o  m*/
    BitmapFactory.decodeFile(path, options);

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

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

/**
 * cut a circle from a bitmap/*  w w  w.  j a va  2  s.  co  m*/
 *
 * @param picturePath complete path name for the file.
 * @param destCube the cube dimension of dest bitmap
 * @return the bitmap
 */
public static Bitmap cutCircleFromBitmap(String picturePath, int destCube) {
    BitmapFactory.Options opts = new BitmapFactory.Options();

    opts.inDither = false; //Disable Dithering mode
    opts.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
    opts.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
    opts.inTempStorage = new byte[32 * 1024];

    Bitmap bitmapImg = BitmapFactory.decodeFile(picturePath, opts);

    int cube = destCube;

    if (bitmapImg == null)
        return null;

    int smallest = Math.min(bitmapImg.getWidth(), bitmapImg.getHeight());

    Bitmap output = Bitmap.createBitmap(cube, cube, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();

    int left = (int) ((bitmapImg.getWidth() - smallest) * 0.5);
    int top = (int) ((bitmapImg.getHeight() - smallest) * 0.5);

    final Rect rectSrc = new Rect(left, top, left + smallest, top + smallest);
    final Rect rectDest = new Rect(0, 0, cube, cube);

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);

    canvas.drawCircle(cube / 2, cube / 2, cube / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

    canvas.drawBitmap(bitmapImg, rectSrc, rectDest, paint);

    bitmapImg.recycle();

    return output;
}

From source file:Main.java

/**
 * scale image to fixed height and weight
 *
 * @param imagePath/*from  w  w  w  .  ja  va  2  s  .c  o  m*/
 * @return
 */
private static int getImageScale(String imagePath) {
    BitmapFactory.Options option = new BitmapFactory.Options();
    // set inJustDecodeBounds to true, allowing the caller to query the bitmap info without having to allocate the
    // memory for its pixels.
    option.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, option);

    int scale = 1;
    while (option.outWidth / scale >= IMAGE_MAX_WIDTH || option.outHeight / scale >= IMAGE_MAX_HEIGHT) {
        scale *= 2;
    }
    return scale;
}

From source file:Main.java

/** load the Bitmap from assets or sdcard_dir
 * @param context//from w w w.  j  a  v a2 s.  c  o m
 * @param outsideFileName
 * @param op
 * @param useSdcard
 * @param sdcard_assetdir_path
 * @return
 */
public static Bitmap loadBitmap(Context context, String outsideFileName, BitmapFactory.Options op,
        boolean useSdcard, String sdcard_assetdir_path) {
    AssetManager am = context.getAssets();

    Bitmap retval = null;
    synchronized (sCacheMap) {
        if (sCacheMap.get(outsideFileName) != null) {
            retval = sCacheMap.get(outsideFileName);
            return retval;
        }
    }

    try {
        retval = BitmapFactory.decodeStream(am.open(outsideFileName), null, op);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (useSdcard) {
        Bitmap _tmp = BitmapFactory.decodeFile(sdcard_assetdir_path + outsideFileName, op);
        if (_tmp != null) {
            retval = _tmp;
        }
    }

    if (retval != null) {
        synchronized (sCacheMap) {
            sCacheMap.put(outsideFileName, retval);
        }

    }

    return retval;
}

From source file:Main.java

public static Bitmap getResizedBitmap(File file, int targetWidth, int targetHeight, float degrees) {
    Bitmap ret = null;//from w  w w . ja v a 2s .  c  om

    if (file == null) {
    } else {
        BitmapFactory.Options option = new BitmapFactory.Options();
        Bitmap src = null;
        int sampleSize = 0;

        option.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), option);
        // more than 1MP
        if ((option.outWidth * option.outHeight) > 1048576) {
            double out_area = (double) (option.outWidth * option.outHeight) / 1048576.0;
            sampleSize = (int) (Math.sqrt(out_area) + 1);
        } else {
            sampleSize = 1;
        }

        option.inJustDecodeBounds = false;
        option.inSampleSize = sampleSize;
        src = BitmapFactory.decodeFile(file.getAbsolutePath(), option);
        if (src == null) {
        } else {
            ret = getResizedBitmap(src, targetWidth, targetHeight, degrees);
        }
    }

    return ret;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFd(String filePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/* ww w . j  ava 2 s . c o m*/
    BitmapFactory.decodeFile(filePath, options);

    // Calculate inSampleSize
    //        if(Build.VERSION.SDK_INT < 19)
    //        {
    //            options.inSampleSize = calculateInSampleSize(options, 300, 300);
    //        }
    //        else
    //        {
    options.inSampleSize = calculateInSampleSize(options, 480, 480);

    //        }

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

    Bitmap bm = BitmapFactory.decodeFile(filePath, options);
    if (bm == null) {
        return null;
    }
    int degree = readPictureDegree(filePath);
    bm = rotateBitmap(bm, degree);
    ByteArrayOutputStream baos = null;
    try {
        baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 30, baos);

    } finally {
        try {
            if (baos != null)
                baos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return bm;

}

From source file:Main.java

public static Bitmap rotateBitmap(String path, int orientation, int screenWidth, int screenHeight) {
    Bitmap bitmap = null;/*from w  w  w.ja v  a 2s . c o m*/
    final int maxWidth = screenWidth / 2;
    final int maxHeight = screenHeight / 2;
    try {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int sourceWidth, sourceHeight;
        if (orientation == 90 || orientation == 270) {
            sourceWidth = options.outHeight;
            sourceHeight = options.outWidth;
        } else {
            sourceWidth = options.outWidth;
            sourceHeight = options.outHeight;
        }
        boolean compress = false;
        if (sourceWidth > maxWidth || sourceHeight > maxHeight) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;

            options.inJustDecodeBounds = false;
            if (new File(path).length() > 512000) {
                float maxRatio = Math.max(widthRatio, heightRatio);
                options.inSampleSize = (int) maxRatio;
                compress = true;
            }
            bitmap = BitmapFactory.decodeFile(path, options);
        } else {
            bitmap = BitmapFactory.decodeFile(path);
        }
        if (orientation > 0) {
            Matrix matrix = new Matrix();
            //matrix.postScale(sourceWidth, sourceHeight);
            matrix.postRotate(orientation);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        }
        sourceWidth = bitmap.getWidth();
        sourceHeight = bitmap.getHeight();
        if ((sourceWidth > maxWidth || sourceHeight > maxHeight) && compress) {
            float widthRatio = (float) sourceWidth / (float) maxWidth;
            float heightRatio = (float) sourceHeight / (float) maxHeight;
            float maxRatio = Math.max(widthRatio, heightRatio);
            sourceWidth = (int) ((float) sourceWidth / maxRatio);
            sourceHeight = (int) ((float) sourceHeight / maxRatio);
            Bitmap bm = Bitmap.createScaledBitmap(bitmap, sourceWidth, sourceHeight, true);
            bitmap.recycle();
            return bm;
        }
    } catch (Exception e) {
    }
    return bitmap;
}