## 分享图;

This commit is contained in:
cabbage 2025-09-22 15:15:05 +08:00
parent f5d9ebf5dc
commit 239d1868ff
4 changed files with 85 additions and 12 deletions

View File

@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.config.OssConfig;
import com.hzs.common.core.enums.EShareType;
import com.hzs.common.core.utils.BarCodeUtils;
import com.hzs.third.share.unit.BarCodeUtils;
import com.hzs.common.domain.third.share.TShareCode;
import com.hzs.third.share.dto.ShareServiceDTO;
import com.hzs.third.share.mapper.TShareCodeMapper;

View File

@ -1,24 +1,24 @@
package com.hzs.common.core.utils;
package com.hzs.third.share.unit;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.hzs.common.core.utils.OssUtil;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author: sui q
* @time: 2021/11/23 11:02
* @description: 条形码+文字 生成
* @classname: BarCodeUtils
* @package_name: com.hzs.web.util
* 条形码+文字 生成
*/
@Slf4j
public class BarCodeUtils {
@ -32,21 +32,30 @@ public class BarCodeUtils {
public static String createZeroQRCode(String contents) {
String filePath = "";
try {
String ext = "png";
String ext = "jpg";
Map<EncodeHintType, Object> hints = new HashMap<>();
// 生成二维码时白边框间隔距离
hints.put(EncodeHintType.MARGIN, 1);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix bitMatrix = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, 320, 320, hints);
BitMatrix bitMatrix = qrCodeWriter.encode(contents, BarcodeFormat.QR_CODE, 260, 260, hints);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix, ext, byteArrayOutputStream);
final InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ImageComposer composer = new ImageComposer();
// 加载背景图
BufferedImage baseImage = composer.loadBaseImage();
// 合成图片
BufferedImage composedImage = composer.composeImages(baseImage, ImageIO.read(inputStream));
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(composedImage, "png", os);
final InputStream is = new ByteArrayInputStream(os.toByteArray());
// 上传OSS
filePath = OssUtil.uploadFileForStream(inputStream, ext, "shareCode", null);
filePath = OssUtil.uploadFileForStream(is, ext, "shareCode", null);
// filePath = OssUtil.uploadFileForStream(inputStream, ext, "shareCode", null);
} catch (Exception e) {
log.error("createZeroQRCode error! contents: {}", contents, e);
}

View File

@ -0,0 +1,64 @@
package com.hzs.third.share.unit;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class ImageComposer {
public BufferedImage loadImage(String filePath) throws IOException {
return ImageIO.read(new File(filePath));
}
public BufferedImage loadBaseImage() throws IOException {
InputStream inputStream = ImageComposer.class.getClassLoader().getResourceAsStream("share.jpg");
return ImageIO.read(inputStream);
}
public BufferedImage composeImages(BufferedImage baseImage, BufferedImage overlayImage) {
int overlayWidth = 255;
int overlayHeight = 255;
BufferedImage composedImage = new BufferedImage(
baseImage.getWidth(),
baseImage.getHeight(),
BufferedImage.TYPE_INT_ARGB
);
Graphics2D g2d = composedImage.createGraphics();
g2d.drawImage(baseImage, 0, 0, null);
g2d.drawImage(overlayImage, 829, 1547, overlayWidth, overlayHeight, null);
Font font = new Font("Serif", Font.BOLD, 55);
g2d.setFont(font);
g2d.drawString("BL****5678", 500, 2425);
g2d.dispose();
return composedImage;
}
public void saveImage(BufferedImage image, String filePath) throws IOException {
ImageIO.write(image, "png", new File(filePath));
}
// public static void main(String[] args) {
// try {
// ImageComposer composer = new ImageComposer();
//
// // 加载图片
// BufferedImage baseImage = composer.loadImage("d://图片合成//base.jpg");
// BufferedImage overlayImage = composer.loadImage("d://图片合成//code.png");
//
// // 合成图片
// BufferedImage composedImage = composer.composeImages(baseImage, overlayImage);
// // 保存合成后的图片
// composer.saveImage(composedImage, "d://图片合成//print.jpg");
// System.out.println("图片合成成功!");
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 619 KiB