我在做图片切割的时候遇到一个问题:如何用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();
}
}帮我改改!
切割程序如下:
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();
}
}帮我改改!
我的博客里写的有一篇,java按比例压缩图片的
-----------------个性签名---------------------
500人的编程群:欢迎大家加入,交流探讨!
群名:程序人生大联盟主群
群号:49282233