Image theImg = ...;BufferedImage bufImg = new BufferedImage(theImg.getWidth(null), theImg.getHeight(null), BufferedImage.TYPE_INT_RGB);img.getGraphics().drawImage(theImg, 0, 0, null);FileOutputStream fos = new FileOutputStream("test1.jpg");
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufImg);
fos.close();
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(fos);
encoder.encode(bufImg);
fos.close();
解决方案 »
- 日志记录器 级别
- struts 问题Cannot find ActionMappings or ActionFormBeans collection
- 请教如何用ftpclient上传和下载文本文件?
- 学习的困惑!!!1
- 访问一些页面的时候,怎样做到一部分需要验证,而另一部分页面不需要验证~~~~
- 一道小程序有问题?请教!
- 请问,向另一个类的构造函数传参数的方法
- 谁有keytool的中文文档?
- 一个java初学者的问题:我是初学者
- 这种json怎么用Gson解析,
- java怎样实现文件的拷贝啊,只能用流一行一行读写吗?有没有其它更好的办法?
- 想问个javascript的问题,可是转来转去,不知该在哪个板块问比较好,还是放这里试试吧:在javascript中能调整窗口的大小吗?HOW?
img.getGraphics().drawImage(theImg, 0, 0, null);
应该是:
bufImg.getGraphics().drawImage(theImg, 0, 0, null);
cbhyk 兄厉害
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;
import javax.swing.*;class A{
public static void main(String[] args)
throws Exception{ Image img=new ImageIcon("nh.gif").getImage();
int width = img.getWidth(null);
int height = img.getHeight(null);
BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawImage(img,0,0,width,height,null);
g.dispose();
File f = new File("myimage.jpg");
ImageIO.write(bi, "jpg", f);
}
}