如题,就是2个图片按位置叠加。

解决方案 »

  1.   

    同意楼上,drawImage()方法绘制图像和设置绘制位置。
    如果还需要透明的话,可以用Graphics2D这个类,有设置透明度大小
      

  2.   

    直接在photo图片上盖logo图片并保存为一个新图片import java.io.*;
    import java.awt.*;
    import java.awt.image.*;
    import javax.swing.*;public class ImagePrint extends JComponent
    {
        public ImagePrint()
        {
            init();
        }    public void init()
        {
            try
            {
                ImageIcon imageIconlogo = new javax.swing.ImageIcon(Toolkit.
                        getDefaultToolkit().createImage(ImagePrint.
                        class.getResource("/images/printpic.png")));
                BufferedImage blogo = new java.awt.image.BufferedImage(
                        imageIconlogo.getIconWidth(), imageIconlogo.getIconHeight(),
                        java.awt.image.BufferedImage.TYPE_INT_ARGB);
                Graphics g1 = blogo.getGraphics();
                g1.drawImage(imageIconlogo.getImage(), 0, 0, this);
                
                ImageIcon imageIconphoto = new javax.swing.ImageIcon(Toolkit.
                        getDefaultToolkit().createImage(ImagePrint.
                        class.getResource("/images/bgpic1.jpg")));
                BufferedImage bphoto = new java.awt.image.BufferedImage(
                        imageIconphoto.getIconWidth(),
                        imageIconphoto.getIconHeight(),
                        java.awt.image.BufferedImage.TYPE_3BYTE_BGR);
                Graphics g2 = bphoto.getGraphics();
                g2.drawImage(imageIconphoto.getImage(), 0, 0, this);
                
                //重合并保存图片 logo右下角和photo右下角的距离2,2
                printAndAave(blogo, bphoto, 2, 2);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }    /**
         * 进行图片透明印章处理并保存
         * @param bprint BufferedImage logo图
         * @param bibackground BufferedImage photo图
         * @param x int logo右下角和photo右下角x轴方向距离
         * @param y int logo右下角和photo右下角y轴方向距离
         */
        public void printAndAave(BufferedImage blogo, BufferedImage bphoto,
                                 int x, int y)
        {
            float alpha = 0.5f; // 透明度
            Graphics2D g2d = bphoto.createGraphics(); // 设置透明度
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha)); // 开始 //
            g2d.drawImage(blogo, bphoto.getWidth() - blogo.getWidth() - x,
                          bphoto.getHeight() - blogo.getHeight() - y, null);
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 结束
            FileOutputStream out = null;
            try
            {
                out = new FileOutputStream("image//logonphoto.jpg");
                com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.
                        codec.jpeg.JPEGCodec.createJPEGEncoder(out);
                encoder.encode(bphoto);
                out.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }    public static void main(String args[])
        {
            ImagePrint a = new ImagePrint();
        }
    }
      

  3.   

    读取图片使用ImageIO类,public static BufferedImage ImageIO.read(File input) ;
    它返回BufferedImage对象,正符合我们的需求。
    获取大图片的Graphics对象,如果有需要可以获取Graphics2D对象
    然后使用Graphics对象的drawImage(Image img, int x, int y, ImageObserver observer) 
    方法将小图片画到大图片的指定位置。其最后一个参数可以设置为null.
    然后再使用ImageIO类将图片保存到文件。
    public static boolean  ImageIO.write(RenderedImage im, String formatName, File output) 
    formatName参数可以使用常见的几种图片格式,如JPG,PNG,GIF等。
    可以使用static String[] ImageIO.getWriterFormatNames() 来获取支持的图片输出格式。
    同样可以使用static String[] ImageIO.getReaderFormatNames() 来获取支持的图片读取格式。