0

I have a very puzzling scenario here. Let me paste the code first and explain later

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>

CODE

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

The Problem

The code simply creates a barcode image using the barbecue library provided in the pom.xml.

When I remove/comment this line final ConfigurableApplicationContext context = SpringApplication.run(App.class, args);, the text in the barcode is printed as shown in the image below

Barcode with text

But when I leave it there, the text is not printed in the barcode as shown below

Barcode without text

What I am trying to achieve is provide a controller where a barcode image can be generated from text.

I am just puzzled on the relation between initializing a Spring Boot application and printing text on a barcode.

Kihats
  • 3,326
  • 5
  • 31
  • 46

1 Answers1

0

This github project fixes the issue. Pull the project and manually install the .jar file in your project with the following command:

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

Kihats
  • 3,326
  • 5
  • 31
  • 46