gpt4 book ai didi

java - 带有 BufferedImage 的 JPEG 图像质量会降低(变红)

转载 作者:行者123 更新时间:2023-12-02 13:22:49 25 4
gpt4 key购买 nike

我正在制作一个程序,从图像中提取像素数组,取出 ARGB 值。然后再次将它们写回以制作另一个图像。

        BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();

pixels = new int[iw * ih];
PixelGrabber pg = new PixelGrabber(imagebuffer, 0, 0, iw, ih, pixels, 0, iw);
pg.grabPixels();

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
image.setRGB(0, 0, width, height, pixels, 0, width);


ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));

现在 png 和 gif 的输出图像看起来不错,但输出 jpg 图像变得相当偏红。

这是原图 enter image description here这是输出的 jpg 图像 enter image description here

知道是什么原因造成的吗?任何朝着正确方向努力的努力都将受到赞赏。

最佳答案

我不知道这是否适合你,但我总是逐像素地进行。所以:

BufferedImage imagebuffer = ImageIO.read(new File("C:\\Users\\Ramandeep\\Downloads\\w3.jpg"));
iw = imagebuffer.getWidth();
ih = imagebuffer.getHeight();
BufferedImage image = new BufferedImage(iw,ih,BufferedImage.TYPE_INT_ARGB);
for (int x=0; x < iw; x++) {
for (int y=0; y < ih; y++) {
image.setRGB(x,y,imagebuffer.getRGB(x,y));
}
}
ImageIO.write(image, "jpg", new File("C:\\Users\\Ramandeep\\Desktop\\out.jpg"));
ImageIO.write(image, "gif", new File("C:\\Users\\Ramandeep\\Desktop\\out.gif"));
ImageIO.write(image, "png", new File("C:\\Users\\Ramandeep\\Desktop\\out.png"));

这种方式的行数相似,所以我认为你可以尝试一下。

如果您想插入文本,请输入 id ìmport java.awt.*;,其中包括 Graphics、Graphics2D 和 Font,然后:

Font font=new Font("Sans,0,20); //Name, type(none, bold, italic), size
Graphics2D imagegraphics=imagebuffer.createGraphics();
imagegraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //enable antialiasing
imagegraphics.setFont(font);
imagegraphics.setColor(Color.BLACK);
String yourtext="Fighter";
int h=imagegraphics.getFontMetrics().getHeight();
int w=imagegraphics.getFontMetrics().stringWidth(yourtext);
imagegraphics.drawString(yourtext,5,h); //Draw text upper left corner, note that y-value is the bottom line of the string

编辑:

这是 Alpha 值。修正者:

BufferedImage image = new
BufferedImage(iw,ih,BufferedImage.TYPE_INT_RGB); //RGB, jpeg hasnt got alpha, ints have been converted as if they contain red first, but its alpha(the first bytes, these ints are interpreted bitwise i think) (argb), so it became more red.

关于java - 带有 BufferedImage 的 JPEG 图像质量会降低(变红),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43515431/

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