wanfa99 пре 2 година
родитељ
комит
2b942c139f

+ 41 - 3
jeecg-boot-module-system/src/main/java/org/jeecg/modules/openApi/service/impl/Rdrecord11ServiceImpl.java

@@ -10,6 +10,9 @@ import org.jeecg.modules.system.util.oConvertUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
 import java.io.File;
 import java.math.BigDecimal;
 import java.util.ArrayList;
@@ -89,14 +92,49 @@ public class Rdrecord11ServiceImpl  implements IRdrecord11Service {
                 sum=sum+num;
                 for(int j=0;j<num;j++){
                     serialNum=serialNum+1;
-                    onejson.put("计数","第"+i+1+"行,"+(j+1)+"/"+num);
                     //二维码位置拼名称
                     String newFilePath=finalPath+"/"+serialNum+".png";
                     pathList.add(newFilePath);
-                    //生成二维码
-                    oConvertUtils.generateQRCodeImage(onejson.toString(),200,200,newFilePath);
+                    //生成二维码图片
+                    BufferedImage bufferedImage=oConvertUtils.getBufferedImage(onejson.toJSONString(),200,200);
+                    //背景图片
+                    BufferedImage backImage = new BufferedImage(400, 400,BufferedImage.TYPE_INT_RGB);
+                    //显示文字 缸号,委外订单号,匹数,物料,描述,关键属性,供应商,接收方
+
+                    //绘制合成
+                    Graphics2D backGraphics=backImage.createGraphics();
+                    backGraphics.setClip(0, 0, 400, 400);//设置绘画的区域,默认背景是黑色,也叫做获取画笔,指定画笔可画的范围
+                    backGraphics.setColor(Color.white);//设置画笔颜色
+                    backGraphics.fillRect(0, 0, 400, 400);//画一个矩形块,颜色是白色,因为画笔颜色是白色
+
+                    //放文字
+                    backGraphics.setColor(Color.black);
+                    int bTextSize=15;
+                    Font font=new Font("宋体",Font.BOLD,bTextSize);
+                    backGraphics.setFont(font);
+                    backGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+
+                    backGraphics.drawString("缸号:"+onejson.getString("code"),0,bTextSize*0);
+                    backGraphics.drawString("委外订单号:"+onejson.getString("comcode"),0,(bTextSize+5)*1);
+                    backGraphics.drawString("匹数:"+onejson.getString("cdefine26"),0,(bTextSize+5)*2);
+                    backGraphics.drawString("物料:"+onejson.getString("cInvName"),0,(bTextSize+5)*3);
+                    backGraphics.drawString("描述:"+onejson.getString("text"),0,(bTextSize+5)*4);
+                    backGraphics.drawString("关键属性:",0,(bTextSize+5)*5);
+                    backGraphics.drawString("供应商:"+onejson.getString("cVenName"),0,(bTextSize+5)*6);
+                    backGraphics.drawString("接收方:"+onejson.getString("cVenCode"),0,(bTextSize+5)*7);
+
+                    //放图片
+                    backGraphics.drawImage(bufferedImage, 0,180, bufferedImage.getWidth(), bufferedImage.getHeight(),null);
+
+                    backGraphics.dispose();// 释放对象
+
+                    File outputfile = new File(newFilePath);
+                    ImageIO.write(backImage, "PNG", outputfile);
                 }
             }
+            //图片全部生成后打印
+            oConvertUtils.print(pathList,"test");
+
             resultJson.put("b",JsonChangeUtils.toJSONObject(result));
         }else if(org.equals("102")){
 

+ 41 - 12
jeecg-boot-module-system/src/main/java/org/jeecg/modules/system/util/oConvertUtils.java

@@ -10,21 +10,20 @@ import org.apache.shiro.SecurityUtils;
 import org.jeecg.modules.system.entity.SysUser;
 
 import javax.imageio.ImageIO;
+import javax.print.*;
+import javax.print.attribute.HashPrintRequestAttributeSet;
+import javax.print.attribute.PrintRequestAttributeSet;
 import javax.servlet.http.HttpServletRequest;
 import java.awt.*;
 import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.IOException;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.net.*;
-import java.nio.file.FileSystems;
-import java.nio.file.Path;
 import java.sql.Date;
 import java.text.SimpleDateFormat;
-import java.util.*;
 import java.util.List;
+import java.util.*;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
@@ -646,23 +645,24 @@ public class oConvertUtils {
     }
 
     /***
-     * 字段信息生成二维码图片
+     * 字段信息生成二维码
      * @param text
      * @param width
      * @param height
-     * @param filePath
      * @throws WriterException
      * @throws IOException
      */
-    public static void generateQRCodeImage(String text, int width, int height, String filePath) throws WriterException, IOException {
+    public static BufferedImage getBufferedImage(String text, int width, int height) throws WriterException, IOException {
         QRCodeWriter qrCodeWriter = new QRCodeWriter();
         HashMap<EncodeHintType, Object> hints = new HashMap<>();
         hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
         BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
+        BufferedImage bufferedImage=MatrixToImageWriter.toBufferedImage(bitMatrix);
+        return bufferedImage;
 
-        Path path = FileSystems.getDefault().getPath(filePath);
-
-        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
+        //生成文件至指定位置 filePath生成文件位置
+//        Path path = FileSystems.getDefault().getPath(filePath);
+//        MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
 
     }
 
@@ -701,4 +701,33 @@ public class oConvertUtils {
         ImageIO.write(bufferedImage, "PNG", outputfile);
         return bufferedImage;
     }
+
+    /***
+     * 打印
+     * @param filePath
+     * @param printName
+     * @return
+     */
+    public static void print(List<String> filePath,String printName) throws FileNotFoundException, PrintException {
+        PrintService myPrintService=null;
+        PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
+        for (PrintService printService : printServices) {
+            if (printService.getName().trim().equals(printName)) {
+                myPrintService=myPrintService;
+            }
+        }
+        myPrintService=printServices[0];
+        // 设置打印格式,如果未确定类型,可选择autosense
+        DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
+        // 设置打印参数
+        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
+
+        for (int i=0;i<filePath.size();i++){
+            File file=new File(filePath.get(0));
+            FileInputStream fis = new FileInputStream(file); // 构造待打印的文件流
+            Doc doc = new SimpleDoc(fis, flavor, null);
+            DocPrintJob job = myPrintService.createPrintJob(); // 创建打印作业
+            job.print(doc, aset);
+        }
+    }
 }