| package com.doumee.core.utils; | 
|   | 
| import com.google.zxing.common.BitMatrix; | 
| import org.apache.commons.fileupload.FileItem; | 
| import org.apache.commons.fileupload.FileItemFactory; | 
| import org.apache.commons.fileupload.disk.DiskFileItemFactory; | 
| import org.springframework.http.MediaType; | 
| import org.springframework.web.multipart.MultipartFile; | 
| import org.springframework.web.multipart.commons.CommonsMultipartFile; | 
|   | 
| import javax.imageio.ImageIO; | 
| import javax.imageio.stream.ImageOutputStream; | 
| import java.awt.*; | 
| import java.awt.geom.Ellipse2D; | 
| import java.awt.image.BufferedImage; | 
| import java.io.*; | 
| import java.math.BigDecimal; | 
| import java.math.RoundingMode; | 
| import java.net.URL; | 
|   | 
| public class GeneratePicUtil { | 
|   | 
|     static String bg="https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230404/SysBaseBackGround/D56231512C0B4A4C9D06B02F9566B0B0.png"; | 
|   | 
|     static String headUrl = "https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230404/SysBaseBackGround/B802B64FE31447DCB030F13ECC7CA9F9.png"; | 
|   | 
|     private static float jPEGcompression = 0.75f;// 图片清晰比率 | 
|     /** | 
|      * test | 
|      * | 
|      * @param args | 
|      * @throws | 
|      */ | 
|     public static void main(String[] args) { | 
|         long starttime = System.currentTimeMillis(); | 
|         System.out.println("开始:" + starttime); | 
|         try { | 
| //            String a="https://shoeslxkj.oss-cn-beijing.aliyuncs.com/file/20230403/SysBaseBackGround/883D63E108F44FC68ED856B42B9AB063.png"; | 
| //            generateImg(a,"D:/","baidu.com","清澄白河马克杯-节日版定制日版定制", BigDecimal.valueOf(129.88) ); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|         System.out.println("结束:" + (System.currentTimeMillis() - starttime) / 1000); | 
|     } | 
|   | 
|     private static final int width = 898; | 
|     private static final int height = 1308; | 
|   | 
|     /** | 
|      * 生成分享图片 | 
|      * @param headImg 头部图片 | 
|      * @param wxQrCodeImg 微信小程序二维码 | 
|      * @param goodsName 商品名称 | 
|      * @param price  商品价格 | 
|      * @return | 
|      * @throws Exception | 
|      */ | 
|     public static InputStream generateImg(String headImg,String wxQrCodeImg, String goodsName, BigDecimal price) throws Exception { | 
|         BufferedImage bi =  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); | 
|         //得到它的绘制环境(这张图片的笔) | 
|         Graphics2D g2 = (Graphics2D) bi.getGraphics(); | 
|         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); | 
|         g2.fillRect(0, 0, width, height); | 
|         //背景图片 | 
|         BufferedImage titles = ImageIO.read(new URL(bg)); | 
|         g2.drawImage(titles, 1, 1, 894, 1305, null); | 
|         g2.drawRect(0, 0, width - 1, height - 1); | 
|         //头部图片 | 
|         BufferedImage headBg = ImageIO.read(new URL(headImg)); | 
|         g2.drawImage(headBg, 1, 1, 894, 900, null); | 
|         g2.drawRect(0, 0, width - 1, height - 1); | 
|   | 
|         //商品名称 | 
|         if(goodsName.length()>13){ | 
|             g2.setColor(Color.black); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(goodsName.substring(0,13), 46, 1000); | 
|             g2.setColor(Color.black); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(goodsName.substring(13), 46, 1054); | 
|         }else{ | 
|             g2.setColor(Color.black); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(goodsName, 46, 1020); | 
|         } | 
|   | 
|         //商品价格 | 
|         g2.setColor(Color.red); | 
|         g2.setFont(new Font("黑体", Font.PLAIN, 30)); | 
|         g2.drawString("¥", 620, 1020); | 
|         BigDecimal integerPart = price.setScale(0, RoundingMode.DOWN); | 
|         BigDecimal fractionalPart  = price.subtract(integerPart); | 
|         if(price.compareTo(new BigDecimal(1000))>=0){ | 
|             //整数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020); | 
|             //小数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 30)); | 
|             g2.drawString(fractionalPart.toString().substring(1), 740, 1020); | 
|         }else if(price.compareTo(new BigDecimal(100))>=0){ | 
|             //整数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020); | 
|             //小数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 30)); | 
|             g2.drawString(fractionalPart.toString().substring(1), 720, 1020); | 
|         }else if(price.compareTo(new BigDecimal(10))>=0){ | 
|             //整数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020); | 
|             //小数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 30)); | 
|             g2.drawString(fractionalPart.toString().substring(1), 700, 1020); | 
|         }else{ | 
|             //整数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 40)); | 
|             g2.drawString(price.setScale(0, RoundingMode.DOWN).toString(), 660, 1020); | 
|             //小数部分 | 
|             g2.setColor(Color.red); | 
|             g2.setFont(new Font("黑体", Font.PLAIN, 30)); | 
|             g2.drawString(fractionalPart.toString().substring(1), 680, 1020); | 
|         } | 
|         //设置二维码图片 | 
|         BufferedImage barcodeCode = ImageIO.read(new URL(wxQrCodeImg)); | 
|         g2.drawImage(barcodeCode, 670, 1100, 180, 180,null); | 
|         g2.drawRect(0, 0, width - 1, height - 1); | 
|         InputStream inputStream = bufferedImageToInputStream(bi); | 
|         return inputStream; | 
| //        String fileName = "8701EAC36DEC405A94C530F0E6796589.jpg"; //ID.nextGUID() + ".jpg"; | 
| //        GeneratePicUtil.saveFile(inputStream, filePath+ File.separator+fileName); | 
| //        return filePath + File.separator+ fileName; | 
|     } | 
|   | 
|     public  static MultipartFile getMultipartFile(InputStream inputStream, String fileName) { | 
|         FileItem fileItem = createFileItem(inputStream, fileName); | 
|         return new CommonsMultipartFile(fileItem); | 
|     } | 
|   | 
|     /** | 
|      * FileItem类对象创建 | 
|      * | 
|      * @param inputStream inputStream | 
|      * @param fileName    fileName | 
|      * @return FileItem | 
|      */ | 
|     public static FileItem createFileItem(InputStream inputStream, String fileName) { | 
|         FileItemFactory factory = new DiskFileItemFactory(16, null); | 
|         String textFieldName = "file"; | 
|         FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName); | 
|         int bytesRead = 0; | 
|         byte[] buffer = new byte[8192]; | 
|         OutputStream os = null; | 
|         //使用输出流输出输入流的字节 | 
|         try { | 
|             os = item.getOutputStream(); | 
|             while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) { | 
|                 os.write(buffer, 0, bytesRead); | 
|             } | 
|             inputStream.close(); | 
|         } catch (IOException e) { | 
|             throw new IllegalArgumentException("文件上传失败"); | 
|         } finally { | 
|             if (os != null) { | 
|                 try { | 
|                     os.close(); | 
|                 } catch (IOException e) { | 
|                     e.printStackTrace();; | 
|                 } | 
|             } | 
|             if (inputStream != null) { | 
|                 try { | 
|                     inputStream.close(); | 
|                 } catch (IOException e) { | 
|                     e.printStackTrace();; | 
|                 } | 
|             } | 
|         } | 
|   | 
|         return item; | 
|     } | 
|   | 
|     public static void saveFile(InputStream is, String fileName) throws IOException { | 
|         try (BufferedInputStream in = new BufferedInputStream(is); | 
|              BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) { | 
|             int len; | 
|             byte[] b = new byte[1024]; | 
|             while ((len = in.read(b)) != -1) { | 
|                 out.write(b, 0, len); | 
|             } | 
|         } | 
|     } | 
|   | 
|     public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException { | 
|         return bufferedImageToInputStream(backgroundImage, "jpg"); | 
|     } | 
|   | 
|     /** | 
|      * backgroundImage 转换为输出流 | 
|      * | 
|      * @param backgroundImage | 
|      * @param format | 
|      * @return | 
|      * @throws IOException | 
|      */ | 
|     public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException { | 
|         ByteArrayOutputStream bs = new ByteArrayOutputStream(); | 
|         try ( | 
|                 ImageOutputStream | 
|                         imOut = ImageIO.createImageOutputStream(bs)) { | 
|             ImageIO.write(backgroundImage, format, imOut); | 
|             InputStream is = new ByteArrayInputStream(bs.toByteArray()); | 
|             return is; | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 传入的图像必须是正方形的 才会 圆形 如果是长方形的比例则会变成椭圆的 | 
|      * | 
|      * 用户头像地址 | 
|      * @return | 
|      * @throws IOException | 
|      */ | 
|     public static BufferedImage convertCircular(BufferedImage bi1) throws IOException { | 
|   | 
|         BufferedImage bi2 = new BufferedImage(bi1.getWidth(), bi1.getHeight(), BufferedImage.TYPE_4BYTE_ABGR); | 
|   | 
|         Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, bi1.getWidth(), bi1.getHeight()); | 
|   | 
|         Graphics2D g2 = bi2.createGraphics(); | 
|   | 
|         g2.setClip(shape); | 
|   | 
|         // 使用 setRenderingHint 设置抗锯齿 | 
|   | 
|         g2.drawImage(bi1, 0, 0, null); | 
|   | 
|         // 设置颜色 | 
|   | 
|         g2.setBackground(Color.green); | 
|   | 
|         g2.dispose(); | 
|   | 
|         return bi2; | 
|   | 
|     } | 
|   | 
|   | 
|     //去除二维码白边 | 
|     public static BitMatrix deleteWhite(BitMatrix matrix) { | 
|         int[] rec = matrix.getEnclosingRectangle(); | 
|         int resWidth = rec[2] + 1; | 
|         int resHeight = rec[3] + 1; | 
|   | 
|         BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); | 
|         resMatrix.clear(); | 
|         for (int i = 0; i < resWidth; i++) { | 
|             for (int j = 0; j < resHeight; j++) { | 
|                 if (matrix.get(i + rec[0], j + rec[1])) resMatrix.set(i, j); | 
|             } | 
|         } | 
|         return resMatrix; | 
|     } | 
|   | 
|   | 
|   | 
|   | 
| } |