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 阅读(1689)
评论(0) 编辑 收藏 所属分类:
J2EE开发技术