Java 代碼
1 import java.io.*;
2 import java.awt.*;
3 import java.awt.image.*;
4 import java.awt.Graphics;
5 import java.awt.color.ColorSpace;
6 import javax.imageio.ImageIO;
7
8 public class ChangeImageSize
9 {
10 /** *//**
11 * 縮放圖像
12 * @param srcImageFile 源圖像文件地址
13 * @param result 縮放后的圖像地址
14 * @param scale 縮放比例
15 * @param flag 縮放選擇:true 放大; false 縮小;
16 */
17 public static void scale(String srcImageFile, String result, int scale, boolean flag)
18 {
19 try
20 {
21 BufferedImage src = ImageIO.read(new File(srcImageFile)); // 讀入文件
22 int width = src.getWidth(); // 得到源圖寬
23 int height = src.getHeight(); // 得到源圖長
24 if (flag)
25 {
26 // 放大
27 width = width * scale;
28 height = height * scale;
29 }
30 else
31 {
32 // 縮小
33 width = width / scale;
34 height = height / scale;
35 }
36 Image image = src.getScaledInstance(width, height, Image.SCALE_DEFAULT);
37 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
38 Graphics g = tag.getGraphics();
39 g.drawImage(image, 0, 0, null); // 繪制縮小后的圖
40 g.dispose();
41 ImageIO.write(tag, "JPEG", new File(result));// 輸出到文件流
42 }
43 catch (IOException e)
44 {
45 e.printStackTrace();
46 }
47 }
48
49 /** *//**
50 * 圖像切割
51 * @param srcImageFile 源圖像地址
52 * @param descDir 切片目標文件夾
53 * @param destWidth 目標切片寬度
54 * @param destHeight 目標切片高度
55 */
56 public static void cut(String srcImageFile, String descDir, int destWidth, int destHeight)
57 {
58 try
59 {
60 Image img;
61 ImageFilter cropFilter;
62 // 讀取源圖像
63 BufferedImage bi = ImageIO.read(new File(srcImageFile));
64 int srcWidth = bi.getHeight(); // 源圖寬度
65 int srcHeight = bi.getWidth(); // 源圖高度
66 if (srcWidth > destWidth && srcHeight > destHeight)
67 {
68 Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);
69 destWidth = 200; // 切片寬度
70 destHeight = 150; // 切片高度
71 int cols = 0; // 切片橫向數量
72 int rows = 0; // 切片縱向數量
73 // 計算切片的橫向和縱向數量
74 if (srcWidth % destWidth == 0)
75 {
76 cols = srcWidth / destWidth;
77 }
78 else
79 {
80 cols = (int) Math.floor(srcWidth / destWidth) + 1;
81 }
82 if (srcHeight % destHeight == 0)
83 {
84 rows = srcHeight / destHeight;
85 }
86 else
87 {
88 rows = (int) Math.floor(srcHeight / destHeight) + 1;
89 }
90 // 循環建立切片
91 // 改進的想法:是否可用多線程加快切割速度
92 for (int i = 0; i < rows; i++)
93 {
94 for (int j = 0; j < cols; j++)
95 {
96 // 四個參數分別為圖像起點坐標和寬高
97 // 即: CropImageFilter(int x,int y,int width,int height)
98 cropFilter = new CropImageFilter(j * 200, i * 150, destWidth, destHeight);
99 img = Toolkit.getDefaultToolkit().createImage(
100 new FilteredImageSource(image.getSource(), cropFilter));
101 BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
102 Graphics g = tag.getGraphics();
103 g.drawImage(img, 0, 0, null); // 繪制縮小后的圖
104 g.dispose();
105 // 輸出為文件
106 ImageIO.write(tag, "JPEG", new File(descDir + "pre_map_" + i + "_" + j + ".jpg"));
107 }
108 }
109 }
110 }
111 catch (Exception e)
112 {
113 e.printStackTrace();
114 }
115 }
116
117 /** *//**
118 * 圖像類型轉換 GIF->JPG GIF->PNG PNG->JPG PNG->GIF(X)
119 */
120 public static void convert(String source, String result)
121 {
122 try
123 {
124 File f = new File(source);
125 f.canRead();
126 f.canWrite();
127 BufferedImage src = ImageIO.read(f);
128 ImageIO.write(src, "JPG", new File(result));
129 }
130 catch (Exception e)
131 {
132 // TODO Auto-generated catch block
133 e.printStackTrace();
134 }
135 }
136
137 /** *//**
138 * 彩色轉為黑白
139 * @param source
140 * @param result
141 */
142 public static void gray(String source, String result)
143 {
144 try
145 {
146 BufferedImage src = ImageIO.read(new File(source));
147 ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
148 ColorConvertOp op = new ColorConvertOp(cs, null);
149 src = op.filter(src, null);
150 ImageIO.write(src, "JPEG", new File(result));
151 }
152 catch (IOException e)
153 {
154 e.printStackTrace();
155 }
156 }
157
158 /** *//**
159 * @param args
160 */
161 public static void main(String[] args)
162 {
163 scale("c:\\test\\456.jpg","C:\\test\\image1.jpg",2,false);
164 cut("c:\\test\\456.jpg","C:\\test\\image2.jpg",64,64);
165 gray("c:\\test\\456.jpg","C:\\test\\image4.jpg");
166 }
167
168 }
169
posted on 2008-11-01 10:56
aisoft 閱讀(1693)
評論(0) 編輯 收藏 所屬分類:
J2EE開發技術