Java 两张图像的叠加无法正常工作

Java 两张图像的叠加无法正常工作,java,image,png,jpeg,Java,Image,Png,Jpeg,我正在尝试将两个图像合并为一个PNG/TIFF文件,而不是一个JPEG图像。我正在使用以下代码 try { image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\a.jpg")); } catch (IOException e) { // TODO Auto-generated catch

我正在尝试将两个图像合并为一个PNG/TIFF文件,而不是一个JPEG图像。我正在使用以下代码

try {
                        image = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\a.jpg"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                     BufferedImage overlay = null;
                    try {
                        overlay = ImageIO.read(new File("C:\\Users\\user\\Desktop\\test\\b.png"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                     // create the new image, canvas size is the max. of both image sizes
                     int w = Math.max(image.getWidth(), overlay.getWidth());
                     int h = Math.max(image.getHeight(), overlay.getHeight());
                     BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

                     // paint both images, preserving the alpha channels
                     Graphics2D g = combined.createGraphics();
                     /**Set Antialias Rendering**/
                     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                             RenderingHints.VALUE_ANTIALIAS_ON);
                     /**
                      * Draw background image at location (0,0)
                      * You can change the (x,y) value as required
                      */
                     g.drawImage(image, 0, 0, null);

                     /**
                      * Draw foreground image at location (0,0)
                      * Change (x,y) value as required.
                      */
                     g.drawImage(overlay, 0, 0, null);

                     g.dispose();

                     // Save as new image
                     try {
                        ImageIO.write(combined, "JPG", new File("C:\\Users\\user\\Desktop\\test\\c.jpg"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
但是,最终形成的图像并不是我想要的。为了清晰起见,请参考所附图片

import java.awt.*;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

class CombineImages {

    public static void main(String[] args) throws Exception {
        URL urlImage1 = new URL("http://i.stack.imgur.com/T5uTa.png");
        // Load the FG image (must have transparent parts)
        final Image fgImage = ImageIO.read(urlImage1);
        int w = fgImage.getWidth(null);
        int h = fgImage.getHeight(null);
        // Create a non-trasparent BG image
        final BufferedImage bgImage =
                new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        // Create the final image
        final BufferedImage finalImage =
                new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        Graphics2D g = finalImage.createGraphics();
        g.drawImage(bgImage, 0, 0, null);
        g.drawImage(fgImage, 0, 0, null);
        g.dispose();

        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new GridLayout(1,0,5,5));

                gui.add(new JLabel(new ImageIcon(bgImage)));
                gui.add(new JLabel(new ImageIcon(fgImage)));
                gui.add(new JLabel(new ImageIcon(finalImage)));

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}


PNG图像应具有透明背景,以便您可以看到其下方的图像。是吗?1)为了更快地获得更好的帮助,请发布一条消息。2) 请使用较小的图像进行测试。3) 第三幅图像是前两幅图像组合的结果吗?1)这是我的代码2)这段代码适用于两个PNG文件,但不适用于JPEG和PNG/TIFF 3)是的..是..实际上我使用的是透明背景的TIF图像,因为TIFF图像非常大,我不能发布它们。PNG图像应该有透明的背景,这样你可以看到它下面的图像。是吗?1)为了更快地获得更好的帮助,请发布一条消息。2) 请使用较小的图像进行测试。3) 第三幅图像是前两幅图像结合的结果吗?1)这是我的代码2)这段代码适用于两个PNG文件,但不适用于JPEG和PNG/TIFF 3)是的..是..事实上我使用的是透明背景的TIF图像,由于TIFF图像非常大,我无法发布它们。在本例中,它的工作非常完美,但是,当我用JPG文件替换BG图像时,它似乎不起作用。。请参阅以获取后续信息。在本例中,它工作得非常好,但当我用JPG文件替换BG图像时,dosnt似乎工作得很好。。有关后续行动,请参阅。