【文件属性】:
文件名称:用java如何把图片处理到指定大小
文件大小:3KB
文件格式:TXT
更新时间:2012-08-29 00:45:52
用java如何把图片处理到指定大小
用java如何把图片处理到指定大小
切割程序如下:
public void cut(String srcImageFile,FileOutputStream fileout, int w, int h, int x1,
int y1, int sw, int sh) {
// TODO Auto-generated method stub
try {
// http://localhost:8080/ImpCra/createServlet?p=Sunset.jpg&x=117&y=201&w=61&h=50&pw=300&ph=400
Image img;
ImageFilter cropFilter;
// 读取源图像
BufferedImage bi = ImageIO.read(new File(srcImageFile));
if (sw >= w && sh >= h) {
Image image = bi.getScaledInstance(sw, sh, Image.SCALE_DEFAULT);
// 剪切起始坐标点
int x = x1;
int y = y1;
int destWidth = w; // 切片宽度
int destHeight = h; // 切片高度
// 图片比例
double pw = sw;
double ph = sh;
double m = (double) sw / pw;
double n = (double) sh / ph;
System.out.println(m);
int wth = (int) (destWidth * m);
int hth = (int) (destHeight * n);
int xx = (int) (x * m);
int yy = (int) (y * n);
// 四个参数分别为图像起点坐标和宽高
// 即: CropImageFilter(int x,int y,int width,int height)
cropFilter = new CropImageFilter(xx, yy, wth, hth);
img = Toolkit.getDefaultToolkit().createImage(
new FilteredImageSource(image.getSource(), cropFilter));
BufferedImage tag = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制缩小后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", fileout);
}
} catch (Exception e) {
e.printStackTrace();
}
}
网友评论
- 好歹给个注释啊,没有注释就算了,好歹能用啊。