最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot生成條形碼的方案詳解

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

引言

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

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

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 條形碼內(nèi)容
     * @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. 調(diào)用條形碼服務(wù)

最后,在Spring Boot的中調(diào)用這個服務(wù)生成條形碼:

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

現(xiàn)在,當(dāng)你訪問/generate-barcode端點并傳遞相應(yīng)的參數(shù)時,它將生成一個條形碼并將其保存到指定的路徑。例如:

http://localhost:8080/generate-barcode?content=123456789&path=/path/to/barcode.png&width=300&height=100

這將生成一個內(nèi)容為123456789、寬度為300像素、高度為100像素的條形碼,并將其保存到/path/to/barcode.png路徑下。

4. 返回條形碼

如果需要將條形碼直接返回,簡單修改一下就好。

先修改BarcodeService以返回一個byte[]數(shù)組:

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é)數(shù)據(jù)
     * @param content 條形碼內(nèi)容
     * @param width 條形碼寬度
     * @param height 條形碼高度
     * @return 條形碼的字節(jié)數(shù)據(jù)
     */
    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é)數(shù)據(jù):

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);
        }
    }
}

現(xiàn)在,當(dāng)你在Spring Boot, Spring Cloud 項目中訪問/generate-barcode端點并傳遞相應(yīng)的參數(shù)時,它將生成一個條形碼并將其作為PNG圖片的字節(jié)數(shù)據(jù)返回。

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

相關(guān)文章

  • Java中Date時間類的使用方法舉例

    Java中Date時間類的使用方法舉例

    這篇文章主要給大家介紹了關(guān)于Java中Date時間類的使用方法,在java開發(fā)中,很多字段是Date類型的,文中通過代碼示例將Date時間類使用的方法介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • java實現(xiàn)鏈表反轉(zhuǎn)

    java實現(xiàn)鏈表反轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)鏈表反轉(zhuǎn),分別通過迭代法、遞歸法實現(xiàn)java鏈表反轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Spring Boot 使用 logback、logstash、ELK 記錄日志文件的方法

    Spring Boot 使用 logback、logstash、ELK 記錄日志文件的方法

    這篇文章主要介紹了Spring Boot 使用 logback、logstash、ELK 記錄日志文件的思路詳解,文中給大家提到了logback 取代 log4j的理由,需要的朋友可以參考下
    2017-12-12
  • MyBatis-Plus通過version機(jī)制實現(xiàn)樂觀鎖的思路

    MyBatis-Plus通過version機(jī)制實現(xiàn)樂觀鎖的思路

    version機(jī)制的核心思想就是,假設(shè)發(fā)生并發(fā)沖突的幾率很低,只有當(dāng)更新數(shù)據(jù)的時候采取檢查是否有沖突,而判斷是否有沖突的依據(jù)就是version的值是否被改變了,這篇文章主要介紹了MyBatis-Plus通過version機(jī)制實現(xiàn)樂觀鎖的思路,需要的朋友可以參考下
    2021-09-09
  • SpringBoot中如何統(tǒng)一接口返回與全局異常處理詳解

    SpringBoot中如何統(tǒng)一接口返回與全局異常處理詳解

    全局異常處理是個比較重要的功能,一般在項目里都會用到,這篇文章主要給大家介紹了關(guān)于SpringBoot中如何統(tǒng)一接口返回與全局異常處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-09-09
  • JavaWeb中的Filter過濾器解讀

    JavaWeb中的Filter過濾器解讀

    這篇文章主要介紹了JavaWeb中的Filter過濾器解讀,Filter過濾器是JavaWeb的三大組件之一,Filter過濾器是JavaEE的規(guī)范也就是接口,Filter的作用是攔截請求,過濾響應(yīng),需要的朋友可以參考下
    2023-10-10
  • Java工具之ja-netfilter?2022.1?配置教程

    Java工具之ja-netfilter?2022.1?配置教程

    這篇文章主要介紹了Java工具之ja-netfilter?2022.1?配置教程,本防火墻基于javaagent,所以目前只有基于java的程序能夠使用,需要的朋友可以參考下
    2022-04-04
  • 你真的會使用Java的方法引用嗎

    你真的會使用Java的方法引用嗎

    這篇文章主要給大家介紹了關(guān)于Java方法引用的相關(guān)資料,方法引用是Java8的新特性,方法引用其實也離不開Lambda表達(dá)式,本文通過示例代碼介紹的很詳細(xì),需要的朋友可以參考下
    2021-08-08
  • SpringAOP中的動態(tài)代理技術(shù)深入解析

    SpringAOP中的動態(tài)代理技術(shù)深入解析

    這篇文章主要介紹了SpringAOP中的動態(tài)代理技術(shù)深入解析,spring默認(rèn)使用JDK動態(tài)代理實現(xiàn)AOP,類如果實現(xiàn)了接口,spring就會用JDK動態(tài)代理實現(xiàn)AOP,如果目標(biāo)類沒有實現(xiàn)接口,spring則使用Cglib動態(tài)代理來實現(xiàn)AOP,需要的朋友可以參考下
    2024-01-01
  • 深入淺出的講解Java關(guān)鍵字final的作用

    深入淺出的講解Java關(guān)鍵字final的作用

    final是Java中非常常見的一個關(guān)鍵字,可以說每天都在使用它,雖然常見,但卻也不見得都那么顯而易見,今天就來研究一下final,以加深對它的理解和更合理的運(yùn)用,需要的朋友可以參考下
    2023-06-06

最新評論

紫金县| 高阳县| 葫芦岛市| 桑植县| 杭州市| 元朗区| 剑河县| 常州市| 舞钢市| 聊城市| 拉萨市| 邹城市| 弥勒县| 顺义区| 奉节县| 璧山县| 张家界市| 清苑县| 大埔区| 定安县| 弥渡县| 巴青县| 腾冲县| 大邑县| 克东县| 格尔木市| 伊宁市| 济源市| 论坛| 察雅县| 东至县| 内丘县| 嘉鱼县| 海城市| 九龙坡区| 湖北省| 黄平县| 大连市| 绍兴县| 阳朔县| 镇坪县|