Combine Jaspersoft and springboot to generate one-dimensional code and two-dimensional code

Combine Jaspersoft and springboot to generate one-dimensional code and two-dimensional code

Add dependency on backend

<!-- https://mvnrepository.com/artifact/com.google.zxing/core -->
  <!-- 二维码生成所需依赖 -->
  <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>core</artifactId>
      <version>3.3.3</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
  <dependency>
      <groupId>com.google.zxing</groupId>
      <artifactId>javase</artifactId>
      <version>3.3.3</version>
  </dependency>
  <!--一维码所需依赖-->
  <!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j -->
  <dependency>
      <groupId>net.sf.barcode4j</groupId>
      <artifactId>barcode4j</artifactId>
      <version>2.1</version>
  </dependency>
 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

Note: After downloading these two packages, the jaspersoft Studio tool also needs to import these two dependent related packages to prepare for generating the QR code.

Insert picture description here

One-dimensional code

The one-dimensional code was very smooth when I was doing it, but the two-dimensional code encountered some difficulties.
One-dimensional code can directly select code128 in Barcode, as shown in 1.
Insert picture description here
Then bind with the fields corresponding to fields as shown in the figure below:
Insert picture description here

QR code

When doing the QR code here, I always wanted to use the QRcode in the second figure above to do it, but when I used it, a null pointer exception was reported. I tried many solutions but failed to solve it. Later, it was discovered that you can also directly insert an empty picture to display the QR code. This uses the jar package in the first figure.
1. First drag the Image component to select as shown in the figure below:
Insert picture description here2. The picture is bound to the relevant fields in the fields, which is different from the one-dimensional code. The binding code is as follows:

com.google.zxing.client.j2se.MatrixToImageWriter.toBufferedImage( 
 new com.google.zxing.qrcode.QRCodeWriter().encode(        
   $F{
    
    qrCode},         
 com.google.zxing.BarcodeFormat.QR_CODE, 300, 300))
 
  
  
  • 1
  • 2
  • 3

Just replace $F{qrCode} with your corresponding bound field. This code is very important. It must be bound to the Image component and not QRCode.
Finally, poject-clean then copy the jasper file to the back-end corresponding folder, and the operation can be displayed successfully.

Record the detours you have traveled. If you have any shortcomings, please advise.

Guess you like

Origin blog.csdn.net/s_156/article/details/114135865