add code39 writer
This commit is contained in:
parent
64bbee5cd3
commit
45b783c90a
4 changed files with 63 additions and 10 deletions
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import org.xbib.graphics.zxing.common.BitMatrix;
|
||||||
|
|
||||||
import org.xbib.graphics.zxing.WriterException;
|
import org.xbib.graphics.zxing.WriterException;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
@ -16,15 +17,21 @@ public class QRCodeWriter {
|
||||||
public 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 {
|
throws WriterException, IOException, NotFoundException {
|
||||||
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
|
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 {
|
throws WriterException, IOException, NotFoundException {
|
||||||
BitMatrix matrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,11 @@ import org.junit.jupiter.api.Test;
|
||||||
import org.xbib.graphics.barcode.Code3Of9;
|
import org.xbib.graphics.barcode.Code3Of9;
|
||||||
import org.xbib.graphics.barcode.HumanReadableLocation;
|
import org.xbib.graphics.barcode.HumanReadableLocation;
|
||||||
import org.xbib.graphics.barcode.render.BarcodeGraphicsRenderer;
|
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.Color;
|
||||||
import java.awt.Graphics2D;
|
import java.awt.Graphics2D;
|
||||||
import java.awt.Rectangle;
|
import java.awt.Rectangle;
|
||||||
|
@ -73,4 +78,13 @@ public class Code39Test {
|
||||||
return new BarcodeGraphicsRenderer(g2d, rectangle, scalingFactorX, scalingFactorY,
|
return new BarcodeGraphicsRenderer(g2d, rectangle, scalingFactorX, scalingFactorY,
|
||||||
Color.WHITE, Color.BLACK, false, false);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,9 @@ import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class QRCodeTest {
|
public class QRCodeTest {
|
||||||
|
|
||||||
private static final Logger logger = Logger.getLogger(QRCodeTest.class.getName());
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The hard way by using Okapi Barcode.
|
* The hard way by using Okapi Barcode.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
@ -57,7 +53,6 @@ public class QRCodeTest {
|
||||||
Color.WHITE, Color.BLACK, false, false);
|
Color.WHITE, Color.BLACK, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Easy way by using zxing.
|
* Easy way by using zxing.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
|
@ -69,7 +64,7 @@ public class QRCodeTest {
|
||||||
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
||||||
try (OutputStream outputStream = Files.newOutputStream(Paths.get("build/ifla-20.png"))) {
|
try (OutputStream outputStream = Files.newOutputStream(Paths.get("build/ifla-20.png"))) {
|
||||||
qrCodeWriter.write("IFLA-20: Art der gewünschten Lieferung nicht möglich",
|
qrCodeWriter.write("IFLA-20: Art der gewünschten Lieferung nicht möglich",
|
||||||
outputStream, 200, 200);
|
outputStream, "png", 200, 200);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue