goback add

图片等比例缩放

2893 点击·0 回帖
灯火互联
楼主

图片的等比例缩放,第一个参数是图片路径,第二个是最终所需要图片的(宽高里取值最大的)的最大值
[java] // 限制值MaxSize*(2/3)=实际使用值的比较值IMAGE_MAX_SIZE  
    // 例如:限制图片大小为400,则实际使用的比较值应为400*(2/3)  
    // 260*2/3=390  
    public static Bitmap decodeFile(String path, int MaxSize) {

        File f = new File(path);
        int IMAGE_MAX_SIZE = MaxSize * 2 / 3;
        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();  

            double scale = 1;

            if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                scale = Math.pow(2, (int) Math.round(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 = (int) scale;
            FileInputStream fis = new FileInputStream(f);

            b = BitmapFactory.decodeStream(fis, null, o2);

            fis.close();
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
            // TODO Auto-generated catch block  
            e.printStackTrace();
        }
        return b;
    }



喜欢0 评分0