[BAEL-3390] - Overview of generating barcodes and QR codes in Java (#8418)
Co-authored-by: ashleyfrieze <ashley@incredible.org.uk>
This commit is contained in:
parent
57d4ba8a19
commit
e9e36b35d2
|
@ -8,8 +8,9 @@
|
|||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -23,16 +24,63 @@
|
|||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sourceforge.barbecue</groupId>
|
||||
<artifactId>barbecue</artifactId>
|
||||
<version>${barbecue.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.barcode4j</groupId>
|
||||
<artifactId>barcode4j</artifactId>
|
||||
<version>${barcode4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.zxing</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>${zxing.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.zxing</groupId>
|
||||
<artifactId>javase</artifactId>
|
||||
<version>${zxing.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.kenglxn.qrgen</groupId>
|
||||
<artifactId>javase</artifactId>
|
||||
<version>${qrgen.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.cactoos</groupId>
|
||||
<artifactId>cactoos</artifactId>
|
||||
<version>${cactoos.version}</version>
|
||||
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jitpack.io</id>
|
||||
<url>https://jitpack.io</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<jcommander.version>1.78</jcommander.version>
|
||||
<lombok.version>1.18.6</lombok.version>
|
||||
<barbecue.version>1.5-beta1</barbecue.version>
|
||||
<barcode4j.version>2.1</barcode4j.version>
|
||||
<zxing.version>3.3.0</zxing.version>
|
||||
<qrgen.version>2.6.0</qrgen.version>
|
||||
|
||||
<cactoos.version>0.43</cactoos.version>
|
||||
</properties>
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,99 @@
|
|||
package com.baeldung.barcodes;
|
||||
|
||||
import com.baeldung.barcodes.generators.BarbecueBarcodeGenerator;
|
||||
import com.baeldung.barcodes.generators.Barcode4jBarcodeGenerator;
|
||||
import com.baeldung.barcodes.generators.QRGenBarcodeGenerator;
|
||||
import com.baeldung.barcodes.generators.ZxingBarcodeGenerator;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/barcodes")
|
||||
public class BarcodesController {
|
||||
|
||||
//Barbecue library
|
||||
|
||||
@GetMapping(value = "/barbecue/upca/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barbecueUPCABarcode(@PathVariable("barcode") String barcode) throws Exception {
|
||||
return okResponse(BarbecueBarcodeGenerator.generateUPCABarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/barbecue/ean13/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barbecueEAN13Barcode(@PathVariable("barcode") String barcode) throws Exception {
|
||||
return okResponse(BarbecueBarcodeGenerator.generateEAN13BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/barbecue/code128", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barbecueCode128Barcode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(BarbecueBarcodeGenerator.generateCode128BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/barbecue/pdf417", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barbecuePDF417Barcode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(BarbecueBarcodeGenerator.generatePDF417BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
//Barcode4j library
|
||||
|
||||
@GetMapping(value = "/barcode4j/upca/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barcode4jUPCABarcode(@PathVariable("barcode") String barcode) {
|
||||
return okResponse(Barcode4jBarcodeGenerator.generateUPCABarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/barcode4j/ean13/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barcode4jEAN13Barcode(@PathVariable("barcode") String barcode) {
|
||||
return okResponse(Barcode4jBarcodeGenerator.generateEAN13BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/barcode4j/code128", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barcode4jCode128Barcode(@RequestBody String barcode) {
|
||||
return okResponse(Barcode4jBarcodeGenerator.generateCode128BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/barcode4j/pdf417", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> barcode4jPDF417Barcode(@RequestBody String barcode) {
|
||||
return okResponse(Barcode4jBarcodeGenerator.generatePDF417BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
//Zxing library
|
||||
|
||||
@GetMapping(value = "/zxing/upca/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> zxingUPCABarcode(@PathVariable("barcode") String barcode) throws Exception {
|
||||
return okResponse(ZxingBarcodeGenerator.generateUPCABarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@GetMapping(value = "/zxing/ean13/{barcode}", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> zxingEAN13Barcode(@PathVariable("barcode") String barcode) throws Exception {
|
||||
return okResponse(ZxingBarcodeGenerator.generateEAN13BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/zxing/code128", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> zxingCode128Barcode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(ZxingBarcodeGenerator.generateCode128BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/zxing/pdf417", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> zxingPDF417Barcode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(ZxingBarcodeGenerator.generatePDF417BarcodeImage(barcode));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/zxing/qrcode", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> zxingQRCode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(ZxingBarcodeGenerator.generateQRCodeImage(barcode));
|
||||
}
|
||||
|
||||
//QRGen
|
||||
|
||||
@PostMapping(value = "/qrgen/qrcode", produces = MediaType.IMAGE_PNG_VALUE)
|
||||
public ResponseEntity<BufferedImage> qrgenQRCode(@RequestBody String barcode) throws Exception {
|
||||
return okResponse(QRGenBarcodeGenerator.generateQRCodeImage(barcode));
|
||||
}
|
||||
|
||||
private ResponseEntity<BufferedImage> okResponse(BufferedImage image) {
|
||||
return new ResponseEntity<>(image, HttpStatus.OK);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.barcodes;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootApp {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootApp.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public HttpMessageConverter<BufferedImage> createImageHttpMessageConverter() {
|
||||
return new BufferedImageHttpMessageConverter();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.baeldung.barcodes.generators;
|
||||
|
||||
import net.sourceforge.barbecue.Barcode;
|
||||
import net.sourceforge.barbecue.BarcodeFactory;
|
||||
import net.sourceforge.barbecue.BarcodeImageHandler;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class BarbecueBarcodeGenerator {
|
||||
|
||||
private static final Font BARCODE_TEXT_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 14);
|
||||
|
||||
public static BufferedImage generateUPCABarcodeImage(String barcodeText) throws Exception {
|
||||
Barcode barcode = BarcodeFactory.createUPCA(barcodeText); //checksum is automatically added
|
||||
barcode.setFont(BARCODE_TEXT_FONT);
|
||||
barcode.setResolution(400);
|
||||
|
||||
return BarcodeImageHandler.getImage(barcode);
|
||||
}
|
||||
|
||||
public static BufferedImage generateEAN13BarcodeImage(String barcodeText) throws Exception {
|
||||
Barcode barcode = BarcodeFactory.createEAN13(barcodeText); //checksum is automatically added
|
||||
barcode.setFont(BARCODE_TEXT_FONT);
|
||||
|
||||
return BarcodeImageHandler.getImage(barcode);
|
||||
}
|
||||
|
||||
public static BufferedImage generateCode128BarcodeImage(String barcodeText) throws Exception {
|
||||
Barcode barcode = BarcodeFactory.createCode128(barcodeText);
|
||||
barcode.setFont(BARCODE_TEXT_FONT);
|
||||
|
||||
return BarcodeImageHandler.getImage(barcode);
|
||||
}
|
||||
|
||||
public static BufferedImage generatePDF417BarcodeImage(String barcodeText) throws Exception {
|
||||
Barcode barcode = BarcodeFactory.createPDF417(barcodeText);
|
||||
barcode.setFont(BARCODE_TEXT_FONT);
|
||||
|
||||
return BarcodeImageHandler.getImage(barcode);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.barcodes.generators;
|
||||
|
||||
import org.krysalis.barcode4j.impl.code128.Code128Bean;
|
||||
import org.krysalis.barcode4j.impl.pdf417.PDF417Bean;
|
||||
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
|
||||
import org.krysalis.barcode4j.impl.upcean.UPCABean;
|
||||
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class Barcode4jBarcodeGenerator {
|
||||
|
||||
public static BufferedImage generateUPCABarcodeImage(String barcodeText) {
|
||||
UPCABean barcodeGenerator = new UPCABean();
|
||||
BitmapCanvasProvider canvas = new BitmapCanvasProvider(160, BufferedImage.TYPE_BYTE_BINARY, false, 0);
|
||||
|
||||
barcodeGenerator.generateBarcode(canvas, barcodeText);
|
||||
return canvas.getBufferedImage();
|
||||
}
|
||||
|
||||
public static BufferedImage generateEAN13BarcodeImage(String barcodeText) {
|
||||
EAN13Bean barcodeGenerator = new EAN13Bean();
|
||||
BitmapCanvasProvider canvas = new BitmapCanvasProvider(160, BufferedImage.TYPE_BYTE_BINARY, false, 0);
|
||||
|
||||
barcodeGenerator.generateBarcode(canvas, barcodeText);
|
||||
return canvas.getBufferedImage();
|
||||
}
|
||||
|
||||
public static BufferedImage generateCode128BarcodeImage(String barcodeText) {
|
||||
Code128Bean barcodeGenerator = new Code128Bean();
|
||||
BitmapCanvasProvider canvas = new BitmapCanvasProvider(160, BufferedImage.TYPE_BYTE_BINARY, false, 0);
|
||||
|
||||
barcodeGenerator.generateBarcode(canvas, barcodeText);
|
||||
return canvas.getBufferedImage();
|
||||
}
|
||||
|
||||
public static BufferedImage generatePDF417BarcodeImage(String barcodeText) {
|
||||
PDF417Bean barcodeGenerator = new PDF417Bean();
|
||||
BitmapCanvasProvider canvas = new BitmapCanvasProvider(160, BufferedImage.TYPE_BYTE_BINARY, false, 0);
|
||||
barcodeGenerator.setColumns(10);
|
||||
|
||||
barcodeGenerator.generateBarcode(canvas, barcodeText);
|
||||
return canvas.getBufferedImage();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.barcodes.generators;
|
||||
|
||||
import net.glxn.qrgen.javase.QRCode;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class QRGenBarcodeGenerator {
|
||||
|
||||
public static BufferedImage generateQRCodeImage(String barcodeText) throws Exception {
|
||||
ByteArrayOutputStream stream = QRCode
|
||||
.from(barcodeText)
|
||||
.withSize(250, 250)
|
||||
.stream();
|
||||
ByteArrayInputStream bis = new ByteArrayInputStream(stream.toByteArray());
|
||||
|
||||
return ImageIO.read(bis);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.barcodes.generators;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.oned.Code128Writer;
|
||||
import com.google.zxing.oned.EAN13Writer;
|
||||
import com.google.zxing.oned.UPCAWriter;
|
||||
import com.google.zxing.pdf417.PDF417Writer;
|
||||
import com.google.zxing.qrcode.QRCodeWriter;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public class ZxingBarcodeGenerator {
|
||||
|
||||
public static BufferedImage generateUPCABarcodeImage(String barcodeText) throws Exception {
|
||||
UPCAWriter barcodeWriter = new UPCAWriter();
|
||||
BitMatrix bitMatrix = barcodeWriter.encode(barcodeText, BarcodeFormat.UPC_A, 300, 150);
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
public static BufferedImage generateEAN13BarcodeImage(String barcodeText) throws Exception {
|
||||
EAN13Writer barcodeWriter = new EAN13Writer();
|
||||
BitMatrix bitMatrix = barcodeWriter.encode(barcodeText, BarcodeFormat.EAN_13, 300, 150);
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
public static BufferedImage generateCode128BarcodeImage(String barcodeText) throws Exception {
|
||||
Code128Writer barcodeWriter = new Code128Writer();
|
||||
BitMatrix bitMatrix = barcodeWriter.encode(barcodeText, BarcodeFormat.CODE_128, 300, 150);
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
public static BufferedImage generatePDF417BarcodeImage(String barcodeText) throws Exception {
|
||||
PDF417Writer barcodeWriter = new PDF417Writer();
|
||||
BitMatrix bitMatrix = barcodeWriter.encode(barcodeText, BarcodeFormat.PDF_417, 700, 700);
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
public static BufferedImage generateQRCodeImage(String barcodeText) throws Exception {
|
||||
QRCodeWriter barcodeWriter = new QRCodeWriter();
|
||||
BitMatrix bitMatrix = barcodeWriter.encode(barcodeText, BarcodeFormat.QR_CODE, 200, 200);
|
||||
|
||||
return MatrixToImageWriter.toBufferedImage(bitMatrix);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue