문제

나는 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/e2/sunflower_as_gif_small.gif
flower
이에: https://www.google.com/intl/en_all/images/logo.gif
alt text

제품은 다음과 같이 나타납니다. http://imgur.com/xnpfp.png
crappy image

내가 본 예에서 이것은 적절한 방법 인 것 같습니다. 단계가 없어? 이것을 처리하는 더 좋은 방법이 있습니까? 귀하의 답변에 감사드립니다.

도움이 되었습니까?

해결책

내가 모든 잘못된 방식으로 이것에 대해 가고있는 것 같습니다. 이 솔루션은 다음에 요약되어 있습니다 선 포럼 완벽하게 작동합니다 (여기에서 복사) :

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);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top