gpt4 book ai didi

java - 如何在java中精确复制图像?

转载 作者:行者123 更新时间:2023-12-02 05:37:54 24 4
gpt4 key购买 nike

加载图像后,我想创建图像的精确副本,从而使质量和比例保持不变。使用我当前的代码,质量降低了。

public class Image {
private static final String path = "C:/Users.../src/7horses.jpg";
private static final File file = new File(path);

static BufferedImage deepCopy(BufferedImage bi) throws IOException {
String saveAs = "copy.jpg";
ColorModel cm = bi.getColorModel();
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
WritableRaster raster = bi.copyData(null);
BufferedImage cImg = new BufferedImage(cm, raster, isAlphaPremultiplied, null);
File saveImage = new File("C:/Users.../src", saveAs);
ImageIO.write(cImg, "jpg", saveImage);
return cImg;
}

public static void main(String[] args) throws IOException {
BufferedImage cp, img;
img = ImageIO.read(file);
cp = deepCopy(img);
}
}

最佳答案

尝试复制您的图像文件,使用以下代码:

        InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(new File("path/to/img/src"));
os = new FileOutputStream(new File("path/to/img/dest"));
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}

如果您使用的是Java 8,那么您只需调用Files.copy方法即可,在 docs 中检查它

关于java - 如何在java中精确复制图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24807725/

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