package com.xzx.gc.common.utils.image; import cn.hutool.core.io.FileUtil; import com.xzx.gc.common.constant.Constants; import com.xzx.gc.common.utils.BusinessUtil; import lombok.extern.slf4j.Slf4j; import net.coobird.thumbnailator.Thumbnails; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; import java.util.HashMap; import java.util.Map; @Component @Slf4j public class GraphicsUtils { @Autowired private BusinessUtil businessUtil; /** * @param imgName 图片名称 * @param codePath 二维码路径 * @param avata 头像全路径 * @param backgroundUrl 背景图地址 * @param spuName 文字 * @throws Exception */ public Map generatePoster(String imgName, String codePath, String avata, String backgroundUrl, String spuName, Integer x1, Integer y1, Integer x2, Integer y2, Integer x3, Integer y3, int size) { String urlPrefix = null; String imgPath = null; try { urlPrefix = businessUtil.getViewUrl(); imgPath = Constants.IMG_UPLOAD_PATH + imgName; if (!FileUtil.exist(imgPath)) { //背景图 BufferedImage bufferImage = QrCodeBaseUtils.imageToBufferedImage(backgroundUrl); Graphics2D graphics = bufferImage.createGraphics(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //小程序码 BufferedImage qrCodeImage = ImageIO.read(new File(codePath)); graphics.drawImage(qrCodeImage, x1, y1, null); //头像 if (x2 != null) { BufferedImage logoBufferImage = Thumbnails.of(new URL(avata)).size(150, 150).asBufferedImage(); graphics.drawImage(logoBufferImage, x2, y2, null); } //文字 if (x3 != null) { Font font = new Font("黑体", Font.BOLD, size); graphics.setFont(font); graphics.setPaint(Color.black); graphics.drawString(spuName, x3, y3); } QrCodeGraphicsUtils.savePic(bufferImage, 1, "jpg", 0.8, imgPath); graphics.dispose(); File file = new File(imgPath); long uploadUrl = FileUtil.size(file); //小于10kb重新生成 if (uploadUrl <= Constants.MIN_FILE_SIZE) { log.error("生成海报失败:图片大小异常:{}", uploadUrl); return null; } } else { //判断文件是否正常 不正常 删除 File file = new File(imgPath); long uploadUrl = FileUtil.size(file); if (uploadUrl <= Constants.MIN_FILE_SIZE) { FileUtil.del(file); return null; } } } catch (Exception e) { return null; } log.debug("生成海报成功,路径:" + imgPath); Map map = new HashMap<>(); map.put("viewUrl", urlPrefix + "/" + imgName); map.put("uploadUrl", imgPath); return map; } // public static void createPosterByRedTemplate(byte[] linkUrl, boolean logoStatus, String logoPath, // String backgroundUrl, String spuPicUrl, String spuLeaguerPrice, String spuPrice, String spuName) // throws Exception { // // qrCode //// BufferedImage qrCodeImage = QrCodeGraphicsUtils.createQrCode(linkUrl, false, logoStatus, logoPath, true, 160); // ByteArrayInputStream in = new ByteArrayInputStream(linkUrl); //// BufferedImage qrCodeImage = ImageIO.read(in ); // // File file = FileUtil.writeFromStream(in, new File("d:/a.jpg")); // // // 海报背景 // BufferedImage bufferImage = QrCodeBaseUtils.imageToBufferedImage(backgroundUrl); // // Graphics2D graphics = bufferImage.createGraphics(); // graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // graphics = bufferImage.createGraphics(); // // 绘制 qrCode //// graphics.drawImage(qrCodeImage, 437, 616, null); // // // 绘制 头像 // // graphics = QrCodeGraphicsUtils.drawAvatar(graphics, logoPath, bufferImage, 32, 714); // // // 商品主图 // BufferedImage spuPicBufferImage = null; // spuPicBufferImage = QrCodeBaseUtils.imageToBufferedImage(spuPicUrl); // // 绘制商品主图 // // graphics.drawImage(spuPicBufferImage, 244, 249, null); // // // 文本 // // QrCodeGraphicsUtils.drawRightTextNewLine(graphics, spuName, 235, 132, 35, 346, Color.WHITE, 24, 2, 350); // // // 会员特价 // Font font = new Font("微软雅黑", Font.PLAIN, 26); // graphics.setFont(font); // // QrCodeGraphicsUtils.drawText(graphics, spuLeaguerPrice, 232, 65, Color.WHITE); // // 原价 // Font font2 = new Font("微软雅黑", Font.PLAIN, 22); // graphics.setFont(font2); // // QrCodeGraphicsUtils.drawText(graphics, spuPrice, 232, 100, Color.WHITE); // // graphics.dispose(); // // ByteArrayOutputStream os = new ByteArrayOutputStream(); // ImageIO.write(bufferImage, "JPG", os); // byte[] arrayImage = os.toByteArray(); // InputStream inputStream = new ByteArrayInputStream(arrayImage); // if (os != null) { // os.close(); // } // if (inputStream != null) { // inputStream.close(); // } // // QrCodeGraphicsUtils.savePic(bufferImage, 1, "jpg", 0.8, System.currentTimeMillis() + ""); // } }