`
xusaomaiss
  • 浏览: 608836 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

答复: 如何用java为文件生成缩略图

阅读更多
public static boolean scale(String imagepath,String newpath){
// 返回一个 BufferedImage,作为使用从当前已注册 ImageReader 中自动选择的 ImageReader 解码所提供 File 的结果

BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("读取图片文件出错!"+e.getMessage());
return false;
}

// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;

        if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
            if (image.getHeight() > image.getWidth())
                //图片要缩放的比例
            Ratio = 300.0 / image.getHeight();
            else
                Ratio = 300.0 / image.getWidth();
        }
// 根据仿射转换和插值类型构造一个 AffineTransformOp。
        AffineTransformOp op = new AffineTransformOp(AffineTransform
                .getScaleInstance(Ratio, Ratio), null);
       // 转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
        image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);


FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("写图片文件出错!!"+e.getMessage());
return false;
}
return true;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics