【问题标题】:Spring Boot and Barbecue Barcode Library - Text not printedSpring Boot 和烧烤条形码库 - 未打印文本
【发布时间】:2020-12-23 13:17:10
【问题描述】:

我在这里有一个非常令人费解的场景。我先贴代码,稍后解释

POM

<?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); 时,条形码中的文本如下图所示打印

但是当我把它放在那里时,文本没有打印在条形码中,如下所示

我想要实现的是提供一个控制器,可以从文本中生成条形码图像。

我只是对初始化Spring Boot 应用程序和在条形码上打印文本之间的关系感到困惑。

【问题讨论】:

    标签: spring-boot barcode barbecue


    【解决方案1】:

    这个github 项目解决了这个问题。拉取项目并使用以下命令在项目中手动安装 .jar 文件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多