0

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

聚甲醛

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

问题

该代码只是barbecue使用pom.xml.

当我删除/注释此行时final ConfigurableApplicationContext context = SpringApplication.run(App.class, args);,条形码中的文本如下图所示打印

带文字的条码

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

没有文字的条码

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

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

4

1 回答 1

0

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

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

于 2020-12-23T05:17:10.813 回答