在Spring Boot, Spring Cloud 项目中整合ZXing库来生成条形码在特定行业也是一个常见需求。
ZXing是google开源的一个功能强大的Java库,专门用于二维码/条形码等的生成与解析。它不仅能够生成QR码/条形码,还能解析包括QR码/条形码在内的多种格式.
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.5.2</version>
</dependency>
import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
@Service
public class BarcodeService {
/**
* 生成条形码并保存到指定路径
* @param content 条形码内容
* @param path 保存路径
* @param width 条形码宽度
* @param height 条形码高度
*/
public void generateBarcodeImage(String content, String path, int width, int height) {
Code128Writer barcodeWriter = new Code128Writer();
BitMatrix bitMatrix = null;
try {
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height);
Path filePath = FileSystems.getDefault().getPath(path);
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", filePath);
} catch (WriterException | IOException e) {
e.printStackTrace();
}
}
}
最后,在Spring Boot的中调用这个服务生成条形码:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BarcodeController {
@Autowired
private BarcodeService barcodeService;
@GetMapping("/generate-barcode")
public String generateBarcode(@RequestParam String content, @RequestParam String path, @RequestParam int width, @RequestParam int height) {
barcodeService.generateBarcodeImage(content, path, width, height);
return "Barcode generated successfully at " + path;
}
}
现在,当你访问/generate-barcode
端点并传递相应的参数时,它将生成一个条形码并将其保存到指定的路径。例如:
http://localhost:8080/generate-barcode?content=123456789&path=/path/to/barcode.png&width=300&height=100
这将生成一个内容为123456789
、宽度为300像素、高度为100像素的条形码,并将其保存到/path/to/barcode.png
路径下。
如果需要将条形码直接返回,简单修改一下就好。
先修改BarcodeService
以返回一个byte[]
数组:
import com.google.zxing.*;
import com.google.zxing.client.j2se.MatrixToImageConfig;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.oned.Code128Writer;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@Service
public class BarcodeService {
/**
* 生成条形码的字节数据
* @param content 条形码内容
* @param width 条形码宽度
* @param height 条形码高度
* @return 条形码的字节数据
*/
public byte[] generateBarcodeImage(String content, int width, int height) {
Code128Writer barcodeWriter = new Code128Writer();
BitMatrix bitMatrix = null;
try {
bitMatrix = barcodeWriter.encode(content, BarcodeFormat.CODE_128, width, height);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
MatrixToImageConfig config = new MatrixToImageConfig(MatrixToImageConfig.BLACK, MatrixToImageConfig.WHITE);
MatrixToImageWriter.writeToStream(bitMatrix, "PNG", outputStream, config);
return outputStream.toByteArray();
} catch (WriterException | IOException e) {
e.printStackTrace();
return null;
}
}
}
然后,修改BarcodeController
以返回图片的字节数据:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BarcodeController {
@Autowired
private BarcodeService barcodeService;
@GetMapping("/generate-barcode")
public ResponseEntity<byte[]> generateBarcode(@RequestParam String content, @RequestParam int width, @RequestParam int height) {
byte[] barcodeImage = barcodeService.generateBarcodeImage(content, width, height);
if (barcodeImage != null) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<>(barcodeImage, headers, HttpStatus.OK);
} else {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
现在,当你在Spring Boot, Spring Cloud 项目中访问/generate-barcode
端点并传递相应的参数时,它将生成一个条形码并将其作为PNG图片的字节数据返回。
详情参见 : SpringBoot中生成二维码图片的方案