灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2335回复:0

图片等比例缩放

楼主#
更多 发布于:2012-09-06 13:52

图片的等比例缩放,第一个参数是图片路径,第二个是最终所需要图片的(宽高里取值最大的)的最大值
[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
游客

返回顶部