【问题标题】:Image quality reduces (turns reddish) as JPEG with BufferedImage使用 BufferedImage 的 JPEG 图像质量会降低(变红)
【发布时间】:2017-09-16 20:18:26
【问题描述】:

我正在制作一个程序,从图像中提取像素数组,取出 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 图像却偏红。

这是原图 这是输出的 jpg 图像

知道是什么原因造成的吗?任何朝着正确方向的推动将不胜感激。

【问题讨论】:

  • 您尝试的图像处理应该达到什么程度?
  • 嗯,我正在努力使用 LSB 替换方法在图像中嵌入纯文本消息。但在此之前,我只是尝试简单地在新图像中写入像素以使其不碍事。
  • 为什么不用图形?
  • 嗯。我想,我不知道那堂课。它是否有适当的方法来获取像素数组,反之亦然?你能指出我在某个地方的一个工作示例吗?那很好啊。或者我会仔细研究文档 xD

标签: java image image-processing jpeg


【解决方案1】:

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

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.

【讨论】:

  • 让我试一试然后回来。
  • 啊.. 不幸的是,同样的结果。 jpg 的色调偏红。
  • @Ramandeep :请看一下 -my edit about how to insert text -try jpeg 可能而不是 jpg
  • 我想你误解了我一点。我不想在图像上绘制文本。我正在使用 LSB 替换方法实现隐写术,以隐藏 rgb 像素位中的文本。
  • @Ramandeep:你的意思是像水印这样的东西?或者你想隐藏一些数据?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-17
  • 2019-08-23
  • 2017-08-03
  • 1970-01-01
  • 2014-04-09
  • 1970-01-01
相关资源
最近更新 更多