最近看到别人用java写图片感觉到很神奇,就在网上找了些例子,这个例子是用来改变图片大小的:
-----------------------------------------------------------------------------------------------------------------
package pic;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 改变图片大小,改变成功
* @author Administrator
*
*/
public class ResizePic {
/**
* @param args
*/
public static void main(String[] args) {
File src = new File("E://图片//a.jpg") ;
try {
Image image = ImageIO.read(src) ;
int width = image.getWidth(null) ;
int height = image.getHeight(null) ;
BufferedImage bufferedImage = new BufferedImage(width/2,height/2,BufferedImage.TYPE_INT_RGB) ;
bufferedImage.getGraphics().drawImage(image, 0, 0, width/2, height/2,null) ;
FileOutputStream fos = new FileOutputStream("E://图片//change5.jpg") ;
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fos) ;
encoder.encode(bufferedImage) ;
System.out.println("转换成功...");
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}