+ * This class provides methods to create barcodes in popular formats such as
+ * Code128, Code39, Code93, EAN-13, EAN-8, UPC-A, QR Code, and Data Matrix.
+ * Each barcode type can be generated as a {@link BufferedImage} (for PNG)
+ * or as a data URI string (in either PNG or SVG formats) that can be
+ * directly embedded into HTML or other documents.
+ *
+ *
+ * Example usage:
+ *
+ *
{@code
+ * // Generate a PNG BufferedImage for a Code128 barcode
+ * BufferedImage image = Okapi.code128("1234567890", 200, 100);
+ *
+ * // Generate a data URI for embedding a Code39 barcode in SVG format
+ * String svgDataUri = Okapi.code39Svg("12345", 1.0);
+ * }
+ *
+ *
+ * All methods are static, and this class is not intended to be instantiated.
+ * The methods are designed for easy generation of barcode images with adjustable
+ * dimensions (for PNG) or magnification (for SVG) to suit various rendering contexts.
+ *
+ *
+ *
+ * Supported barcode formats:
+ *
+ *
+ *
Code128
+ *
Code39
+ *
Code93
+ *
EAN-13
+ *
EAN-8
+ *
UPC-A
+ *
QR Code
+ *
Data Matrix
+ *
+ *
+ *
+ * Supported image outputs:
+ *
+ *
+ *
PNG - rendered as a {@link BufferedImage}
+ *
SVG - rendered as a Base64-encoded data URI string
+ *
+ *
+ *
+ * All SVG generation methods offer an optional magnification parameter to scale
+ * the output image, making it easy to produce barcodes suitable for a wide range
+ * of display sizes and resolutions.
+ *
+ *
+ *
+ * Note: PNG generation relies on the {@link Java2DRenderer}
+ * and requires setting appropriate image dimensions for accurate rendering.
+ * SVG output uses {@link SvgRenderer} and supports magnification scaling.
+ *
+ *
+ * @see Code128
+ * @see Code3Of9
+ * @see Ean
+ * @see SvgRenderer
+ * @see Java2DRenderer
+ */
+public class Okapi {
+
+ private Okapi() {
+ // prevent instantiation
+ }
+
+ public static BufferedImage code128(String value, int width, int height) {
+ Code128 code128 = new Code128();
+ code128.setContent(value);
+ return generateBarcodePng(code128, width, height);
+ }
+
+ public static String code128Png(String value, int width, int height) {
+ return dataUriPng(code128(value, width, height));
+ }
+
+ public static String code128Svg(String value, double magnification) {
+ Code128 code128 = new Code128();
+ code128.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(code128, magnification));
+ }
+
+ public static BufferedImage code39(String value, int width, int height) {
+ Code3Of9 code39 = new Code3Of9();
+ code39.setContent(value);
+ return generateBarcodePng(code39, width, height);
+ }
+
+ public static String code39Png(String value, int width, int height) {
+ return dataUriPng(code39(value, width, height));
+ }
+
+ public static String code39Svg(String value, double magnification) {
+ Code3Of9 code39 = new Code3Of9();
+ code39.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(code39, magnification));
+ }
+
+ public static BufferedImage code93(String value, int width, int height) {
+ Code93 code93 = new Code93();
+ code93.setContent(value);
+ return generateBarcodePng(code93, width, height);
+ }
+
+ public static String code93Png(String value, int width, int height) {
+ return dataUriPng(code93(value, width, height));
+ }
+
+ public static String code93Svg(String value, double magnification) {
+ Code93 code93 = new Code93();
+ code93.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(code93, magnification));
+ }
+
+ public static BufferedImage ean13(String value, int width, int height) {
+ Ean ean13 = new Ean(Ean.Mode.EAN13);
+ ean13.setContent(value);
+ return generateBarcodePng(ean13, width, height);
+ }
+
+ public static String ean13Png(String value, int width, int height) {
+ return dataUriPng(ean13(value, width, height));
+ }
+
+ public static String ean13Svg(String value, double magnification) {
+ Ean ean13 = new Ean(Ean.Mode.EAN13);
+ ean13.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(ean13, magnification));
+ }
+
+ public static BufferedImage ean8(String value, int width, int height) {
+ Ean ean8 = new Ean(Ean.Mode.EAN8);
+ ean8.setContent(value);
+ return generateBarcodePng(ean8, width, height);
+ }
+
+ public static String ean8Png(String value, int width, int height) {
+ return dataUriPng(ean8(value, width, height));
+ }
+
+ public static String ean8Svg(String value, double magnification) {
+ Ean ean8 = new Ean(Ean.Mode.EAN8);
+ ean8.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(ean8, magnification));
+ }
+
+ public static BufferedImage upcA(String value, int width, int height) {
+ Upc upcA = new Upc(Upc.Mode.UPCA);
+ upcA.setContent(value);
+ return generateBarcodePng(upcA, width, height);
+ }
+
+ public static String upcAPng(String value, int width, int height) {
+ return dataUriPng(upcA(value, width, height));
+ }
+
+ public static String upcASvg(String value, double magnification) {
+ Upc upcA = new Upc(Upc.Mode.UPCA);
+ upcA.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(upcA, magnification));
+ }
+
+ public static BufferedImage qrCode(String value, int width, int height) {
+ QrCode qrCode = new QrCode();
+ qrCode.setContent(value);
+ return generateBarcodePng(qrCode, width, height);
+ }
+
+ public static String qrCodePng(String value, int width, int height) {
+ return dataUriPng(qrCode(value, width, height));
+ }
+
+ public static String qrCodeSvg(String value, double magnification) {
+ QrCode qrCode = new QrCode();
+ qrCode.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(qrCode, magnification));
+ }
+
+ public static BufferedImage dataMatrix(String value, int width, int height) {
+ DataMatrix dataMatrix = new DataMatrix();
+ dataMatrix.setContent(value);
+ return generateBarcodePng(dataMatrix, width, height);
+ }
+
+ public static String dataMatrixPng(String value, int width, int height) {
+ return dataUriPng(dataMatrix(value, width, height));
+ }
+
+ public static String dataMatrixSvg(String value, double magnification) {
+ DataMatrix dataMatrix = new DataMatrix();
+ dataMatrix.setContent(value);
+ return dataUriSvg(generateBarcodeSvg(dataMatrix, magnification));
+ }
+
+ /**
+ * Generates an SVG barcode as a String with default magnification of 1.0.
+ *
+ * @param symbol The barcode symbol to render
+ * @return String containing the SVG barcode
+ */
+ public static String generateBarcodeSvgAsString(Symbol symbol) {
+ return new String(generateBarcodeSvg(symbol, 1.0));
+ }
+
+ /**
+ * Generates an SVG barcode as a byte array with default magnification of 1.0.
+ *
+ * @param symbol The barcode symbol to render
+ * @return byte array containing the SVG barcode
+ */
+ public static byte[] generateBarcodeSvg(Symbol symbol) {
+ return generateBarcodeSvg(symbol, 1.0);
+ }
+
+ /**
+ * Generates an SVG barcode as a byte array with specified magnification.
+ *
+ * @param symbol The barcode symbol to render
+ * @param magnification The magnification factor to apply
+ * @return byte array containing the SVG barcode
+ */
+ public static byte[] generateBarcodeSvg(Symbol symbol, double magnification) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ SvgRenderer renderer = new SvgRenderer(out, magnification, Color.WHITE, Color.BLACK, true);
+ try {
+ renderer.render(symbol);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return out.toByteArray();
+ }
+
+ /**
+ * Generates a PNG barcode as a BufferedImage with default magnification of 1.0.
+ *
+ * @param symbol The barcode symbol to render
+ * @param width The desired width of the image
+ * @param height The desired height of the image
+ * @return BufferedImage containing the PNG barcode
+ */
+ public static BufferedImage generateBarcodePng(Symbol symbol, int width, int height) {
+ return generateBarcodePng(symbol, width, height, 1.0);
+ }
+
+ /**
+ * Generates a PNG barcode as a BufferedImage with specified magnification.
+ *
+ * @param symbol The barcode symbol to render
+ * @param width The desired width of the image
+ * @param height The desired height of the image
+ * @param magnification The magnification factor to apply
+ * @return BufferedImage containing the PNG barcode
+ */
+ public static BufferedImage generateBarcodePng(Symbol symbol, int width, int height, double magnification) {
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
+ Graphics2D g2d = image.createGraphics();
+ Java2DRenderer renderer = new Java2DRenderer(g2d, magnification, Color.WHITE, Color.BLACK);
+ renderer.render(symbol);
+ g2d.dispose();
+ return image;
+ }
+
+ /**
+ * Converts a BufferedImage to a data URI string in PNG format.
+ *
+ * @param image The BufferedImage to convert
+ * @return String containing the data URI
+ */
+ public static String dataUriPng(BufferedImage image) {
+ return base64ToDataUri(pngToBase64(image), "image/png");
+ }
+
+ /**
+ * Converts a byte array to a data URI string in SVG format.
+ *
+ * @param bytes The byte array containing SVG data
+ * @return String containing the data URI
+ */
+ public static String dataUriSvg(byte[] bytes) {
+ final String base64 = bytesToBase64(bytes);
+ return base64ToDataUri(base64, "image/svg+xml");
+ }
+
+ /**
+ * Converts a BufferedImage to a PNG byte array.
+ *
+ * @param image The BufferedImage to convert
+ * @return byte array containing the PNG data
+ * @throws RuntimeException if the image cannot be written
+ */
+ public static byte[] pngToBytes(BufferedImage image) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ ImageIO.write(image, "png", out);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ return out.toByteArray();
+ }
+
+ /**
+ * Converts a BufferedImage to a Base64 encoded PNG string.
+ *
+ * @param image The BufferedImage to convert
+ * @return String containing the Base64 encoded PNG data
+ */
+ public static String pngToBase64(BufferedImage image) {
+ return bytesToBase64(new String(pngToBytes(image), StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
+ }
+
+ /**
+ * Converts a byte array to a Base64 encoded string.
+ *
+ * @param bytes The byte array to convert
+ * @return String containing the Base64 encoded data
+ */
+ public static String bytesToBase64(byte[] bytes) {
+ return Base64.getEncoder().encodeToString(bytes);
+ }
+
+ /**
+ * Creates a data URI string from Base64 encoded data and MIME type.
+ *
+ * @param base64 The Base64 encoded data
+ * @param mimeType The MIME type of the data
+ * @return String containing the data URI
+ */
+ public static String base64ToDataUri(String base64, String mimeType) {
+ return "data:%s;base64,%s".formatted(mimeType, base64);
+ }
+
+}
\ No newline at end of file
diff --git a/ext-zxing/runtime/src/main/java/io/quarkiverse/barcode/zxing/ZebraCrossing.java b/ext-zxing/runtime/src/main/java/io/quarkiverse/barcode/zxing/ZebraCrossing.java
index 3726977..185c901 100644
--- a/ext-zxing/runtime/src/main/java/io/quarkiverse/barcode/zxing/ZebraCrossing.java
+++ b/ext-zxing/runtime/src/main/java/io/quarkiverse/barcode/zxing/ZebraCrossing.java
@@ -6,11 +6,19 @@
import java.util.Base64;
import java.util.Map;
-import com.google.zxing.*;
+import com.google.zxing.BarcodeFormat;
+import com.google.zxing.EncodeHintType;
+import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.datamatrix.DataMatrixWriter;
-import com.google.zxing.oned.*;
+import com.google.zxing.oned.Code128Writer;
+import com.google.zxing.oned.Code39Writer;
+import com.google.zxing.oned.Code93Writer;
+import com.google.zxing.oned.EAN13Writer;
+import com.google.zxing.oned.EAN8Writer;
+import com.google.zxing.oned.UPCAWriter;
+import com.google.zxing.oned.UPCEWriter;
import com.google.zxing.qrcode.QRCodeWriter;
/**
@@ -182,10 +190,23 @@ public static String dataMatrixImg(String value, int width, int height) {
return dataUriImg(dataMatrix(value, width, height));
}
+ /**
+ * Converts a BitMatrix barcode to an HTML img tag with embedded PNG data URI.
+ *
+ * @param encoded The BitMatrix containing the barcode data
+ * @return String containing an HTML img tag with the barcode as a data URI
+ */
public static String dataUriImg(BitMatrix encoded) {
return dataUriImg(base64ToDataUri(pngToBase64(barcodetoPng(encoded))));
}
+ /**
+ * Converts a BitMatrix barcode to a PNG byte array.
+ *
+ * @param encoded The BitMatrix containing the barcode data
+ * @return byte array containing the PNG data
+ * @throws RuntimeException if the image cannot be written
+ */
public static byte[] barcodetoPng(BitMatrix encoded) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
@@ -196,18 +217,42 @@ public static byte[] barcodetoPng(BitMatrix encoded) {
return out.toByteArray();
}
+ /**
+ * Converts a PNG byte array to a Base64 encoded string.
+ *
+ * @param png The PNG byte array to convert
+ * @return String containing the Base64 encoded PNG data
+ */
public static String pngToBase64(byte[] png) {
return Base64.getEncoder().encodeToString(png);
}
+ /**
+ * Creates a PNG data URI string from Base64 encoded data.
+ *
+ * @param base64 The Base64 encoded PNG data
+ * @return String containing the PNG data URI
+ */
public static String base64ToDataUri(String base64) {
return "data:image/png;base64," + base64;
}
+ /**
+ * Wraps a data URI in an HTML img tag.
+ *
+ * @param dataUri The data URI to wrap
+ * @return String containing an HTML img tag with the data URI as the src
+ */
public static String dataUriImg(String dataUri) {
return "";
}
+ /**
+ * Gets the default encoding hints used for barcode generation.
+ * Sets UTF-8 as the default character set.
+ *
+ * @return Map containing the default encoding hints
+ */
private static Map getHints() {
return Map.of(EncodeHintType.CHARACTER_SET, StandardCharsets.UTF_8.name());
}