亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot生成條形碼的方案詳解

 更新時間:2024年08月13日 08:53:43   作者:碼到三十五  
在Spring Boot, Spring Cloud 項目中整合ZXing庫來生成條形碼在特定行業(yè)也是一個常見需求,ZXing是google開源的一個功能強大的Java庫,專門用于二維碼/條形碼等的生成與解析,所以本文給大家介紹了SpringBoot生成條形碼的方案,需要的朋友可以參考下

引言

在Spring Boot, Spring Cloud 項目中整合ZXing庫來生成條形碼在特定行業(yè)也是一個常見需求。

ZXing是google開源的一個功能強大的Java庫,專門用于二維碼/條形碼等的生成與解析。它不僅能夠生成QR碼/條形碼,還能解析包括QR碼/條形碼在內的多種格式.

1. 添加zxing的依賴

<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>

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 {

    /**
     * 生成條形碼并保存到指定路徑
     * @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 {

    /**
     * 生成條形碼的字節(jié)數據
     * @param content 條形碼內容
     * @param width 條形碼寬度
     * @param height 條形碼高度
     * @return 條形碼的字節(jié)數據
     */
    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以返回圖片的字節(jié)數據:

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圖片的字節(jié)數據返回。

到此這篇關于SpringBoot生成條形碼的方案詳解的文章就介紹到這了,更多相關SpringBoot生成條形碼內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 使用Prometheus+Grafana的方法監(jiān)控Springboot應用教程詳解

    使用Prometheus+Grafana的方法監(jiān)控Springboot應用教程詳解

    這篇文章主要介紹了用Prometheus+Grafana的方法監(jiān)控Springboot應用,本文通過實例代碼詳解給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 關于SpringBoot的@ConfigurationProperties注解和松散綁定、數據校驗

    關于SpringBoot的@ConfigurationProperties注解和松散綁定、數據校驗

    這篇文章主要介紹了關于SpringBoot的@ConfigurationProperties注解和松散綁定、數據校驗,@ConfigurationProperties主要作用就是將prefix屬性指定的前綴配置項的值綁定到這個JavaBean上?,通過指定的前綴,來綁定配置文件中的配置,需要的朋友可以參考下
    2023-05-05
  • spring Mvc配置xml使ResponseBody返回Json的方法示例

    spring Mvc配置xml使ResponseBody返回Json的方法示例

    這篇文章主要給大家介紹了關于spring Mvc配置xml使ResponseBody返回Json的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2018-04-04
  • 淺談Strut2如何對請求參數的封裝

    淺談Strut2如何對請求參數的封裝

    這篇文章主要介紹了淺談Strut2如何對請求參數的封裝,具有一定借鑒價值,需要的朋友可以參考下
    2017-12-12
  • 使用Spring組合自定義的注釋 mscharhag操作

    使用Spring組合自定義的注釋 mscharhag操作

    這篇文章主要介紹了使用Spring組合自定義的注釋 mscharhag,本文通過實例代碼給大家介紹的非常詳細,對大家的工作或學習有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • springboot使用注解獲取yml配置的兩種方法

    springboot使用注解獲取yml配置的兩種方法

    本文主要介紹了springboot使用注解獲取yml配置的兩種方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-09-09
  • IDEA導入外部項目報Error:java: 無效的目標發(fā)行版: 11的解決方法

    IDEA導入外部項目報Error:java: 無效的目標發(fā)行版: 11的解決方法

    這篇文章主要介紹了IDEA導入外部項目報Error:java: 無效的目標發(fā)行版: 11,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Java中關于char類型變量能夠輸出中文的問題

    Java中關于char類型變量能夠輸出中文的問題

    這篇文章主要介紹了Java中關于char類型變量能夠輸出中文的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Java視頻斷點上傳的實現示例

    Java視頻斷點上傳的實現示例

    斷點續(xù)傳指的是在下載或上傳時,將下載或上傳任務人為的劃分為幾個部分,本文主要介紹了Java視頻斷點上傳的實現示例,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • 解決mybatisplus MetaObjectHandler 失效的問題

    解決mybatisplus MetaObjectHandler 失效的問題

    本文主要介紹了解決mybatisplus MetaObjectHandler 失效的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-02-02

最新評論