java多图合成加文字描述

假装没事ソ 提交于 2019-12-10 07:00:32

2中方式:
目的将现有的几张图片及文字描述通过BufferedImage合成一张图片
2种方式都可以实现,相对来说第二种更好一点图片失真度可以忽略,第一种会有失真情况

直接上代码(本地文件地址需要自己改动)
第一种方式:


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.text.AttributedString;
import java.text.ParseException;

public class shareImg {

    public static void main(String[] args) throws ParseException, IOException {
        String templatePath = "C:/Users/Administrator/Desktop/迪奥2/";
        String templateImg = templatePath + "temp.png";

        String mainImg = templatePath + "1.jpg";
        String outPath = templatePath + "res.jpg";
        String moneyContent = "抢购价:" ;
        String goodsMoneyContent =  "¥996";
        String priceMoneyContent =  "¥1152";
        String typeContent = "立即购买";
        String typeContentB = "限时特卖";

        try {
            templateImgAddQRcodeImgAndText(templateImg, mainImg, moneyContent, outPath, typeContent, typeContentB,goodsMoneyContent,priceMoneyContent);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 合成图片
     * @param templateImgPath 模板图片
     * @param mainImg 主图拼接图
     * @param moneyContent 现价
     * @param toPath 保存奔赴位置
     * @param contentText1
     * @param contentText2
     * @param goodsMoneyContent
     * @param priceMoneyContent
     * @throws IOException
     */
    public static void templateImgAddQRcodeImgAndText(String templateImgPath, String mainImg, String moneyContent,
                                                      String toPath, String contentText1, String contentText2,String goodsMoneyContent,String priceMoneyContent) throws IOException {
        //读取模板图
        BufferedImage template = ImageIO.read(new File(templateImgPath));
        //模板宽高
        int width = template.getWidth();
        int height = template.getHeight();
        //主图片
        BufferedImage groupImg = ImageIO.read(new File(mainImg));




        //创建画布 --以模板图的宽高做画布大小
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //创建画笔
        Graphics2D g = img.createGraphics();
        //设置画板背景颜色
        g.setBackground(new Color(255, 255, 255, 255));
        //去除边框
        g.fillRect(0,0,width, height);
        //统一对文字去除锯齿边框
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        //载入模板 -- getScaledInstance方法可以保持图片改变大小不是太失真
        g.drawImage(template.getScaledInstance(width,height, Image.SCALE_SMOOTH), 0, 0, width, height, null);
        //商品主图位置 6 - 6 代表x,y 轴的距离
        g.drawImage(groupImg.getScaledInstance(252,252, Image.SCALE_SMOOTH), 6, 6, 252,252,  null);

        if (contentText1 != null) {
//            字体 / 粗细 / 大小
            Font font = new Font("宋体", Font.BOLD , 22);

            //设置字体及颜色
            Color color = new Color(255, 255, 255);
            g.setColor(color);
            g.setFont(font);
            //文字所在模板位置
            g.drawString(contentText1, 291, 231);
        }

        if (contentText1 != null) {
//            字体 / 粗细 / 大小
            Font font = new Font("苹方", Font.BOLD , 22);

            //设置字体及颜色
            Color color = new Color(255, 31, 9);
            g.setColor(color);
            g.setFont(font);
            //文字所在模板位置
            g.drawString(contentText1, 291, 200);
        }



        if (contentText2 != null) {
            Font font = new Font("微软雅黑", Font.BOLD , 30);
            Color color = new Color(255,255,255);
            g.setColor(color);
            g.setFont(font);
            g.drawString(contentText2, 215, 314);
        }
        if (moneyContent != null) {
            Font font = new Font("苹方 粗体", Font.BOLD, 22);
            Color color = new Color(51,51,51);
            g.setColor(color);
            g.setFont(font);
            g.drawString(moneyContent, 266, 80);
        }
        if (goodsMoneyContent != null) {
            Font font = new Font("微软雅黑", Font.BOLD, 34);
            Color color = new Color(255,80,0);
            g.setColor(color);
            g.setFont(font);
            g.drawString(goodsMoneyContent, 266, 120);
        }
        if (priceMoneyContent != null) {
            Font font = new Font("苹方 粗体", Font.BOLD, 22);
            Color color = new Color(153,153,153 );
            g.setColor(color);

            //给文字加删除线
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            AttributedString as = new AttributedString(priceMoneyContent);
            as.addAttribute(TextAttribute.FONT, font);
            as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 0, priceMoneyContent.length());
            g.drawString(as.getIterator(), 266, 150);
        }
        g.dispose();
        //写入本地位置
        ImageIO.write(img, "jpg", new File(toPath));
    }
}

第二种方式:



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


public class Test {


