public static void main(String[] arg){
try{
File _file = new File("d:\\img\\mimi.jpg"); //读入文件
Image src = javax.imageio.ImageIO.read(_file); //构造Image对象
int wideth=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
System.out.println("长度为"+String.valueOf(wideth));
System.out.println("宽度为"+String.valueOf(height));
BufferedImage tag = new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,wideth,height,null); //绘制缩小后的图
// 输出到文件流
FileOutputStream out= new FileOutputStream("d:\\img\\mimi1.jpg");
// File file = new File("newFile.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //JPEG编码
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
读写图片
最新推荐文章于 2023-04-01 11:29:13 发布