簡體   English   中英

Spring 引導和燒烤條形碼庫 - 未打印文本

[英]Spring Boot and Barbecue Barcode Library - Text not printed

我在這里有一個非常令人費解的場景。 我先貼代碼,后面解釋

聚甲醛

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>DeleteMe</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>15</maven.compiler.source>
    <maven.compiler.target>15</maven.compiler.target>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.5.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>net.sourceforge.barbecue</groupId>
        <artifactId>barbecue</artifactId>
        <version>1.5-beta1</version>
    </dependency>
</dependencies>

代碼

import net.sourceforge.barbecue.*;
import net.sourceforge.barbecue.output.OutputException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;    

@SpringBootApplication
public class App {
    public static void main(String[] args) throws BarcodeException, OutputException, IOException {
        final ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
        String barcodeText = "MSTR-23829X-9";
        final var barbecue = barbecue(barcodeText);

        System.out.println("Writing image");

        writeImage(barbecue, barcodeText);
    }

    public static void writeImage(BufferedImage bufferedImage, String barcodeText) throws IOException {
        File outputfile = new File(MessageFormat.format("{0}.png", barcodeText));
        ImageIO.write(bufferedImage, "png", outputfile);
    }

    public static BufferedImage barbecue(String barcodeText) throws BarcodeException, OutputException {
        Barcode barcode = BarcodeFactory.createCode128(barcodeText);
        barcode.setBarHeight(100);
        barcode.setResolution(200);

        Font font = new Font("Monospaced", Font.PLAIN, 20);
        barcode.setFont(font);

        return BarcodeImageHandler.getImage(barcode);
    }
}

問題

該代碼只是使用pom.xml中提供的barbecue庫創建一個條形碼圖像。

當我刪除/評論這一行final ConfigurableApplicationContext context = SpringApplication.run(App.class, args); ,條碼中的文字打印如下圖所示

帶文字的條碼

但是當我把它留在那里時,文本沒有打印在條形碼中,如下所示

沒有文字的條碼

我想要實現的是提供一個 controller ,其中可以從文本生成條形碼圖像。

我只是對初始化Spring Boot應用程序和在條形碼上打印文本之間的關系感到困惑。

這個github項目解決了這個問題。 拉取工程,在工程中手動安裝.jar文件,命令如下:

mvn install:install-file -Dfile=barbecue-1.5-beta1.jar -DgroupId=local -DartifactId=barbecue -Dversion=1.5-beta1-linux -Dpackaging=jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM