Java将跨度添加到图像背景颜色

时间:2018-09-20 19:30:08

标签: java image-processing graphics

我想在图像中添加额外的空间,我们称其为“ span”。

我的代码是:

        BufferedImage newImage = new BufferedImage(image2.getWidth(), image2.getHeight()+200, image2.getType());

        Graphics g = newImage.getGraphics();

        g.setColor(Color.white);
        g.fillRect(0,0,image.getWidth(),image.getHeight()+100);
        g.drawImage(image, 0, 100, null);
        g.setColor(Color.white);
        g.dispose();
        RenderedImage rendImage = newImage;

        String newUrl8006002 = splitUrl[0]+"-800x6002.jpg";
        File file = new File(newUrl8006002);
        ImageIO.write(rendImage, "jpg", file);

问题是,底部的图像有黑色背景,我希望是白色(顶部是白色)。

您知道要在整个图像中添加白色背景时要进行哪些更改吗?

1 个答案:

答案 0 :(得分:0)

如何?

static BufferedImage growY(BufferedImage im, int span, Color color)
{
  BufferedImage newImage = new BufferedImage(im.getWidth(), im.getHeight()+span, im.getType());
  int topSpan = span/2;
  int botSpan = span - topSpan;
  Graphics g = newImage.getGraphics();
  g.setColor(color);
  g.fillRect(0, 0, newImage.getWidth(), topSpan);
  g.fillRect(0, topSpan+im.getHeight(), newImage.getWidth(), botSpan);
  g.drawImage(im, 0, topSpan, null);
  g.dispose();    
  return newImage;
}