我想制作一个thunbnail图像(调整大小的图像)
这是我的代码
public static void createImage(String loadFile, String saveFile)throws IOException{
File load_image = new File(loadFile); //가져오는거
FileInputStream fis = new FileInputStream(load_image);
File save = new File(saveFile); // 썸네일
BufferedImage bi = ImageIO.read(fis);
int width = bi.getWidth();
int height = bi.getHeight();
int maxWidth=0;
int maxHeight=0;
if(width>height){
maxWidth = 1280;
maxHeight = 720;
}else{
maxWidth = 720;
maxHeight = 1280;
}
if(width > maxWidth){
float widthRatio = maxWidth/(float)width;
width = (int)(width*widthRatio);
height = (int)(height*widthRatio);
}
if(height > maxHeight){
float heightRatio = maxHeight/(float)height;
width = (int)(width*heightRatio);
height = (int)(height*heightRatio);
}
BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = thu.createGraphics();
g2.drawImage(bi, 0, 0, width, height, null);
ImageIO.write(thu, "jpg", save);
}
有时我的图像颜色会因意外的颜色而改变 这是图像示例
首先是原产地
第二个是缩略图
我不知道为什么...... 哪里我搞错了?
请帮助我......答案 0 :(得分:2)
您使用BufferedImage.TYPE_INT_RGB
编写图像。但如果这不是源图像的类型,则颜色会出错。
尝试替换此行
BufferedImage thu = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
由此:
BufferedImage thu = new BufferedImage(width, height, bi.getType());