覆盖2张图片无法正常工作 [英] Overlaying of 2 images doesnt work properly

查看:137
本文介绍了覆盖2张图片无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将两个图像合并为JPEG图像上的PNG / TIFF文件。我使用以下代码

I am trying to merge two images a PNG/TIFF file over a JPEG image. I am using the following code

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();
                    }

但是,正在形成的最终图像不符合我的要求。为清晰起见,请参阅附图。

But, then final image that is forming is not according to what i want. Please refer to the attached images for clarity.

推荐答案

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);
    }
}

这篇关于覆盖2张图片无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