批量打印带文字的二维码

public class Qrcode4jUtil {

    /**
     * 将字符串生成二维码
     */
    @SuppressWarnings("deprecation")
    public static String createQrcode(String text) {
        String root = ServletActionContext.getServletContext().getRealPath("/systemfile/picture");
        // 从配置文件中读取宽高(二维码宽高相等)
        int width1 = Integer.parseInt(MyProperties.getByKey("width"));
        int width2 = Integer.parseInt(MyProperties.getByKey("width2"));
        String format = "jpg";
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        String newFileName = NewIdUtil.getNewIdUtil().getNewId() + ".jpg";// 给二维码图片命名
        String path = root + "/" + newFileName;
        String path1 = root + "1/" + newFileName;
        String path2 = root + "2/" + newFileName;
        // System.out.println(path);
        BitMatrix bitMatrix;
        BitMatrix bitMatrix1;
        try {
            bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width2, width2, hints);
            bitMatrix1 = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width1, width1, hints);
            File outputFile = new File(path);
            File outputFile1 = new File(path1);
            File outputFile2 = new File(path2);
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);// 大个的二维码
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile2);// 也是大个的二维码
            MatrixToImageWriter.writeToFile(bitMatrix1, format, outputFile1);// 经过缩略的二维码
            return newFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将字符串生成二维码
     */
    @SuppressWarnings("deprecation")
    public static String createQrcode(String text,String root) {
        //String root = ServletActionContext.getServletContext().getRealPath("/systemfile/picture");
        // 从配置文件中读取宽高(二维码宽高相等)
        int width1 = Integer.parseInt(MyProperties.getByKey("width"));
        int width2 = Integer.parseInt(MyProperties.getByKey("width2"));
        String format = "jpg";
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        String newFileName = NewIdUtil.getNewIdUtil().getNewId() + ".jpg";// 给二维码图片命名
        String path = root + "/" + newFileName;
        String path1 = root + "1/" + newFileName;
        String path2 = root + "2/" + newFileName;
        // System.out.println(path);
        BitMatrix bitMatrix;
        BitMatrix bitMatrix1;
        try {
            bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width2, width2, hints);
            bitMatrix1 = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width1, width1, hints);
            File outputFile = new File(path);
            File outputFile1 = new File(path1);
            File outputFile2 = new File(path2);
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);// 大个的二维码
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile2);// 也是大个的二维码
            MatrixToImageWriter.writeToFile(bitMatrix1, format, outputFile1);// 经过缩略的二维码
            return newFileName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
     public static void pressText(String pressText, String newImg, String targetImg, int fontStyle, Color color, int fontSize, int width, int height) {

            //计算文字开始的位置
            //x开始的位置:(图片宽度-字体大小*字的个数)/2
            int startX = (width-(fontSize*pressText.length()))/2;
            //y开始的位置:图片高度-(图片高度-图片宽度)/2
            int startY = height-(height-width)/2;        

            try {
                File file = new File(targetImg);
                Image src = ImageIO.read(file);
                int imageW = src.getWidth(null);
                int imageH = src.getHeight(null);
                BufferedImage image = new BufferedImage(imageW, imageH, BufferedImage.TYPE_INT_RGB);
                Graphics g = image.createGraphics();
                g.drawImage(src, 0, 0, imageW, imageH, null);
                g.setColor(color);
                g.setFont(new Font(null, fontStyle, fontSize));
                g.drawString(pressText, startX, startY);
                g.dispose();

                FileOutputStream out = new FileOutputStream(newImg);
                ImageIO.write(image, "JPEG", out);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                encoder.encode(image);
                out.close();
                System.out.println("image press success");
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    @SuppressWarnings("deprecation")
    public static void main(String[] args) throws Exception {

        Map<String, String> datas = new HashMap<String, String>();
        datas.put("test", "test");
        datas.put("test1", "test1");
        createImgWithText(datas);
    }
    public static void createImgWithText(Map<String, String> datas) throws IOException, WriterException{
//      String text = "你好";
          Iterator<Map.Entry<String, String>> it = datas.entrySet().iterator();
          while (it.hasNext()) {
            Map.Entry<String, String> entry = it.next();
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            int width = 300;
            int height = 300;
            String format = "jpg";
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(entry.getKey(), BarcodeFormat.QR_CODE, width, height, hints);
            File outputFile = new File("D:\\"+entry.getKey()+".jpg");
            MatrixToImageWriter.writeToFile(bitMatrix, format, outputFile);
            int font = 18; //字体大小
            int fontStyle = 1; //字体风格

            //用来存放带有logo+文字的二维码图片
            String newImageWithText = "D:/imageWithText"+entry.getKey()+".jpg"; 
            //带有logo的二维码图片
            String targetImage = "D:/"+entry.getKey()+".jpg";
            //附加在图片上的文字信息
    //        String content = "文字测试一二三四五六123";

            //在二维码下方添加文字(文字居中)
            pressText(entry.getValue(), newImageWithText, targetImage, fontStyle, Color.BLUE, font,  width,  height) ;
        }
    }
}

结果就可以在D盘查看了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Java 中批量打印二维码,可以使用 Zebra 打印机和 ZPL 程序设计语言。以下是一个简单的示例程序,可以将多个二维码一次性打印到 Zebra 打印机上。 ```java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class BatchPrintQR { public static void main(String[] args) { try { // 读取二维码图片文件夹下的所有文件 Path dirPath = Paths.get("C:/qrcodes"); List<Path> files = Files.list(dirPath).toList(); // 打印机连接字符串 String printerConnectionString = "tcp://192.168.1.100:9100"; // 遍历所有文件,生成 ZPL 命令并发送到打印机 for (int i = 0; i < files.size(); i++) { Path filePath = files.get(i); byte[] fileBytes = Files.readAllBytes(filePath); String zplCommand = "^XA^FO50,50^BQN,2,10^FDMA," + i + "^FS^XZ"; ZebraPrinter.sendZplCommand(printerConnectionString, zplCommand, fileBytes); } System.out.println("All QR Codes have been printed!"); } catch (IOException e) { e.printStackTrace(); } } } class ZebraPrinter { public static void sendZplCommand(String printerConnectionString, String zplCommand, byte[] imageData) { try { // 打印机连接 Connection connection = new TcpConnection(printerConnectionString); connection.open(); // 发送 ZPL 命令 connection.write(zplCommand.getBytes()); // 发送图片数据 if (imageData != null) { String imageStartCommand = "^GFA,"; imageStartCommand += imageData.length + ","; imageStartCommand += imageData.length + ","; imageStartCommand += "0,"; // 图片类型,0 表示黑白 imageStartCommand += " "; connection.write(imageStartCommand.getBytes()); connection.write(imageData); } // 断开连接 connection.close(); } catch (ConnectionException e) { e.printStackTrace(); } catch (ZebraPrinterLanguageUnknownException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } } ``` 这个程序假设所有的二维码图片都保存在 C:/qrcodes 文件夹下。程序会读取该文件夹下的所有文件,并将每个文件生成一个 ZPL 命令发送到打印机。ZPL 命令中包含了二维码图片的位置和编号,打印机会将所有的二维码一次性打印出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值