|  |  | 
 |  |  | package com.doumee.core.utils; | 
 |  |  |  | 
 |  |  | import com.google.zxing.common.BitMatrix; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.apache.commons.fileupload.FileItem; | 
 |  |  | import org.apache.commons.fileupload.FileItemFactory; | 
 |  |  | import org.apache.commons.fileupload.disk.DiskFileItemFactory; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.checkerframework.checker.units.qual.C; | 
 |  |  | import org.springframework.http.MediaType; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  | import org.springframework.web.multipart.commons.CommonsMultipartFile; | 
 |  |  | 
 |  |  | import javax.imageio.stream.ImageOutputStream; | 
 |  |  | import java.awt.*; | 
 |  |  | import java.awt.geom.Ellipse2D; | 
 |  |  | import java.awt.geom.RoundRectangle2D; | 
 |  |  | import java.awt.image.BufferedImage; | 
 |  |  | import java.io.*; | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.math.RoundingMode; | 
 |  |  | import java.net.URL; | 
 |  |  |  | 
 |  |  | @Slf4j | 
 |  |  | public class GeneratePicUtil { | 
 |  |  |  | 
 |  |  |     static String bg="https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230404/SysBaseBackGround/D56231512C0B4A4C9D06B02F9566B0B0.png"; | 
 |  |  | 
 |  |  |             g2.drawRect( 0, 0, 750, 700); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         int th = 30; | 
 |  |  |         //名称 | 
 |  |  |         if(name.length()>5){ | 
 |  |  |             name = name.substring(0,5)+"..."; | 
 |  |  | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static BufferedImage getImgIO(String imgurl) { | 
 |  |  |     public static BufferedImage getImgIO(String imgurl) { | 
 |  |  |         try { | 
 |  |  |         return     ImageIO.read(new URL(imgurl)); | 
 |  |  |         }catch (Exception e){ | 
 |  |  |  | 
 |  |  |         }return null; | 
 |  |  |     } | 
 |  |  |     private static BufferedImage getImgIO(InputStream is) { | 
 |  |  |         if(is ==null){ | 
 |  |  |           log.error("=============================qrcode error 小程序码为空"); | 
 |  |  |         }else{ | 
 |  |  |  | 
 |  |  |             log.error("=============================qrcode okay 小程序码有有數據"); | 
 |  |  |         } | 
 |  |  |         try { | 
 |  |  |              return     ImageIO.read(is); | 
 |  |  |         }catch (Exception e){ | 
 |  |  |             log.error("=============================qrcode error 小程序码为空11111111111:"+e.getMessage()); | 
 |  |  |  | 
 |  |  |         } | 
 |  |  |         return null; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public static InputStream generateShareImg(String imgurl, InputStream mpCode1, String title) { | 
 |  |  |  | 
 |  |  |         BufferedImage bi =  new BufferedImage(750, 976, BufferedImage.TYPE_INT_RGB); | 
 |  |  |         //得到它的绘制环境(这张图片的笔) | 
 |  |  |         Graphics2D g2 = (Graphics2D) bi.getGraphics(); | 
 |  |  |         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | 
 |  |  |         g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | 
 |  |  |         g2.fillRect(0, 0, 750, 976); | 
 |  |  |         //背景图片 | 
 |  |  |         dealTitleCenter(g2,th,title); | 
 |  |  |         if(StringUtils.isNotBlank(imgurl)){ | 
 |  |  |             BufferedImage img1 =getImgIO(imgurl); | 
 |  |  |             if(img1!=null){ | 
 |  |  |                 int w = img1.getWidth(); | 
 |  |  |                 int h = img1.getHeight(); | 
 |  |  |                 int h1 = (int)((double)w *502/670); | 
 |  |  |                 int w1 = (int)((double)h *670/502); | 
 |  |  |                 if(h>h1){ | 
 |  |  |                     h = h1; | 
 |  |  |                 } | 
 |  |  |                 if(w>w1){ | 
 |  |  |                     w = w1; | 
 |  |  |                 } | 
 |  |  |                 img1 = img1.getSubimage(0,0,w,h); | 
 |  |  |                 g2.drawImage(setRadius(img1,20,0,0), 40, 204, 670 ,502, null); | 
 |  |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  | //        g2.setColor(Color.white); | 
 |  |  | //        g2.drawRoundRect(39,204, 672, 502, 1,1);//画一个圆角矩形 | 
 |  |  |  | 
 |  |  |         g2.setColor(Color.black); | 
 |  |  |         g2.setFont(new Font("黑体", Font.BOLD, 40)); | 
 |  |  |         g2.drawString("长按识别小程序", 40,768+th); | 
 |  |  |         g2.setColor(Color.gray); | 
 |  |  |         g2.setFont(new Font("黑体", Font.PLAIN, 32)); | 
 |  |  |         g2.drawString("查看更多精彩内容", 40,820+th); | 
 |  |  |  | 
 |  |  |  | 
 |  |  |         BufferedImage img3 = getImgIO(mpCode1); | 
 |  |  |         if(img3!=null){ | 
 |  |  |             g2.setColor(Color.white); | 
 |  |  |             g2.drawImage(img3, 530, 736, 180, 180, null); | 
 |  |  |         } | 
 |  |  |         InputStream inputStream = bufferedImageToInputStream(bi); | 
 |  |  |         return inputStream; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 图片设置圆角 | 
 |  |  |      * @param srcImage | 
 |  |  |      * @return | 
 |  |  |      * @throws | 
 |  |  |      */ | 
 |  |  |     public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding){ | 
 |  |  |         int width = srcImage.getWidth(); | 
 |  |  |         int height = srcImage.getHeight(); | 
 |  |  |         int canvasWidth = width + padding * 2; | 
 |  |  |         int canvasHeight = height + padding * 2; | 
 |  |  |  | 
 |  |  |         BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB); | 
 |  |  |         Graphics2D gs = image.createGraphics(); | 
 |  |  |         gs.setComposite(AlphaComposite.Src); | 
 |  |  |         gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | 
 |  |  |         gs.setColor(Color.WHITE); | 
 |  |  |         gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius)); | 
 |  |  |         gs.setComposite(AlphaComposite.SrcAtop); | 
 |  |  |         gs.drawImage(setClip(srcImage, radius), padding, padding, null); | 
 |  |  |         if(border !=0){ | 
 |  |  |             gs.setColor(Color.white); | 
 |  |  |             gs.setStroke(new BasicStroke(border)); | 
 |  |  |             gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius); | 
 |  |  |         } | 
 |  |  |         gs.dispose(); | 
 |  |  |         return image; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 图片切圆角 | 
 |  |  |      * @param srcImage | 
 |  |  |      * @param radius | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public static BufferedImage setClip(BufferedImage srcImage, int radius){ | 
 |  |  |         int width = srcImage.getWidth(); | 
 |  |  |         int height = srcImage.getHeight(); | 
 |  |  |         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); | 
 |  |  |         Graphics2D gs = image.createGraphics(); | 
 |  |  |  | 
 |  |  |         gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | 
 |  |  |         gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius)); | 
 |  |  |         gs.drawImage(srcImage, 0, 0, null); | 
 |  |  |         gs.dispose(); | 
 |  |  |         return image; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private static void dealTitleCenter(Graphics2D g2, int th, String title) { | 
 |  |  |         String[] titles  = title.split("&"); | 
 |  |  |         for (int i = 0; i < titles.length && i<2; i++) { | 
 |  |  |             String name = titles[i]; | 
 |  |  |             //名称 | 
 |  |  |             if(name.length()>=17){ | 
 |  |  |                 name = name.substring(0,17); | 
 |  |  |             } | 
 |  |  |             Font font =new Font("黑体", Font.BOLD, 38); | 
 |  |  |             FontMetrics fontMetrics = g2.getFontMetrics(font); | 
 |  |  |             int centerX = 750 / 2; | 
 |  |  |             int textWidth = fontMetrics.stringWidth(name); | 
 |  |  |             g2.setColor(Color.black); | 
 |  |  |             g2.setFont(font); | 
 |  |  |             g2.drawString(name, centerX - textWidth / 2, 60+th+i*55); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |     public static int th = 30; | 
 |  |  |     public static InputStream generateShareWithUserImg(BufferedImage imgurl, InputStream mpCode, String header, String name) { | 
 |  |  |         int w = imgurl.getWidth(); | 
 |  |  |         int h = imgurl.getHeight(); | 
 |  |  |         double rate = (double)w/750d; | 
 |  |  |  | 
 |  |  |         //得到它的绘制环境(这张图片的笔) | 
 |  |  |         Graphics2D g2 = (Graphics2D) imgurl.getGraphics(); | 
 |  |  |         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); | 
 |  |  |         g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); | 
 |  |  |         //背景图片 | 
 |  |  |        | 
 |  |  |         BufferedImage img1 =getImgIO(header); | 
 |  |  |         if(img1!=null){ | 
 |  |  |             g2.drawImage(setRadius(img1,img1.getWidth(),0,0), (int)(20*rate), (int)(h-68*rate), (int)(48*rate) ,(int)(48*rate), null); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         g2.setColor(Color.white); | 
 |  |  |         g2.setFont(new Font("黑体", Font.PLAIN, (int)(28 * rate))); | 
 |  |  |         g2.drawString(name, (int) (80*rate),h-(int)((64-th)*rate)); | 
 |  |  |         Font font =new Font("黑体", Font.PLAIN, (int)(24 * rate)); | 
 |  |  |         g2.setColor(Color.white); | 
 |  |  |         g2.setFont(font); | 
 |  |  |         FontMetrics fontMetrics = g2.getFontMetrics(font); | 
 |  |  |         int textWidth = fontMetrics.stringWidth("扫码看更多"); | 
 |  |  |         g2.drawString("扫码看更多", (int)(w -(textWidth+20*rate)),h-(int)(rate*(50-th))); | 
 |  |  |  | 
 |  |  |         BufferedImage img3 = getImgIO(mpCode); | 
 |  |  |         if(img3!=null){ | 
 |  |  |             g2.setColor(Color.white); | 
 |  |  |             g2.fillOval((int)(w-130*rate), (int)(h-rate*150), (int)(100*rate) , (int)(100*rate)); | 
 |  |  |             g2.drawImage(img3, (int)(w-130*rate), (int)(h-rate*150), (int)(100*rate) , (int)(100*rate), null); | 
 |  |  |         } | 
 |  |  |         InputStream inputStream = bufferedImageToInputStream(imgurl); | 
 |  |  |         return inputStream; | 
 |  |  |     } | 
 |  |  | } |