我在这里有一个非常令人费解的场景。我先贴代码,后面解释
聚甲醛
<?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
应用程序和在条形码上打印文本之间的关系感到困惑。