gpt4 book ai didi

java - 保存调整大小的图像java

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:47:33 24 4
gpt4 key购买 nike

如何将调整大小的图像保存到特定文件夹?

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
ImgChooser ic = new ImgChooser();
ImageIcon icon = new ImageIcon(me,"id pic");
Image img1 = icon.getImage();
Image img2 = img1.getScaledInstance(105, 105, 0);
icon.setImage(img2);
jLabel1.setIcon(icon);
}

第一个代码是我获取图像并调整其大小的地方。然后我想将调整大小的图像保存在另一个文件夹中。提前致谢

最佳答案

使用ImageIO.write(...)正如其他人已经说过的(对他们+1),这里添加一个例子给你:

public static void main(String[] args) {

try {

BufferedImage originalImage = ImageIO.read(new File("c:\\test.jpg"));//change path to where file is located
int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

BufferedImage resizeImageJpg = resizeImage(originalImage, type, 100, 100);
ImageIO.write(resizeImageJpg, "jpg", new File("c:\\images\\testresized.jpg")); //change path where you want it saved

} catch (IOException e) {
System.out.println(e.getMessage());
}

}

private static BufferedImage resizeImage(BufferedImage originalImage, int type, int IMG_WIDTH, int IMG_HEIGHT) {
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();

return resizedImage;
}

引用:

关于java - 保存调整大小的图像java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12620158/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com