java 实现图片合成,并添加文字

论坛 期权论坛 脚本     
niminba   2021-5-22 14:49   37   0

最近公司一个需要,需要把商品的优惠卷分享链接,生成一个二维码然后和商品主图合成一张,并且在新合成的主图增加商品信息的描述,好了直接看合成后图片的样式

下面我就直接贴代码,首先是Contorller层

/**
   * 淘宝二维码,商品主图,合成一张图
   * 
   * @param pictUrl
   * @param request
   * @param response
   * @throws IOException
   */
  @RequestMapping("/getTaoBaoqQRCode")
  public void getTaoBaoqQRCode(TaoBaoQRCode taoBaoQRCode, HttpServletRequest request,
      HttpServletResponse response) throws IOException {
    ServletOutputStream os = null;
    InputStream buffin = null;
    try {
      // 二维码
      String couponUlr = "https:" + taoBaoQRCode.getCouponShareUrl();// 高额卷分享链接
      byte[] imgByte = QrCodeUtil.createQrCode2Bytes(250, 250, couponUlr);
      buffin = new ByteArrayInputStream(imgByte);
      BufferedImage couponImage = ImageIO.read(buffin);
      // 商品主图
      String imageUrl = "https:" + taoBaoQRCode.getPictUrl();
      URL url = new URL(imageUrl);
      BufferedImage picImage = ImageIO.read(url);
      BufferedImage modifyImage =
          imageHandleUtil.mergeImage(picImage, couponImage, taoBaoQRCode.getTitle(),
              taoBaoQRCode.getReservePrice(), taoBaoQRCode.getZkFinalPrice());
      response.setContentType("image/jpg");
      os = response.getOutputStream();
      ImageIO.write(modifyImage, "jpg", os);
      os.flush();
    } catch (Exception e) {
      LOGGER.error("getTaoBaoqQRCode error");
      e.printStackTrace();
    } finally {
      buffin.close();
      os.close();
    }
  }

二维码QrCodeUtil 生成帮助类

public class QrCodeUtil {
  private static final int DAFAULT_WIDTH = 360;
  private static final int DAFAULT_HEIGHT = 360;

  private static final Logger LOGGER = LoggerFactory.getLogger(QrCodeUtil.class);

  public static String createQrCode(String text) {
    return createQrCode(DAFAULT_WIDTH, DAFAULT_HEIGHT, text);
  }

  public static String createQrCode(int widht, int height, String text) {
    HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
      byte[] bytes = createQrCode2Bytes(widht, height, text);
      String fileName = UUID.randomUUID().toString().replaceAll("-", "") + ".png";
      return UpYunClient.upload(fileName, bytes);
    } catch (Exception e) {
      LOGGER.error("create qrcode error", e);
    }
    return null;
  }


  public static byte[] createQrCode2Bytes(String text) {
    return createQrCode2Bytes(DAFAULT_WIDTH, DAFAULT_HEIGHT, text);
  }

  public static byte[] createQrCode2Bytes(int widht, int height, String text) {
    HashMap<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
    hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
    try {
      BitMatrix bitMatrix =
          new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, widht, height,
              hints);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
      ImageIO.write(image, "png", out);
      return out.toByteArray();
    } catch (Exception e) {
      LOGGER.error("create qrcode error", e);
    }
    return null;
  }

  /**
   * 生成条形码并已字节码形式返回,生成的图片格式为png
   * 
   * @param contents
   * @param width
   * @param height
   * @return
   */
  public static byte[] createBarcode2Byte(String contents, int width, int height) {
    int codeWidth = 3 + // start guard
        (7 * 6) + // left bars
        5 + // middle guard
        (7 * 6) + // right bars
        3; // end guard
    codeWidth = Math.max(codeWidth, width);
    try {
      BitMatrix bitMatrix =
          new MultiFormatWriter().encode(contents, BarcodeFormat.CODE_128, codeWidth,
              height, null);
      BufferedImage image = MatrixToImageWriter.toBufferedImage(bitMatrix);
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      ImageIO.write(image, "png", out);
      return out.toByteArray();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }
}

二维码生成我这里用的是谷歌的看下面maven pom.xml 文件

<!-- 条形码、二维码生成 -->
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>2.2</version>
    </dependency>
    <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>javase</artifactId>
      <version>2.2</version>
    </dependency>

合成图片方法如何

package com.qft.campuscircle.common.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
*3vr'ZWR">r/RjZW>	EI
3vj"G"_jr3h4(b{:nB#"C*Z_j3nknB#"CjZgΣnZ
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP