Java 利用Graphics2D 合并图片(png格式可设置透明)

 利用Graphics2D 合并图片

 

效果如下: 图一图二合并成图三

Java 利用Graphics2D 合并图片(png格式可设置透明)_第1张图片

tip:  需要上图的效果 frontgroud需要为png格式

 附上代码:

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.text.AttributedString;


public class ImageMerge {
    

    public static String overlapImage(String backgroundPath, String frontgroudPath, String msg1, String msg2, String outPutPath) {
        try {

            Font pinfang_44 = new Font("苹方-简 中黑体", Font.BOLD, 44);
            Font pinfang_24 = new Font("苹方-简 常规体", Font.PLAIN, 24);

            //设置图片大小
            BufferedImage background = resizeImagePng(500, 400, ImageIO.read(new File(backgroundPath)));
            BufferedImage frontgroud = resizeImagePng(500, 400, ImageIO.read(new File(frontgroudPath)));

            //在背景图片中添加入需要写入的信息,
            Graphics2D g = background.createGraphics();

            //设置为透明覆盖
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            //在背景图片上相框
            g.drawImage(frontgroud, 0, 0, frontgroud.getWidth(), frontgroud.getHeight(), null);

            //第一行字
            AttributedString as1 = new AttributedString(msg1);
            int i = msg1.indexOf(".");
            int length = msg1.length();
            //设置同一行字体不同
            as1.addAttribute(TextAttribute.FONT, pinfang_24,0,1);
            as1.addAttribute(TextAttribute.FONT, pinfang_44,1,i);
            as1.addAttribute(TextAttribute.FONT, pinfang_24,i,length);
            g.drawString(as1.getIterator(), 18,45);

            //第二行字
            g.setColor(new Color(253,175,184));
            AttributedString as2 = new AttributedString(msg2);
            as2.addAttribute(TextAttribute.FONT, pinfang_24);
            //删除线
            as2.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 0, msg2.length());
            g.drawString(as2.getIterator(), 25, 85);
            g.dispose();
            //输出图片
            ImageIO.write(background, "png", new FileOutputStream(outPutPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 重定义图片尺寸
     * @param x
     * @param y
     * @param bfi
     * @return
     */
    public static BufferedImage resizeImagePng(int x, int y, BufferedImage bfi) {
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_ARGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }

    public static void main(String[] args) {
        String backgroundPath = "D:\\test\\background.png";
        String frontgroudPath = "D:\\test\\frontgroud.png";
        String msg1 = "¥21.99";
        String msg2 = "¥20.88";
        String outPutPath = "D:\\test\\result.png";
        overlapImage(backgroundPath, frontgroudPath, msg1, msg2, outPutPath);
    }
}

 

你可能感兴趣的:(个人总结)