我是靠谱客的博主 自信豆芽,这篇文章主要介绍java 按比例缩小图片 按照宽度或者高度 缩小图片,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class PicUtils { private String minFile; private String maxFile; private int width; private int height; private BufferedImage img; private String ext; public static void main(String[] args) { File[] files = new File("D:\PitStop\sjpg\3JPG").listFiles(); for(int i=files.length-1;i>=0;i--){ System.out.println(files.length+"-"+i+","+files[i]); if(files[i].getName().indexOf("jpg")!=-1){ new PicUtils(files[i].toString(),"D:\images\max","D:\images\min",0.3f,180); } } } //缩放图片工具的构造函数 public PicUtils(String srcFile,String max,String min,double maxf,int minpt){ try { //得到最后一个.的位置 int index = srcFile.lastIndexOf("."); //获取被缩放的图片的格式 this.ext = srcFile.substring(index + 1); //获取目标路径(和原始图片路径相同,在文件名后添加了一个_s) String name = new File(srcFile).getName(); name = name.replaceAll("_1.jpg",".jpg"); this.maxFile = max+"/"+name; this.minFile = min+"/"+name; //读取图片,返回一个BufferedImage对象 this.img = ImageIO.read(new File(srcFile)); //获取图片的长和宽 this.width = img.getWidth(); this.height = img.getHeight(); zoomByScale(maxf); zoomBySize(minpt); }catch (Exception e){ e.printStackTrace(); } } /** * 按比例对图片进行缩放. * @param scale 缩放比例 * @throws IOException */ public void zoomByScale(double scale) throws IOException { if(!new File(maxFile).exists()){ //获取缩放后的长和宽 int _width = (int) (scale * width); int _height = (int) (scale * height); //获取缩放后的Image对象 Image _img = img.getScaledInstance(_width, _height, Image.SCALE_DEFAULT); //新建一个和Image对象相同大小的画布 BufferedImage image = new BufferedImage(_width, _height, BufferedImage.TYPE_INT_RGB); //获取画笔 Graphics2D graphics = image.createGraphics(); //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空 graphics.drawImage(_img, 0, 0, null); //释放资源 graphics.dispose(); //使用ImageIO的方法进行输出,记得关闭资源 OutputStream out = new FileOutputStream(new File(maxFile)); ImageIO.write(image, ext, out); out.close(); } } /** * 指定长和宽对图片进行缩放 * @throws IOException */ public void zoomBySize(int w) throws IOException { if(!new File(minFile).exists()) { int tempw = 0; int temph = 0; if(this.width>this.height){ tempw = w; temph =this.height* w/this.width; }else{ tempw = w*this.width/this.height; temph =w; } //与按比例缩放的不同只在于,不需要获取新的长和宽,其余相同. Image _img = img.getScaledInstance(tempw, temph, Image.SCALE_DEFAULT); BufferedImage image = new BufferedImage(tempw, temph, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = image.createGraphics(); graphics.drawImage(_img, 0, 0, null); graphics.dispose(); OutputStream out = new FileOutputStream(new File(minFile)); ImageIO.write(image, ext, out); out.close(); } } }

 

最后

以上就是自信豆芽最近收集整理的关于java 按比例缩小图片 按照宽度或者高度 缩小图片的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部