forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-3390] - Overview of generating barcodes and QR codes in Java (e…
…ugenp#8418) Co-authored-by: ashleyfrieze <[email protected]>
- Loading branch information
1 parent
3b33841
commit 52f9fe3
Showing
7 changed files
with
334 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
libraries-3/src/main/java/com/baeldung/barcodes/BarcodesController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
libraries-3/src/main/java/com/baeldung/barcodes/SpringBootApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
libraries-3/src/main/java/com/baeldung/barcodes/generators/BarbecueBarcodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
libraries-3/src/main/java/com/baeldung/barcodes/generators/Barcode4jBarcodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
libraries-3/src/main/java/com/baeldung/barcodes/generators/QRGenBarcodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
libraries-3/src/main/java/com/baeldung/barcodes/generators/ZxingBarcodeGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |