java-使用Graphics2d合成图片和文字

需求:使用一些图片和用户输入的文案合成图片或者海报

使用三方库

mvn管理

<!--图片处理 start -->
<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-jpeg</artifactId>
    <version>3.6</version>
</dependency>
<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-tiff</artifactId>
    <version>3.6</version>
</dependency>
<!--
Optional dependency. Needed only if you deploy `ImageIO` plugins as part of a web app.
Make sure you add the `IIOProviderContextListener` to your `web.xml`, see above.
-->
<dependency>
    <groupId>com.twelvemonkeys.servlet</groupId>
    <artifactId>servlet</artifactId>
    <version>3.6</version>
</dependency>
<!--图片处理 end -->

实现方法

循环合成所有需求

import com.aaa.bbb.common.utils.HttpsUtils;
import com.aaa.bbb.common.utils.UploadImageUtil;
import com.aaa.bbb.common.vo.loan.LoanUserSynthesisListVo;
import org.apache.commons.lang3.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author
 * @Date
 **/
public class ImageUtils {

    /**
     * 操作合成图片
     * @param SynthesisList 操作单张合成图
     * @return
     */
    public String optImage(List<SynthesisListVo> SynthesisList) {
        try {
            // 与前端约定  合成图片列表的第一张图片为底图
            SynthesisListVo backgroundImageInfo = SynthesisList.get(0);
            //设置图片大小  宽  高  网络图片
            // 获取网络图片
            BufferedImage getBackgroundUrlImage = this.getUrlImage(backgroundImageInfo.getImageUrl());
            BufferedImage background = this.resizeImagePng(backgroundImageInfo.getWidth(), backgroundImageInfo.getHeight(), getBackgroundUrlImage);
            // 底部宽
            Integer SynthesisListVo = backgroundImageInfo.getWidth();
            //在背景图片中添加入需要写入的信息,
            Graphics2D g = background.createGraphics();  // x58  y74.3
            //设置为透明覆盖
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 1.0f));
            Boolean isFirst  = true;
            for (SynthesisListVo SynthesisListItem : SynthesisList) {
                if (isFirst) {
                    isFirst = false;
                    continue;
                }
                if (SynthesisListItem.getType().equals("Image")) {
                    // 图片处理 创建图片
                    // 获取网络图片
                    BufferedImage getUrlImage = this.getUrlImage(SynthesisListItem.getImageUrl());
                    // 重新定义图片尺寸
                    BufferedImage userImage = this.resizeImagePng(SynthesisListItem.getWidth(), SynthesisListItem.getHeight(), getUrlImage);
                    // 图片 偏移x  偏移y 图片的宽 图片的高
                    g.drawImage(userImage, SynthesisListItem.getOffsetX(), SynthesisListItem.getOffsetY(), userImage.getWidth(), userImage.getHeight(), null);
                } else if (SynthesisListItem.getType().equals("Text")) {
                    // Font pinfang_44 = new Font("苹方-简 中黑体", Font.BOLD, 44);
                    Font pinfang = new Font(SynthesisListItem.getTextFontFamily(), Font.PLAIN, SynthesisListItem.getTextFontSize());
                    // 写字
                    //指定字体
                    FontMetrics metrics = g.getFontMetrics(pinfang);
                    //指定要绘制的字符串
                    String useMeg = String.valueOf(SynthesisListItem.getText());
                    //得到字符串绘制宽度
                    int logoW = metrics.stringWidth(useMeg);
                    //计算绘制原点坐标,
                    //文本最左边位于x轴logoX
                    // 判断是否水平居中
                    int logoX = 0;
                    if (SynthesisListItem.getTextAlignCenter() != null && SynthesisListItem.getTextAlignCenter()) {
                        // 水平居中
                        logoX = (maxBackgroundWidth - logoW) / 2;
                    } else {
                        logoX = SynthesisListItem.getOffsetX();
                    }
                    //文本基于logoH
                    int logoH = SynthesisListItem.getOffsetY() + (metrics.getAscent());
                    //绘制文本框
                    g.setFont(pinfang);
                    g.setColor(new Color(SynthesisListItem.getTextColor().get(0),SynthesisListItem.getTextColor().get(1),SynthesisListItem.getTextColor().get(2)));
                    g.drawString(useMeg, logoX, logoH);
                }
            }
            g.dispose();
            //输出图片 上传七牛
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(background, "png", out);
            byte[] b = out.toByteArray();
            String hash = UploadImageUtil.uploadByteImage(b);
            if (StringUtils.isNotEmpty(hash)) {
                // 返回图片路径
                return UploadImageUtil.IMG_HOST + hash;
            }
//            ImageIO.write(background, "png", new FileOutputStream(outPutPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取网络图片数据
     * @param url
     * @return
     */
    public BufferedImage getUrlImage(String url) {
        byte[] bytes = HttpsUtils.getBytes(url);
        InputStream buffin = new ByteArrayInputStream(bytes,0,bytes.length);
        BufferedImage img = null;
        try {
            img = ImageIO.read(buffin);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return img;
    }

    /**
     * 重定义图片尺寸
     * @param x
     * @param y
     * @param bfi
     * @return
     */
    public 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;
    }

}

  

上一篇:Java后台生成条形码(linux)生成条形码BarcodePDF417


下一篇:动态生成验证码案例(Java)