SpringBoot生成条形码的方案详解
目录
- 引言
- 1. 添加zxing的依赖
- 2. 生成条形码
- 3. 调用条形码服务
- 4. 返回条形码
引言
在Spring Boot, Spring Cloud 项目中整合ZXing库来生成条形码在特定行业也是一个常见需求。
ZXing是google开源的一个功能强大的Java库,专门用于二维码/条形码等的生成与解析。它不仅能够生成QR码/条形码,还能解析包括QR码/条形码在内的多种格式.
1. 添加zxing的依赖
<dependency>android <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>
2. 生成条形码
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 { /** php * 生成条形码并保存到指定路径 * @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(); } } }
3. 调用条形码服务
最后,在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
路径下。
4. 返回条形码
如果需要将条形码直接返回,简单修改一下就好。
先修改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(); android 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生成条形码的方案详解的文章就介绍到这了,更多相关SpringBoot生成条形码内容请搜索www.devze.com编程客栈(www.cpwww.devze.compcns.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论