【问题标题】:How can I overlay images over one another in Java?如何在 Java 中将图像叠加在一起?
【发布时间】:2010-10-13 07:04:55
【问题描述】:

所以我一直在发帖,但还没有得到一个可靠的答案:

我创建了一个带有裁剪方法的图像大小调整类。种植效果很好。我遇到的问题是我在GraphicsdrawImage 函数中指定的背景颜色无法正常工作。无论我提供什么,它都默认为黑色作为背景(在本例中为 Color.WHITE)。

此外,覆盖图像或最顶部的图像(来自文件)正在反转(我认为是)或以其他方式变色。只是为了让您可以更好地概念化这一点,我正在拍摄 jpeg 并将其覆盖在新的 BufferedImage 之上,新的缓冲图像的背景没有被设置。下面是我正在使用的代码:

public void Crop(int Height, int Width, int SourceX, int SourceY) throws Exception {
    //output height and width
    int OutputWidth = this.OutputImage.getWidth();
    int OutputHeight = this.OutputImage.getHeight();

    //create output streams
    ByteArrayOutputStream MyByteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream MyMemoryCacheImageOutputStream = new MemoryCacheImageOutputStream(MyByteArrayOutputStream);

    //Create a new  BufferedImage
    BufferedImage NewImage = new BufferedImage(Width, Height, BufferedImage.TYPE_INT_RGB);
    Graphics MyGraphics = NewImage.createGraphics();

    MyGraphics.drawImage(this.OutputImage, -SourceX, -SourceY, OutputWidth, OutputHeight, Color.WHITE, null);

    // Get Writer and set compression
    Iterator MyIterator = ImageIO.getImageWritersByFormatName("png");
    if (MyIterator.hasNext()) {
        //get image writer
        ImageWriter MyImageWriter = (ImageWriter)MyIterator.next();

        //get params
        ImageWriteParam MyImageWriteParam = MyImageWriter.getDefaultWriteParam();

        //set outputstream
        MyImageWriter.setOutput(MyMemoryCacheImageOutputStream);

        //create new ioimage
        IIOImage MyIIOImage = new IIOImage(NewImage, null, null);

        //write new image
        MyImageWriter.write(null, MyIIOImage, MyImageWriteParam);
    }

    //convert output stream back to inputstream
    ByteArrayInputStream MyByteArrayInputStream = new ByteArrayInputStream(MyByteArrayOutputStream.toByteArray());
    MemoryCacheImageInputStream MyMemoryCacheImageInputStream = new MemoryCacheImageInputStream(MyByteArrayInputStream);

    //resassign as a buffered image
    this.OutputImage = ImageIO.read(MyMemoryCacheImageInputStream);
}

【问题讨论】:

    标签: java awt overlay drawimage


    【解决方案1】:

    您能否区分是 Graphics 方法还是 ImageIO 方法破坏了您的图像?看起来您可以通过短路整个 ImageIO 进程并简单地分配来测试这一点

    this.OutputImage = NewImage;
    

    就此而言,我认为 ImageIO 操作会有所收获?在编写示例时,它似乎(理想情况下)是无操作的。

    此外,您不要在开始 ImageIO 进程之前处置您的 Graphics2D。它通常没有什么区别,但你不想假设。

    【讨论】:

    • 感谢您的回复,非常感谢就隔离而言,它很奇怪,我删除了所有覆盖的东西,只是创建了一个新的缓冲图像和 setBackgroundColor 并分配给 this.OutputImage 我仍然得到一个黑色背景。另外,为了清楚起见,我使用的是 Graphics - 而不是 Graphics2D。
    • 新代码是什么样的?单独设置背景颜色不会改变图像——它只用于清除操作(clearRect,可能是 Graphics2D 合成?)。
    【解决方案2】:

    关于叠加颜色失真问题,请确保您的图形上下文处于绘制模式而不是异或模式。 (Graphics.setPaintMode())。否则颜色位会被异或在一起。

    【讨论】:

    • 解决方法如下: //设置背景 MyGraphics.setBackground(Color.decode(this.BackgroundColor)); //清除矩形 MyGraphics.clearRect(0, 0, Width, Height);感谢您的所有帮助!
    猜你喜欢
    • 2021-12-13
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2023-01-17
    • 2018-11-08
    • 1970-01-01
    相关资源
    最近更新 更多