服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java中图片文件的缩放


  需要对图片进行处理,使之能够适当的显示在画面中。找到一些资料后,处理也比较简单代码: /* * andychen(andy.chen.sh@gmail.com) * **/ public static icon getfixedboundicon(string filepath, int height, int width) throws exception{ double ratio=0.0; //缩放比例 file f = new file(filepath); if (!f.isfile()) throw new exception(f+" is not image file error in getfixedboundicon!"); icon ret = new imageicon(filepath); bufferedimage bi = imageio.read(f); if ((bi.getheight()>height) || (bi.getwidth()>width)){ if (bi.getheight()>bi.getwidth()){ ratio = (new integer(height)).doublevalue() /bi.getheight(); } else { ratio = (new integer(width)).doublevalue()/bi.getwidth(); } file thf = new file(filepath+"_"+height +"_"+width); image itemp = bi.getscaledinstance (width,height,bi.scale_smooth); affinetransformop op = new affinetransformop(affinetransform.getscaleinstance(ratio, ratio), null); itemp = op.filter(bi, null); try { imageio.write((bufferedimage)itemp, "jpg", thf); ret = new imageicon(thf.getpath()); }catch (exception ex) { } } return ret; }

扫描关注微信公众号