0

我一直在使用 Java 中的一些成像功能,试图将一张图像叠加在另一张图像上。像这样:

BufferedImage background = javax.imageio.ImageIO.read(
    new ByteArrayInputStream(getDataFromUrl(
        "https://www.google.com/intl/en_ALL/images/logo.gif"
    ))
);
BufferedImage foreground = javax.imageio.ImageIO.read(
    new ByteArrayInputStream(getDataFromUrl(
        "https://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif"
    ))
);

WritableRaster backgroundRaster = background.getRaster();
Raster foregroundRaster = foreground.getRaster();

backgroundRaster.setRect(foregroundRaster);

基本上,我试图在此叠加: https
花
://upload.wikimedia.org/wikipedia/commons/e/e2/Sunflower_as_gif_small.gif:https : //www.google.com/intl/en_ALL/images/logo .gif
替代文字

产品显示为:http: //imgur.com/xnpfp.png
蹩脚的形象

从我看到的例子来看,这似乎是合适的方法。我错过了一步吗?有没有更好的方法来处理这个?谢谢你的回复。

4

1 回答 1

1

似乎我以所有错误的方式来解决这个问题。Sun 论坛上概述的这个解决方案非常有效(复制到这里):

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.*;

class TwoBecomeOne {
    public static void main(String[] args) throws IOException {
        BufferedImage large = ImageIO.read(new File("images/tiger.jpg"));
        BufferedImage small = ImageIO.read(new File("images/bclynx.jpg"));
        int w = large.getWidth();
        int h = large.getHeight();
        int type = BufferedImage.TYPE_INT_RGB;
        BufferedImage image = new BufferedImage(w, h, type);
        Graphics2D g2 = image.createGraphics();
        g2.drawImage(large, 0, 0, null);
        g2.drawImage(small, 10, 10, null);
        g2.dispose();
        ImageIO.write(image, "jpg", new File("twoInOne.jpg"));
        JOptionPane.showMessageDialog(null, new ImageIcon(image), "",
                                      JOptionPane.PLAIN_MESSAGE);
    }
}
于 2009-04-28T02:30:43.427 回答