java实现图片合成功能,两张图合成一张图

这是在工作中遇到的一个问题,现在就来总结下~~

首先直接上代码

参数介绍:

        content:是一个链接 我们要将其转为一个二维码,所以用到了一个工具类QRCodeUtil

        productName:就是简单的一个字符串

        posterUrl:这是另一张图片的地址

最终需求:将二维码图片和海报图合成一张图片

    public AjaxResult generatePoster(String content, String productName, String posterUrl, HttpServletResponse servletResponse) throws IOException, WriterException {
        try {
            BufferedImage origonPosterBI = ImageIO.read(new URL(posterUrl));
            int posterWidth = 1000;
            double scale = (double)posterWidth / origonPosterBI.getWidth();
            int posterHeight = (int) (origonPosterBI.getHeight() * scale);
            Image posterImg = origonPosterBI.getScaledInstance(posterWidth, posterHeight, Image.SCALE_DEFAULT);
            //创建一个新的缓存图片
            BufferedImage PosterBI = new BufferedImage(posterWidth, posterHeight, BufferedImage.TYPE_INT_RGB);
            //获取画笔
            Graphics2D graphics = PosterBI.createGraphics();
            //将Image对象画在画布上,最后一个参数,ImageObserver:接收有关 Image 信息通知的异步更新接口,没用到直接传空
            graphics.drawImage(posterImg, 0, 0,null);


            BufferedImage qrCodeBI = QRCodeUtil.getBufferedImage(content, 200, 200);

            int imageWidth = posterWidth;
            int imageHeight = posterHeight + 200;
            BufferedImage canvas = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = (Graphics2D) canvas.getGraphics();
            g.setBackground(Color.WHITE);//设置背景色
            g.clearRect(0, 0, imageWidth, imageHeight);
            g.drawImage(PosterBI, 0, 0, null);

            g.drawImage(qrCodeBI, 0, posterHeight, null);

            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            g.setColor(new Color(125, 127, 131));
            Font font = new Font("黑体", Font.PLAIN,42);
            g.setFont(font);
            if (productName.length()>20){
                g.drawString(productName.substring(0,18)+"...", 220, posterHeight + 80);
            }else {
                g.drawString(productName, 220, posterHeight + 80);
            }
            g.setColor(Color.black);
            g.setFont(font);
            g.drawString("长按识别二维码查看产品详情及投保", 220, posterHeight + 150);

            g.dispose();


            File file = new File(SYS_TEM_DIR + "tempposter" + productName + SecurityUtils.getUserId() + System.currentTimeMillis());
            ImageIO.write(canvas, "jpg", file);
            SysFile sysFile = toolLocalStorageService.upload(null, file,null, 3600);
            String url = sysFile.getPath();
            return AjaxResult.success(url);
        } catch (Exception e) {
            return AjaxResult.error();
        }

    }

工具类:

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 二维码工具
 * @Author:debug (SteadyJack)
 * @Link: weixin-> debug0868  qq-> 1948831260
 * @Date: 2020/11/16 22:38
 **/
public class QRCodeUtil {
    private static final Logger log= LoggerFactory.getLogger(QRCodeUtil.class);

    //CODE_WIDTH:二维码宽度,单位像素
    private static final int CODE_WIDTH = 200;
    //CODE_HEIGHT:二维码高度,单位像素
    private static final int CODE_HEIGHT = 200;
    //FRONT_COLOR:二维码前景色,0x000000 表示黑色
    private static final int FRONT_COLOR = 0x000000;
    //BACKGROUND_COLOR:二维码背景色,0xFFFFFF 表示白色
    //演示用 16 进制表示,和前端页面 CSS 的取色是一样的,注意前后景颜色应该对比明显,如常见的黑白
    private static final int BACKGROUND_COLOR = 0xFFFFFF;

    public static void createCodeToFile(String content, File codeImgFileSaveDir, String fileName) {
        try {
            if (StringUtils.isBlank(content) || StringUtils.isBlank(fileName)) {
                return;
            }
            content = content.trim();
            if (codeImgFileSaveDir==null || codeImgFileSaveDir.isFile()) {
                //二维码图片存在目录为空,默认放在桌面...
                codeImgFileSaveDir = FileSystemView.getFileSystemView().getHomeDirectory();
            }
            if (!codeImgFileSaveDir.exists()) {
                //二维码图片存在目录不存在,开始创建...
                codeImgFileSaveDir.mkdirs();
            }

            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content, CODE_WIDTH, CODE_HEIGHT);

            File codeImgFile = new File(codeImgFileSaveDir, fileName);
            ImageIO.write(bufferedImage, "png", codeImgFile);

            log.info("二维码图片生成成功:" + codeImgFile.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成二维码并输出到输出流, 通常用于输出到网页上进行显示,输出到网页与输出到磁盘上的文件中,区别在于最后一句 ImageIO.write
     * write(RenderedImage im,String formatName,File output):写到文件中
     * write(RenderedImage im,String formatName,OutputStream output):输出到输出流中
     * @param content  :二维码内容
     * @param outputStream :输出流,比如 HttpServletResponse 的 getOutputStream
     */
    public static void createCodeToOutputStream(String content, OutputStream outputStream) {
        try {
            if (StringUtils.isBlank(content)) {
                return;
            }
            content = content.trim();
            //核心代码-生成二维码
            BufferedImage bufferedImage = getBufferedImage(content, CODE_WIDTH, CODE_HEIGHT);

            //区别就是这一句,输出到输出流中,如果第三个参数是 File,则输出到文件中
            ImageIO.write(bufferedImage, "png", outputStream);

            log.info("二维码图片生成到输出流成功...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //核心代码-生成二维码
    public static BufferedImage getBufferedImage(String content, int width, int height) throws WriterException {

        //com.google.zxing.EncodeHintType:编码提示类型,枚举类型
        Map<EncodeHintType, Object> hints = new HashMap();

        //EncodeHintType.CHARACTER_SET:设置字符编码类型
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");

        //EncodeHintType.ERROR_CORRECTION:设置误差校正
        //ErrorCorrectionLevel:误差校正等级,L = ~7% correction、M = ~15% correction、Q = ~25% correction、H = ~30% correction
        //不设置时,默认为 L 等级,等级不一样,生成的图案不同,但扫描的结果是一样的
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);

        //EncodeHintType.MARGIN:设置二维码边距,单位像素,值越小,二维码距离四周越近
        hints.put(EncodeHintType.MARGIN, 1);

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
        BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                bufferedImage.setRGB(x, y, bitMatrix.get(x, y) ? FRONT_COLOR : BACKGROUND_COLOR);
            }
        }
        return bufferedImage;
    }
}

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值