    public static String overlapImage(String backgroundPath, String frontgroudPath, String goodsMoneyContent, String priceMoneyContent, String outPutPath
            ,String moneyContent,String contentText1,String contentText2) {
        try {

			/**
             * 设置文字字体(具体看方法,路径位置需要有这种字体)
             */
            Font font = getFont("1");
            Font font2 = getFont("2");
            Font font3 = getFont("3");

            Font pinfang_h_42 = font3.deriveFont(Font.PLAIN, 42);
            Font pinfang_h_22 = font3.deriveFont(Font.PLAIN, 22);
            Font pinfang_h_30 = font3.deriveFont(Font.PLAIN, 30);

            Font pinfang_m_42 = font3.deriveFont(Font.PLAIN, 42);
            Font pinfang_m_22 = font3.deriveFont(Font.PLAIN, 22);
            Font pinfang_m_30 = font3.deriveFont(Font.PLAIN, 30);

            Font pinfang_30 = font.deriveFont(Font.PLAIN, 30);
            Font pinfang_42 = font.deriveFont(Font.PLAIN, 42);
            Font pinfang_22 = font.deriveFont(Font.PLAIN, 22);
//            Font pinfang_42 = new Font("PingFang Regular", Font.PLAIN, 42);
//            Font pinfang_22 = new Font("PingFang Regular", Font.PLAIN, 22);
//            Font pinfang_30 = new Font("PingFang Regular", Font.PLAIN, 30);

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

            //在背景图片中添加入需要写入的信息,
            Graphics2D g = background.createGraphics();
            //统一对文字去除锯齿边框
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            //设置为透明覆盖
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            //在背景图片上相框
            g.drawImage(frontgroud, 6, 8, frontgroud.getWidth(), frontgroud.getHeight(), null);

            if (contentText1 != null) {
//            字体 / 粗细 / 大小
//                Font font = new Font("宋体", Font.BOLD , 22);

                //设置字体及颜色
                Color color = new Color(255, 255, 255);
                g.setColor(color);
                g.setFont(pinfang_h_22);
//                AttributedString as2 = new AttributedString(contentText1);
//                as2.addAttribute(TextAttribute.FONT, pinfang_42);
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                //文字所在模板位置
                g.drawString(contentText1, 294, 232);
            }
            if (contentText2 != null) {
//            字体 / 粗细 / 大小
//                Font font = new Font("苹方", Font.BOLD , 22);

                //设置字体及颜色
                Color color = new Color(255, 255, 255);
                g.setColor(color);
//                g.setFont(pinfang_30);
                g.setFont(pinfang_m_30);
//                AttributedString as2 = new AttributedString(contentText2);
//                as2.addAttribute(TextAttribute.FONT, pinfang_22);
                //文字所在模板位置
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
                g.drawString(contentText2, 215, 313);
            }
            if (moneyContent != null) {
//                Font font = new Font("苹方 粗体", Font.BOLD, 22);
                Color color = new Color(51,51,51);
                g.setColor(color);
                g.setFont(pinfang_22);
//                AttributedString as2 = new AttributedString(moneyContent);
//                as2.addAttribute(TextAttribute.FONT, pinfang_22);
                g.drawString(moneyContent, 266, 90);
            }
            if (goodsMoneyContent != null) {
//                Font font = new Font("微软雅黑", Font.BOLD, 34);
                Color color = new Color(255,80,0);
                g.setColor(color);
                g.setFont(pinfang_h_42);
//                AttributedString as2 = new AttributedString(goodsMoneyContent);
//                as2.addAttribute(TextAttribute.FONT, pinfang_42);


                g.drawString(goodsMoneyContent, 266, 135);
            }
            if (priceMoneyContent != null) {
//                Font font = new Font("苹方 粗体", Font.BOLD, 22);
                Color color = new Color(153,153,153 );
                g.setColor(color);

                /*//给文字加删除线
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);*/
                AttributedString as = new AttributedString(priceMoneyContent);
//                as.addAttribute(TextAttribute.FONT, font);
                as.addAttribute(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON, 0, priceMoneyContent.length());
                as.addAttribute(TextAttribute.FONT, pinfang_22);
                g.drawString(as.getIterator(), 266, 162);
            }
            g.dispose();
            //输出图片(png格式,尝试过jpg,不能用.具体还未研究)
            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 frontgroudPath = "C:\\Users\\Administrator\\Desktop\\迪奥2\\1.jpg";
        String backgroundPath = "C:\\Users\\Administrator\\Desktop\\迪奥2\\temp.png";
        String goodsMoneyContent = "¥599";
        String priceMoneyContent = "¥1099";
        String moneyContent = "限时价:" ;
        String typeContent = "立即抢购";
        String typeContentB = "限时特卖";
        String outPutPath = "C:\\Users\\Administrator\\Desktop\\迪奥2\\demo2.png";
        overlapImage(backgroundPath, frontgroudPath, goodsMoneyContent, priceMoneyContent, outPutPath,moneyContent,typeContent,typeContentB);
    }


    //filepath字体文件的路径
    private static Font getFont(String type){
        Font font = null;
        String filepath = "";
        if("1".equals(type)){
            filepath = "C:\\Users\\Administrator\\Desktop\\字体\\PingFang Regular.ttf";
        }else if("2".equals(type)){
            filepath = "C:\\Users\\Administrator\\Desktop\\字体\\PingFang Medium.ttf";
        }else if("3".equals(type)){
            filepath = "C:\\Users\\Administrator\\Desktop\\字体\\PingFang Heavy.ttf";
        }else{
            filepath = "C:\\Users\\Administrator\\Desktop\\字体\\PingFang Regular.ttf";
        }

        File file = new File(filepath);
        try{
            font = Font.createFont(Font.TRUETYPE_FONT, file);
        }catch (FontFormatException e){
            return null;
        }catch (FileNotFoundException e){
            return null;
        }catch (IOException e){
            return null;
        }
        return font;
    }

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!