2016年1月6日 星期三

Android-讀寫圖片檔案( Read / Write image in the file )

讀取圖片檔案
private Bitmap decodeFile(File f){
    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;
        // IMAGE_MAX_SIZE
        if (o.outHeight > IMAGE_MAX_SIZE  || o.outWidth > IMAGE_MAX_SIZE) {
            scale = (int)Math.pow(2, (int) Math.ceil(Math.log(IMAGE_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;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
        Log.i("EnsoulCamera", "decodeFile : " + e.toString());
    }
    return b;
}



沒有留言:

張貼留言