【问题标题】:Java BufferedImage saves with unwanted background colorJava BufferedImage 保存不需要的背景颜色
【发布时间】:2011-12-07 05:53:14
【问题描述】:

提前感谢您的帮助

说明: 该程序绘制、显示和保存图像。它的工作原理如下:对象本身扩展了 Frame。在构造函数中,对象创建一个 BufferedImage,并调用一个在该图像上绘制的方法。然后,它将图像显示到框架上。最后,它将图像保存到一个文件中(我不在乎它使用什么格式)。主程序创建对象,其余的由它完成。

问题: 保存的文件总是有彩色背景!这特别奇怪,因为显示的图像很好。如果我在 ImageIO.write() 中使用“jpg”格式,则背景为红色。如果我使用“png”格式,则背景为深灰色。

我在这上面花了一段时间,但我仍然不知道到底发生了什么!

    import java.awt.Frame ;
    import java.awt.image.BufferedImage ;
    import java.io.IOException ;
    import java.awt.event.WindowEvent ;
    import java.awt.event.WindowAdapter ;
    import java.awt.Toolkit ;
    import java.awt.Graphics2D ;
    import java.awt.Graphics ;
    import java.awt.Color ;
    import java.awt.Dimension ;
    import javax.imageio.ImageIO ;
    import java.io.File ;
    import java.awt.geom.Rectangle2D;

    public class HGrapher extends Frame{
       private BufferedImage img ;
       private float colors[][] ; //the colors for every rectangle
       private double availWidth ;
       private double availHeight ;


       public HGrapher(String saveFileName, int numRects) throws IOException {
          //*add window closer
          addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) {
                System.exit(0);            }
          });

          //*get colors to use
          setColors( numRects) ;

          //*figure out the size of the image and frame
          this.availHeight = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().height ;
          this.availWidth = (3.0/4) * Toolkit.getDefaultToolkit().getScreenSize().width ;

          //*create the image
          this.img = new BufferedImage( (int)availWidth, (int)availHeight, BufferedImage.TYPE_INT_ARGB);
          Graphics2D drawer = img.createGraphics() ;
          drawer.setBackground(Color.WHITE);

          this.makeImg( drawer) ;
          //*display the image
          this.setSize( new Dimension( (int)availWidth, (int)availHeight ) ) ;
          this.setVisible(true);

          //*save the image
          ImageIO.write(img, "jpg",new File( (saveFileName +".jpg") ) );
       }


       //*draws the image by filling rectangles whose color are specified by this.colors
       public void makeImg( Graphics2D drawer) {
          double rectWidth = this.availWidth / (double)colors.length ;
          for(int i = 0 ; i < colors.length ; i ++) {
             drawer.setColor( new Color( this.colors[i][0], this.colors[i][1], this.colors[i][2],
                                         this.colors[i][3] ) ) ;
             drawer.fill( new Rectangle2D.Double( rectWidth*i, 0, rectWidth, this.availHeight ) ) ;
          }
       }


       //*paint method
       public void paint(Graphics g) {
          Graphics2D drawer = (Graphics2D)g ;
          drawer.drawImage(img, 0, 0, null) ;
       }


       //*creates an array of the colors rectangles are filled with
       public void setColors( int numRects) {
          this.colors = new float[ numRects][4] ;
          //*make every 1st rect red
          for(int i = 0 ; i< colors.length ; i+= 3) {
             this.colors[i][0] = (float).8 ; this.colors[i][1] = (float).1 ; this.colors[i][2] = (float).1 ;
             this.colors[i][3] = (float).8 ;      }
          //*make every 2nd rect green
          for(int i = 1 ; i< colors.length ; i+= 3) {
             this.colors[i][0] = (float).1 ; this.colors[i][1] = (float).8 ; this.colors[i][2] = (float).1 ;
             this.colors[i][3] = (float).8 ;      }
          //*make every 3rd rect
          for(int i = 2 ; i< colors.length ; i+= 3) {
             this.colors[i][0] = (float).1 ; this.colors[i][1] = (float).1 ; this.colors[i][2] = (float).8 ;
             this.colors[i][3] = (float).8 ;      }
       }




       public static void main (String[]args) throws IOException {
          HGrapher hg = new HGrapher("saved", 14) ;
       }

    }

【问题讨论】:

    标签: java swing bufferedimage graphics2d javax.imageio


    【解决方案1】:

    setBackground()只设置了用于清除图像的颜色,并没有真正清除图像。在setBackground() 之后致电Graphics.clearRect(int,int,int,int)。像这样:

    //*create the image
    this.img = new BufferedImage( (int)availWidth, (int)availHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D drawer = img.createGraphics() ;
    drawer.setBackground(Color.WHITE);
    drawer.clearRect(0,0,(int)availWidth,(int)availHeight);
    

    【讨论】:

    • 非常感谢彼得!这种方法不适用于 .jpg 格式(产生纯黑色图像),但它确实适用于 .png 格式,这对我来说已经足够了!我部分倾向于问为什么它适用于一种格式而不是另一种格式......无论如何,谢谢彼得!
    • clearRect() 可能会留下 0 的 alpha 值。JPG 不支持透明度,如果某些东西的 alpha 为 0,则可能默认为黑色。您可以使用 setColor() 和 fillRect() 而不是 @ 987654329@ 和 clearRect() 使其适用于 JPG
    • @PeterT。我对 JPG 也有类似的问题,但我无法让它工作 - 我打开了一个新问题 stackoverflow.com/questions/50159394/… - 你介意看一下吗?
    猜你喜欢
    • 2010-10-21
    • 2022-12-12
    • 2018-09-08
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多