add code39 writer

This commit is contained in:
Jörg Prante 2023-12-07 14:46:25 +01:00
parent 64bbee5cd3
commit 45b783c90a
4 changed files with 63 additions and 10 deletions

View file

@ -0,0 +1,37 @@
package org.xbib.graphics.barcode.render;
import org.xbib.graphics.zxing.BarcodeFormat;
import org.xbib.graphics.zxing.MultiFormatWriter;
import org.xbib.graphics.zxing.NotFoundException;
import org.xbib.graphics.zxing.WriterException;
import org.xbib.graphics.zxing.common.BitMatrix;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
public class Code39Writer {
public Code39Writer() {
}
public void write(String content, Path path, String format, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_39, width, height);
MatrixToImageWriter.writeToPath(matrix, format, path);
}
public void write(String content, OutputStream outputStream, String format, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_39, width, height);
MatrixToImageWriter.writeToStream(matrix, format, outputStream);
}
public BufferedImage write(String content, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_39, width, height);
return MatrixToImageWriter.toBufferedImage(matrix);
}
}

View file

@ -7,6 +7,7 @@ import org.xbib.graphics.zxing.common.BitMatrix;
import org.xbib.graphics.zxing.WriterException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
@ -16,15 +17,21 @@ public class QRCodeWriter {
public QRCodeWriter() {
}
public void write(String content, Path path, int width, int height)
public void write(String content, Path path, String format, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToPath(matrix, "png", path);
MatrixToImageWriter.writeToPath(matrix, format, path);
}
public void write(String content, OutputStream outputStream, int width, int height)
public void write(String content, OutputStream outputStream, String format, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
MatrixToImageWriter.writeToStream(matrix, "png", outputStream);
MatrixToImageWriter.writeToStream(matrix, format, outputStream);
}
public BufferedImage write(String content, int width, int height)
throws WriterException, IOException, NotFoundException {
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
return MatrixToImageWriter.toBufferedImage(matrix);
}
}

View file

@ -4,6 +4,11 @@ import org.junit.jupiter.api.Test;
import org.xbib.graphics.barcode.Code3Of9;
import org.xbib.graphics.barcode.HumanReadableLocation;
import org.xbib.graphics.barcode.render.BarcodeGraphicsRenderer;
import org.xbib.graphics.barcode.render.Code39Writer;
import org.xbib.graphics.barcode.render.QRCodeWriter;
import org.xbib.graphics.zxing.NotFoundException;
import org.xbib.graphics.zxing.WriterException;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
@ -73,4 +78,13 @@ public class Code39Test {
return new BarcodeGraphicsRenderer(g2d, rectangle, scalingFactorX, scalingFactorY,
Color.WHITE, Color.BLACK, false, false);
}
@Test
public void testQrCode() throws IOException, NotFoundException, WriterException {
Code39Writer codeWriter = new Code39Writer();
try (OutputStream outputStream = Files.newOutputStream(Paths.get("build/code39.png"))) {
codeWriter.write("123456789", outputStream, "png", 200, 50);
}
}
}

View file

@ -14,13 +14,9 @@ import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
public class QRCodeTest {
private static final Logger logger = Logger.getLogger(QRCodeTest.class.getName());
/**
* The hard way by using Okapi Barcode.
* @throws IOException
@ -57,7 +53,6 @@ public class QRCodeTest {
Color.WHITE, Color.BLACK, false, false);
}
/**
* Easy way by using zxing.
* @throws IOException
@ -69,7 +64,7 @@ public class QRCodeTest {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
try (OutputStream outputStream = Files.newOutputStream(Paths.get("build/ifla-20.png"))) {
qrCodeWriter.write("IFLA-20: Art der gewünschten Lieferung nicht möglich",
outputStream, 200, 200);
outputStream, "png", 200, 200);
}
}
}