From 43199f9190062ca5255a3e54d12bdc13380308c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E9=9C=8F=E9=9C=8F?= Date: Tue, 10 Sep 2024 16:23:09 +0800 Subject: [PATCH 1/2] improvement: MakeBarcode easy to use as Java API 1. Rewrite process to fix some default value problem 2. Add processToStream to create OutputStream 3. Add processToByte to create byte[] 4. Add setters to Settings, add whiteSpace Support 5. Add SymbolType, SymbolFormat 6. Add WhiteSpace Support, Add BarHeight Support problem found: 1. the EAN-8, EAN13, data length validator is not smart --- .gitignore | 17 + .../java/uk/org/okapibarcode/MakeBarcode.java | 573 +++++++++++------- .../java/uk/org/okapibarcode/Settings.java | 81 ++- .../uk/org/okapibarcode/SymbolFormat.java | 49 ++ .../java/uk/org/okapibarcode/SymbolType.java | 246 ++++++++ .../uk/org/okapibarcode/MakeBarcodeTest.java | 229 +++++++ 6 files changed, 961 insertions(+), 234 deletions(-) create mode 100644 src/main/java/uk/org/okapibarcode/SymbolFormat.java create mode 100644 src/main/java/uk/org/okapibarcode/SymbolType.java create mode 100644 src/test/java/uk/org/okapibarcode/MakeBarcodeTest.java diff --git a/.gitignore b/.gitignore index 1cb802e3..f34d3fed 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,20 @@ bin/ # Misc temp dir temp/ + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/ +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ +nbactions.xml +nb-configuration.xml diff --git a/src/main/java/uk/org/okapibarcode/MakeBarcode.java b/src/main/java/uk/org/okapibarcode/MakeBarcode.java index 36b62764..86647228 100644 --- a/src/main/java/uk/org/okapibarcode/MakeBarcode.java +++ b/src/main/java/uk/org/okapibarcode/MakeBarcode.java @@ -13,16 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package uk.org.okapibarcode; import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.io.OutputStream; import javax.imageio.ImageIO; @@ -53,7 +54,6 @@ import uk.org.okapibarcode.backend.DataMatrix.ForceMode; import uk.org.okapibarcode.backend.Ean; import uk.org.okapibarcode.backend.GridMatrix; -import uk.org.okapibarcode.backend.HumanReadableLocation; import uk.org.okapibarcode.backend.JapanPost; import uk.org.okapibarcode.backend.KixCode; import uk.org.okapibarcode.backend.KoreaPost; @@ -79,77 +79,229 @@ import uk.org.okapibarcode.output.Java2DRenderer; import uk.org.okapibarcode.output.PostScriptRenderer; import uk.org.okapibarcode.output.SvgRenderer; + /** * * @author Robin Stuart */ public class MakeBarcode { + public byte[] processToByte(Settings settings, String dataInput, String format) { + + ByteArrayOutputStream out = (ByteArrayOutputStream) this.processToStream(settings, dataInput, format); + + if (out != null && out.size() > 0) { + return out.toByteArray(); + } + + return null; + } + + public OutputStream processToStream(Settings settings, String dataInput, String format) { + + Symbol symbol = this.create(settings, dataInput); + + Color ink = settings.getForegroundColour(); + Color paper = settings.getBackgroundColour(); + + // add scale support + int scale = settings.getSymbolScale(); + + if (settings.isReverseColour()) { + ink = Color.WHITE; + paper = Color.BLACK; + } + + try { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + switch (format) { + case "png": + case "gif": + case "jpg": + case "bmp": + BufferedImage image = new BufferedImage( + symbol.getWidth() * scale, + symbol.getHeight() * scale, + BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = image.createGraphics(); + //g2d.setBackground(paper); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Java2DRenderer renderer = new Java2DRenderer(g2d, scale, paper, ink); + + renderer.render(symbol); + ImageIO.write(image, format, out); + + break; + case "svg": + SvgRenderer svg = new SvgRenderer(out, scale, paper, ink, true); + svg.render(symbol); + break; + case "eps": + PostScriptRenderer eps = new PostScriptRenderer(out, scale, paper, ink); + eps.render(symbol); + break; + default: + System.out.println("Unsupported output format"); + + break; + } + + if (out.size() > 0) { + return out; + } + + out.close(); + + } catch (IOException e) { + System.out.printf("Write Error\n"); + } + + return null; + } + + /** + * Make a barcode as file. + * + * @param settings + * @param dataInput + * @param outputFileName + */ public void process(Settings settings, String dataInput, String outputFileName) { - int type = settings.getSymbolType(); - Symbol symbol; + + Symbol symbol = this.create(settings, dataInput); + String extension = ""; - HumanReadableLocation hrtLocation = settings.getHrtPosition(); Color ink = settings.getForegroundColour(); Color paper = settings.getBackgroundColour(); + // add scale support + int scale = settings.getSymbolScale(); + if (settings.isReverseColour()) { ink = Color.WHITE; paper = Color.BLACK; } + File file = new File(outputFileName); + + try { + int i = file.getName().lastIndexOf('.'); + if (i > 0) { + extension = file.getName().substring(i + 1); + } + + switch (extension) { + case "png": + case "gif": + case "jpg": + case "bmp": + BufferedImage image = new BufferedImage( + symbol.getWidth() * scale, + symbol.getHeight() * scale, + BufferedImage.TYPE_INT_RGB); + Graphics2D g2d = image.createGraphics(); + //g2d.setBackground(paper); + g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + Java2DRenderer renderer = new Java2DRenderer(g2d, 1, paper, ink); + System.out.printf("MakeBarcode\n"); + renderer.render(symbol); + + try { + ImageIO.write(image, extension, file); + } catch (IOException e) { + System.out.printf("Error outputting to file\n"); + } + break; + case "svg": + SvgRenderer svg = new SvgRenderer(new FileOutputStream(file), scale, paper, ink, true); + svg.render(symbol); + break; + case "eps": + PostScriptRenderer eps = new PostScriptRenderer(new FileOutputStream(file), scale, paper, ink); + eps.render(symbol); + break; + default: + System.out.println("Unsupported output format"); + break; + } + + } catch (FileNotFoundException e) { + System.out.printf("File Not Found\n"); + } catch (IOException e) { + System.out.printf("Write Error\n"); + } + } + + private Symbol create(Settings settings, String dataInput) { + int type = settings.getSymbolType(); + + if (dataInput == null) { + dataInput = ""; + } + + Symbol symbol = null; + + // GS1 data + if (settings.isDataGs1Mode() && !dataInput.isBlank()) { + dataInput = dataInput.trim() + .replace("(", "[").replace(")", "]"); + } + try { /* values marked "Legacy" are for compatability purposes and should not be documented. - */ - switch(type) { + */ + switch (type) { case 1: // Code 11 Code11 code11 = new Code11(); - code11.setHumanReadableLocation(hrtLocation); - code11.setContent(dataInput); - symbol = code11; + //code11.setHumanReadableLocation(hrtLocation); + //code11.setContent(dataInput); + symbol = this.make(settings, dataInput, code11); break; case 2: // Code 2 of 5 Code2Of5 c25matrix = new Code2Of5(); c25matrix.setMode(ToFMode.MATRIX); - c25matrix.setHumanReadableLocation(hrtLocation); - c25matrix.setContent(dataInput); - symbol = c25matrix; + //c25matrix.setHumanReadableLocation(hrtLocation); + //c25matrix.setContent(dataInput); + symbol = this.make(settings, dataInput, c25matrix); break; case 3: //Interleaved 2 of 5 Code2Of5 c25inter = new Code2Of5(); c25inter.setMode(ToFMode.INTERLEAVED); - c25inter.setHumanReadableLocation(hrtLocation); - c25inter.setContent(dataInput); - symbol = c25inter; + //c25inter.setHumanReadableLocation(hrtLocation); + //c25inter.setContent(dataInput); + symbol = this.make(settings, dataInput, c25inter); break; case 4: // IATA 2 of 5 Code2Of5 c25iata = new Code2Of5(); c25iata.setMode(ToFMode.IATA); - c25iata.setHumanReadableLocation(hrtLocation); - c25iata.setContent(dataInput); - symbol = c25iata; + //c25iata.setHumanReadableLocation(hrtLocation); + //c25iata.setContent(dataInput); + symbol = this.make(settings, dataInput, c25iata); break; case 6: // Data Logic Code2Of5 c25logic = new Code2Of5(); c25logic.setMode(ToFMode.DATA_LOGIC); - c25logic.setHumanReadableLocation(hrtLocation); - c25logic.setContent(dataInput); - symbol = c25logic; + //c25logic.setHumanReadableLocation(hrtLocation); + //c25logic.setContent(dataInput); + symbol = this.make(settings, dataInput, c25logic); break; case 7: // Industrial 2 of 5 Code2Of5 c25ind = new Code2Of5(); c25ind.setMode(ToFMode.INDUSTRIAL); - c25ind.setHumanReadableLocation(hrtLocation); - c25ind.setContent(dataInput); - symbol = c25ind; + //c25ind.setHumanReadableLocation(hrtLocation); + //c25ind.setContent(dataInput); + symbol = this.make(settings, dataInput, c25ind); break; case 8: case 99: @@ -159,16 +311,16 @@ public void process(Settings settings, String dataInput, String outputFileName) if ((type == 99) || (type == 101)) { code3of9.setDataType(Symbol.DataType.HIBC); } - code3of9.setHumanReadableLocation(hrtLocation); - code3of9.setContent(dataInput); - symbol = code3of9; + //code3of9.setHumanReadableLocation(hrtLocation); + //code3of9.setContent(dataInput); + symbol = this.make(settings, dataInput, code3of9); break; case 9: // Extended Code 39 Code3Of9Extended code3of9ext = new Code3Of9Extended(); - code3of9ext.setHumanReadableLocation(hrtLocation); - code3of9ext.setContent(dataInput); - symbol = code3of9ext; + //code3of9ext.setHumanReadableLocation(hrtLocation); + //code3of9ext.setContent(dataInput); + symbol = this.make(settings, dataInput, code3of9ext); break; case 13: case 14: // Legacy @@ -179,19 +331,21 @@ public void process(Settings settings, String dataInput, String outputFileName) // EAN Ean ean = new Ean(); if (eanCalculateVersion(dataInput) == 8) { + //System.out.println("EAN8:"); ean.setMode(Ean.Mode.EAN8); } else { + //System.out.println("EAN13:"); ean.setMode(Ean.Mode.EAN13); } - ean.setContent(dataInput); - symbol = ean; + //ean.setContent(dataInput); + symbol = this.make(settings, dataInput, ean); break; case 18: // Codabar Codabar codabar = new Codabar(); - codabar.setHumanReadableLocation(hrtLocation); - codabar.setContent(dataInput); - symbol = codabar; + //codabar.setHumanReadableLocation(hrtLocation); + //codabar.setContent(dataInput); + symbol = this.make(settings, dataInput, codabar); break; case 20: case 60: @@ -213,25 +367,26 @@ public void process(Settings settings, String dataInput, String outputFileName) code128.setCodeSet(CodeSet.AB); } code128.setReaderInit(settings.isReaderInit()); - code128.setHumanReadableLocation(hrtLocation); - code128.setContent(dataInput); - symbol = code128; + //code128.setHumanReadableLocation(hrtLocation); + //code128.setContent(dataInput); + symbol = this.make(settings, dataInput, code128); + ; break; case 21: // Leitcode Code2Of5 dpLeit = new Code2Of5(); dpLeit.setMode(ToFMode.DP_LEITCODE); - dpLeit.setHumanReadableLocation(hrtLocation); - dpLeit.setContent(dataInput); - symbol = dpLeit; + //dpLeit.setHumanReadableLocation(hrtLocation); + //dpLeit.setContent(dataInput); + symbol = this.make(settings, dataInput, dpLeit); break; case 22: // Identcode Code2Of5 dpIdent = new Code2Of5(); dpIdent.setMode(ToFMode.DP_IDENTCODE); - dpIdent.setHumanReadableLocation(hrtLocation); - dpIdent.setContent(dataInput); - symbol = dpIdent; + //dpIdent.setHumanReadableLocation(hrtLocation); + //dpIdent.setContent(dataInput); + symbol = this.make(settings, dataInput, dpIdent); break; case 23: // Code 16k @@ -240,53 +395,53 @@ public void process(Settings settings, String dataInput, String outputFileName) code16k.setDataType(Symbol.DataType.GS1); } code16k.setReaderInit(settings.isReaderInit()); - code16k.setContent(dataInput); - symbol = code16k; + //code16k.setContent(dataInput); + symbol = this.make(settings, dataInput, code16k); break; case 24: // Code 49 Code49 code49 = new Code49(); - code49.setHumanReadableLocation(hrtLocation); - code49.setContent(dataInput); - symbol = code49; + //code49.setHumanReadableLocation(hrtLocation); + //code49.setContent(dataInput); + symbol = this.make(settings, dataInput, code49); break; case 25: // Code 93 Code93 code93 = new Code93(); - code93.setHumanReadableLocation(hrtLocation); - code93.setContent(dataInput); - symbol = code93; + //code93.setHumanReadableLocation(hrtLocation); + //code93.setContent(dataInput); + symbol = this.make(settings, dataInput, code93); break; case 29: // Databar-14 DataBar14 dataBar14 = new DataBar14(); dataBar14.setMode(Mode.LINEAR); - dataBar14.setHumanReadableLocation(hrtLocation); - dataBar14.setContent(dataInput); - symbol = dataBar14; + //dataBar14.setHumanReadableLocation(hrtLocation); + //dataBar14.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBar14); break; case 30: // Databar Limited DataBarLimited dataBarLimited = new DataBarLimited(); - dataBarLimited.setHumanReadableLocation(hrtLocation); - dataBarLimited.setContent(dataInput); - symbol = dataBarLimited; + //dataBarLimited.setHumanReadableLocation(hrtLocation); + //dataBarLimited.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBarLimited); break; case 31: // Databar Expanded DataBarExpanded dataBarE = new DataBarExpanded(); dataBarE.setStacked(false); - dataBarE.setHumanReadableLocation(hrtLocation); - dataBarE.setContent(dataInput); - symbol = dataBarE; + //dataBarE.setHumanReadableLocation(hrtLocation); + //dataBarE.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBarE); break; case 32: // Telepen Alpha Telepen telepen = new Telepen(); telepen.setMode(Telepen.Mode.NORMAL); - telepen.setHumanReadableLocation(hrtLocation); - telepen.setContent(dataInput); - symbol = telepen; + //telepen.setHumanReadableLocation(hrtLocation); + //telepen.setContent(dataInput); + symbol = this.make(settings, dataInput, telepen); break; case 34: case 35: // Legacy @@ -294,8 +449,8 @@ public void process(Settings settings, String dataInput, String outputFileName) // UPC-A Upc upca = new Upc(); upca.setMode(Upc.Mode.UPCA); - upca.setContent(dataInput); - symbol = upca; + //upca.setContent(dataInput); + symbol = this.make(settings, dataInput, upca); break; case 37: case 38: // Legacy @@ -303,8 +458,8 @@ public void process(Settings settings, String dataInput, String outputFileName) // UPC-E Upc upce = new Upc(); upce.setMode(Upc.Mode.UPCE); - upce.setContent(dataInput); - symbol = upce; + //upce.setContent(dataInput); + symbol = this.make(settings, dataInput, upce); break; case 40: case 41: // Legacy @@ -316,22 +471,22 @@ public void process(Settings settings, String dataInput, String outputFileName) // Postnet and Brizillian CepNet Postnet postnet = new Postnet(); postnet.setMode(Postnet.Mode.POSTNET); - postnet.setContent(dataInput); - symbol = postnet; + //postnet.setContent(dataInput); + symbol = this.make(settings, dataInput, postnet); break; case 47: // MSI Plessey MsiPlessey msiPlessey = new MsiPlessey(); - msiPlessey.setHumanReadableLocation(hrtLocation); - msiPlessey.setContent(dataInput); - symbol = msiPlessey; + //msiPlessey.setHumanReadableLocation(hrtLocation); + //msiPlessey.setContent(dataInput); + symbol = this.make(settings, dataInput, msiPlessey); break; case 50: // LOGMARS Logmars logmars = new Logmars(); - logmars.setHumanReadableLocation(hrtLocation); - logmars.setContent(dataInput); - symbol = logmars; + //logmars.setHumanReadableLocation(hrtLocation); + //logmars.setContent(dataInput); + symbol = this.make(settings, dataInput, logmars); break; case 51: // Pharmacode One-Track @@ -342,8 +497,8 @@ public void process(Settings settings, String dataInput, String outputFileName) case 53: // Pharmacode Two-Track Pharmacode2Track pharmacode2t = new Pharmacode2Track(); - pharmacode2t.setContent(dataInput); - symbol = pharmacode2t; + //pharmacode2t.setContent(dataInput); + symbol = this.make(settings, dataInput, pharmacode2t); break; case 55: case 56: @@ -359,19 +514,23 @@ public void process(Settings settings, String dataInput, String outputFileName) } else if (type == 56) { pdf417.setMode(Pdf417.Mode.TRUNCATED); } - pdf417.setPreferredEccLevel(settings.getSymbolECC() - 1); - pdf417.setDataColumns(settings.getSymbolColumns()); + if (settings.getSymbolECC() > 0) { + pdf417.setPreferredEccLevel(settings.getSymbolECC() - 1); + } + if (settings.getSymbolColumns() > 0) { + pdf417.setDataColumns(settings.getSymbolColumns()); + } pdf417.setReaderInit(settings.isReaderInit()); - pdf417.setContent(dataInput); - symbol = pdf417; + //pdf417.setContent(dataInput); + symbol = this.make(settings, dataInput, pdf417); break; case 57: // Maxicode MaxiCode maxiCode = new MaxiCode(); maxiCode.setPrimary(settings.getPrimaryData()); maxiCode.setMode(settings.getEncodeMode()); - maxiCode.setContent(dataInput); - symbol = maxiCode; + //maxiCode.setContent(dataInput); + symbol = this.make(settings, dataInput, maxiCode); break; case 58: case 104: @@ -384,7 +543,7 @@ public void process(Settings settings, String dataInput, String outputFileName) if ((type == 104) || (type == 105)) { qrCode.setDataType(Symbol.DataType.HIBC); } - switch(settings.getSymbolECC()) { + switch (settings.getSymbolECC()) { case 0: qrCode.setPreferredEccLevel(QrCode.EccLevel.L); break; @@ -398,10 +557,13 @@ public void process(Settings settings, String dataInput, String outputFileName) qrCode.setPreferredEccLevel(QrCode.EccLevel.H); break; } - qrCode.setPreferredVersion(settings.getSymbolVersion()); + + if (settings.getSymbolVersion() > 0) { + qrCode.setPreferredVersion(settings.getSymbolVersion()); + } qrCode.setReaderInit(settings.isReaderInit()); - qrCode.setContent(dataInput); - symbol = qrCode; + //qrCode.setContent(dataInput); + symbol = this.make(settings, dataInput, qrCode); break; case 63: case 64: // Legacy @@ -409,35 +571,35 @@ public void process(Settings settings, String dataInput, String outputFileName) // Australia Post Standard Customer AustraliaPost auPost = new AustraliaPost(); auPost.setMode(AustraliaPost.Mode.POST); - auPost.setContent(dataInput); - symbol = auPost; + //auPost.setContent(dataInput); + symbol = this.make(settings, dataInput, auPost); break; case 66: // Australia Post Reply Paid AustraliaPost auReply = new AustraliaPost(); auReply.setMode(AustraliaPost.Mode.REPLY); - auReply.setContent(dataInput); - symbol = auReply; + //auReply.setContent(dataInput); + symbol = this.make(settings, dataInput, auReply); break; case 67: // Australia Post Re-Routing AustraliaPost auRoute = new AustraliaPost(); auRoute.setMode(AustraliaPost.Mode.ROUTE); - auRoute.setContent(dataInput); - symbol = auRoute; + //auRoute.setContent(dataInput); + symbol = this.make(settings, dataInput, auRoute); break; case 68: // Australia Post Redirection AustraliaPost auRedirect = new AustraliaPost(); auRedirect.setMode(AustraliaPost.Mode.REDIRECT); - auRedirect.setContent(dataInput); - symbol = auRedirect; + //auRedirect.setContent(dataInput); + symbol = this.make(settings, dataInput, auRedirect); break; case 70: // RM4SCC RoyalMail4State royalMail = new RoyalMail4State(); - royalMail.setContent(dataInput); - symbol = royalMail; + //royalMail.setContent(dataInput); + symbol = this.make(settings, dataInput, royalMail); break; case 71: case 102: @@ -453,8 +615,8 @@ public void process(Settings settings, String dataInput, String outputFileName) dataMatrix.setReaderInit(settings.isReaderInit()); dataMatrix.setPreferredSize(settings.getSymbolVersion()); dataMatrix.setForceMode(settings.isMakeSquare() ? ForceMode.SQUARE : ForceMode.NONE); - dataMatrix.setContent(dataInput); - symbol = dataMatrix; + //dataMatrix.setContent(dataInput); + symbol = this.make(settings, dataInput, dataMatrix); break; case 74: case 110: @@ -467,58 +629,58 @@ public void process(Settings settings, String dataInput, String outputFileName) if ((type == 110) || (type == 111)) { codablockF.setDataType(Symbol.DataType.HIBC); } - codablockF.setContent(dataInput); - symbol = codablockF; + //codablockF.setContent(dataInput); + symbol = this.make(settings, dataInput, codablockF); break; case 75: // NVE-18 Nve18 nve18 = new Nve18(); - nve18.setHumanReadableLocation(hrtLocation); - nve18.setContent(dataInput); - symbol = nve18; + //nve18.setHumanReadableLocation(hrtLocation); + //nve18.setContent(dataInput); + symbol = this.make(settings, dataInput, nve18); break; case 76: // Japanese Post JapanPost japanPost = new JapanPost(); - japanPost.setContent(dataInput); - symbol = japanPost; + //japanPost.setContent(dataInput); + symbol = this.make(settings, dataInput, japanPost); break; case 77: // Korea Post KoreaPost koreaPost = new KoreaPost(); - koreaPost.setHumanReadableLocation(hrtLocation); - koreaPost.setContent(dataInput); - symbol = koreaPost; + //koreaPost.setHumanReadableLocation(hrtLocation); + //koreaPost.setContent(dataInput); + symbol = this.make(settings, dataInput, koreaPost); break; case 79: // Databar-14 Stacked DataBar14 dataBar14s = new DataBar14(); dataBar14s.setMode(Mode.STACKED); - dataBar14s.setContent(dataInput); - symbol = dataBar14s; + //dataBar14s.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBar14s); break; case 80: // Databar-14 Stacked Omnidirectional DataBar14 dataBar14so = new DataBar14(); dataBar14so.setMode(Mode.OMNI); - dataBar14so.setContent(dataInput); - symbol = dataBar14so; + //dataBar14so.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBar14so); break; case 81: // Databar Expanded Stacked DataBarExpanded dataBarES = new DataBarExpanded(); dataBarES.setPreferredColumns(settings.getSymbolColumns()); dataBarES.setStacked(true); - dataBarES.setContent(dataInput); - symbol = dataBarES; + //dataBarES.setContent(dataInput); + symbol = this.make(settings, dataInput, dataBarES); break; case 82: case 83: // Legacy // Planet Postnet planet = new Postnet(); planet.setMode(Postnet.Mode.PLANET); - planet.setContent(dataInput); - symbol = planet; + //planet.setContent(dataInput); + symbol = this.make(settings, dataInput, planet); break; case 84: case 108: @@ -533,37 +695,37 @@ public void process(Settings settings, String dataInput, String outputFileName) microPdf417.setDataType(Symbol.DataType.HIBC); } microPdf417.setReaderInit(settings.isReaderInit()); - microPdf417.setDataColumns(settings.getSymbolColumns()); - microPdf417.setContent(dataInput); - symbol = microPdf417; + //microPdf417.setDataColumns(settings.getSymbolColumns()); + //microPdf417.setContent(dataInput); + symbol = this.make(settings, dataInput, microPdf417); break; case 85: // USPS Intelligent Mail UspsOneCode uspsIMail = new UspsOneCode(); - uspsIMail.setContent(dataInput); - symbol = uspsIMail; + //uspsIMail.setContent(dataInput); + symbol = this.make(settings, dataInput, uspsIMail); break; case 87: // Telepen Numeric Telepen telepenNum = new Telepen(); telepenNum.setMode(Telepen.Mode.NUMERIC); - telepenNum.setHumanReadableLocation(hrtLocation); - telepenNum.setContent(dataInput); - symbol = telepenNum; + //telepenNum.setHumanReadableLocation(hrtLocation); + //telepenNum.setContent(dataInput); + symbol = this.make(settings, dataInput, telepenNum); break; case 89: // ITF-14 Code2Of5 itf14 = new Code2Of5(); itf14.setMode(ToFMode.ITF14); - itf14.setHumanReadableLocation(hrtLocation); - itf14.setContent(dataInput); - symbol = itf14; + //itf14.setHumanReadableLocation(hrtLocation); + //itf14.setContent(dataInput); + symbol = this.make(settings, dataInput, itf14); break; case 90: // Dutch Post KIX Code KixCode kixCode = new KixCode(); - kixCode.setContent(dataInput); - symbol = kixCode; + //kixCode.setContent(dataInput); + symbol = this.make(settings, dataInput, kixCode); break; case 92: case 112: @@ -578,20 +740,20 @@ public void process(Settings settings, String dataInput, String outputFileName) aztecCode.setReaderInit(settings.isReaderInit()); aztecCode.setPreferredEccLevel(settings.getSymbolECC()); aztecCode.setPreferredSize(settings.getSymbolVersion()); - aztecCode.setContent(dataInput); - symbol = aztecCode; + //aztecCode.setContent(dataInput); + symbol = this.make(settings, dataInput, aztecCode); break; case 93: // Code 32 Code32 code32 = new Code32(); - code32.setHumanReadableLocation(hrtLocation); - code32.setContent(dataInput); - symbol = code32; + //code32.setHumanReadableLocation(hrtLocation); + //code32.setContent(dataInput); + symbol = this.make(settings, dataInput, code32); break; case 97: // Micro QR Code MicroQrCode microQrCode = new MicroQrCode(); - switch(settings.getSymbolECC()) { + switch (settings.getSymbolECC()) { case 0: microQrCode.setEccMode(MicroQrCode.EccMode.L); break; @@ -602,68 +764,71 @@ public void process(Settings settings, String dataInput, String outputFileName) microQrCode.setEccMode(MicroQrCode.EccMode.Q); break; } - microQrCode.setPreferredVersion(settings.getSymbolVersion()); - microQrCode.setContent(dataInput); - symbol = microQrCode; + if (settings.getSymbolVersion() > 0) { + microQrCode.setPreferredVersion(settings.getSymbolVersion()); + } + //microQrCode.setContent(dataInput); + symbol = this.make(settings, dataInput, microQrCode); break; case 113: case 52: // Legacy // PZN-8 Pharmazentralnummer pzn = new Pharmazentralnummer(); - pzn.setContent(dataInput); - symbol = pzn; + //pzn.setContent(dataInput); + symbol = this.make(settings, dataInput, pzn); break; case 117: // USPS Intelligent Mail Package UspsPackage uspsPackage = new UspsPackage(); - uspsPackage.setContent(dataInput); - symbol = uspsPackage; + //uspsPackage.setContent(dataInput); + symbol = this.make(settings, dataInput, uspsPackage); break; case 128: // Aztec Runes AztecRune aztecRune = new AztecRune(); - aztecRune.setContent(dataInput); - symbol = aztecRune; + //aztecRune.setContent(dataInput); + symbol = this.make(settings, dataInput, aztecRune); break; case 130: // Composite symbol with EAN linear Composite compositeEan = new Composite(); compositeEan.setSymbology(Composite.LinearEncoding.EAN); compositeEan.setLinearContent(settings.getPrimaryData()); - compositeEan.setContent(dataInput); - symbol = compositeEan; + //compositeEan.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeEan); break; case 131: // Composite with Code 128 linear Composite compositeC128 = new Composite(); compositeC128.setSymbology(Composite.LinearEncoding.CODE_128); compositeC128.setLinearContent(settings.getPrimaryData()); - compositeC128.setContent(dataInput); - symbol = compositeC128; + //compositeC128.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeC128); break; case 132: // Composite with Databar-14 Composite compositeDb14 = new Composite(); compositeDb14.setSymbology(Composite.LinearEncoding.DATABAR_14); compositeDb14.setLinearContent(settings.getPrimaryData()); - compositeDb14.setContent(dataInput); - symbol = compositeDb14; + //compositeDb14.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDb14); + ; break; case 133: // Composite with Databar Limited Composite compositeDbLtd = new Composite(); compositeDbLtd.setSymbology(Composite.LinearEncoding.DATABAR_LIMITED); compositeDbLtd.setLinearContent(settings.getPrimaryData()); - compositeDbLtd.setContent(dataInput); - symbol = compositeDbLtd; + //compositeDbLtd.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDbLtd); break; case 134: // Composite with Databar Extended Composite compositeDbExt = new Composite(); compositeDbExt.setSymbology(Composite.LinearEncoding.DATABAR_EXPANDED); compositeDbExt.setLinearContent(settings.getPrimaryData()); - compositeDbExt.setContent(dataInput); - symbol = compositeDbExt; + //compositeDbExt.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDbExt); break; case 135: // Composite with UPC-A @@ -671,47 +836,47 @@ public void process(Settings settings, String dataInput, String outputFileName) compositeUpcA.setSymbology(Composite.LinearEncoding.UPCA); compositeUpcA.setLinearContent(settings.getPrimaryData()); compositeUpcA.setContent(dataInput); - symbol = compositeUpcA; + //symbol = this.make(settings, dataInput, compositeUpcA); break; case 136: // Composite with UPC-E Composite compositeUpcE = new Composite(); compositeUpcE.setSymbology(Composite.LinearEncoding.UPCE); compositeUpcE.setLinearContent(settings.getPrimaryData()); - compositeUpcE.setContent(dataInput); - symbol = compositeUpcE; + //compositeUpcE.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeUpcE); break; case 137: // Composite with Databar-14 Stacked Composite compositeDb14Stack = new Composite(); compositeDb14Stack.setSymbology(Composite.LinearEncoding.DATABAR_14_STACK); compositeDb14Stack.setLinearContent(settings.getPrimaryData()); - compositeDb14Stack.setContent(dataInput); - symbol = compositeDb14Stack; + //compositeDb14Stack.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDb14Stack); break; case 138: // Composite with Databar-14 Stacked Omnidirectional Composite compositeDb14SO = new Composite(); compositeDb14SO.setSymbology(Composite.LinearEncoding.DATABAR_14_STACK_OMNI); compositeDb14SO.setLinearContent(settings.getPrimaryData()); - compositeDb14SO.setContent(dataInput); - symbol = compositeDb14SO; + //compositeDb14SO.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDb14SO); break; case 139: // Composite with Databar-14 Expanded Stacked Composite compositeDb14ES = new Composite(); compositeDb14ES.setSymbology(Composite.LinearEncoding.DATABAR_EXPANDED_STACK); compositeDb14ES.setLinearContent(settings.getPrimaryData()); - compositeDb14ES.setContent(dataInput); - symbol = compositeDb14ES; + //compositeDb14ES.setContent(dataInput); + symbol = this.make(settings, dataInput, compositeDb14ES); break; case 140: // Channel Code ChannelCode channelCode = new ChannelCode(); channelCode.setPreferredNumberOfChannels(settings.getSymbolColumns()); - channelCode.setHumanReadableLocation(hrtLocation); - channelCode.setContent(dataInput); - symbol = channelCode; + //channelCode.setHumanReadableLocation(hrtLocation); + //channelCode.setContent(dataInput); + symbol = this.make(settings, dataInput, channelCode); break; case 141: // Code One @@ -720,7 +885,7 @@ public void process(Settings settings, String dataInput, String outputFileName) codeOne.setDataType(Symbol.DataType.GS1); } codeOne.setReaderInit(settings.isReaderInit()); - switch(settings.getSymbolVersion()) { + switch (settings.getSymbolVersion()) { case 0: codeOne.setPreferredVersion(CodeOne.Version.NONE); break; @@ -755,8 +920,8 @@ public void process(Settings settings, String dataInput, String outputFileName) codeOne.setPreferredVersion(CodeOne.Version.T); break; } - codeOne.setContent(dataInput); - symbol = codeOne; + //codeOne.setContent(dataInput); + symbol = this.make(settings, dataInput, codeOne); break; case 142: // Grid Matrix @@ -767,66 +932,38 @@ public void process(Settings settings, String dataInput, String outputFileName) gridMatrix.setReaderInit(settings.isReaderInit()); gridMatrix.setPreferredEccLevel(settings.getSymbolECC()); gridMatrix.setPreferredVersion(settings.getSymbolVersion()); - gridMatrix.setContent(dataInput); - symbol = gridMatrix; + //gridMatrix.setContent(dataInput); + symbol = this.make(settings, dataInput, gridMatrix); break; default: // Invalid System.out.println("Invaid barcode type"); - return; + return null; } } catch (OkapiException e) { System.out.printf("Encoding error: %s\n", e.getMessage()); - return; + return null; } - File file = new File(outputFileName); - - try { - int i = file.getName().lastIndexOf('.'); - if (i > 0) { - extension = file.getName().substring(i + 1); - } + return symbol; + } - switch (extension) { - case "png": - case "gif": - case "jpg": - case "bmp": - BufferedImage image = new BufferedImage(symbol.getWidth(), - symbol.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D g2d = image.createGraphics(); - //g2d.setBackground(paper); - g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + private Symbol make(Settings settings, String content, Symbol symbol) { - Java2DRenderer renderer = new Java2DRenderer(g2d, 1, paper, ink); - System.out.printf("MakeBarcode\n"); - renderer.render(symbol); + symbol.setHumanReadableLocation(settings.getHrtPosition()); - try { - ImageIO.write(image, extension, file); - } catch (IOException e) { - System.out.printf("Error outputting to file\n"); - } - break; - case "svg": - SvgRenderer svg = new SvgRenderer(new FileOutputStream(file), 1, paper, ink, true); - svg.render(symbol); - break; - case "eps": - PostScriptRenderer eps = new PostScriptRenderer(new FileOutputStream(file), 1, paper, ink); - eps.render(symbol); - break; - default: - System.out.println("Unsupported output format"); - break; - } + if (settings.getSymbolHeight() > 0) { + symbol.setBarHeight(settings.getSymbolHeight()); + } - } catch (FileNotFoundException e){ - System.out.printf("File Not Found\n"); - } catch (IOException e) { - System.out.printf("Write Error\n"); + if (settings.getSymbolWhiteSpace() > 0) { + symbol.setQuietZoneHorizontal(settings.getSymbolWhiteSpace()); + symbol.setQuietZoneVertical(settings.getSymbolWhiteSpace()); } + + symbol.setContent(content); + + return symbol; } private int eanCalculateVersion(String dataInput) { diff --git a/src/main/java/uk/org/okapibarcode/Settings.java b/src/main/java/uk/org/okapibarcode/Settings.java index 30cf6dcb..aa1f7a76 100644 --- a/src/main/java/uk/org/okapibarcode/Settings.java +++ b/src/main/java/uk/org/okapibarcode/Settings.java @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package uk.org.okapibarcode; import com.beust.jcommander.Parameter; @@ -48,8 +47,8 @@ public class Settings { @Parameter(names = "--height", description = "Height of the symbol in multiples of x-dimension", required = false) private int symbolHeight = 0; -// @Parameter(names = {"-w", "--whitesp"}, description = "Width of whitespace in multiples of x-dimension", required = false) -// private int symbolWhiteSpace = 0; + @Parameter(names = {"-w", "--whitesp"}, description = "Width of whitespace in multiples of x-dimension", required = false) + private int symbolWhiteSpace = 0; // // @Parameter(names = "--border", description = "Width of border in multiples of x-dimension", required = false) // private int symbolBorder = 0; @@ -59,7 +58,6 @@ public class Settings { // // @Parameter(names = "--bind", description = "Add boundary bars", required = false) // private boolean addBinding = false; - @Parameter(names = {"-r", "--reverse"}, description = "Reverse colours (white on black)", required = false) private boolean reverseColour = false; @@ -69,14 +67,13 @@ public class Settings { @Parameter(names = "--bg", description = "Specify a background (paper) colour", required = false) private String backgroundColour = "FFFFFF"; + // Set Default 1 @Parameter(names = "--scale", description = "Adjust size of output image", required = false) - private int symbolScale = 0; + private int symbolScale = 1; // --directpng, --directeps, --directsvg, --dump - // @Parameter(names = "--rotate", description = "Rotate symbol", required = false) // private int rotationAngle = 0; - @Parameter(names = "--cols", description = "Number of columns in PDF417", required = false) private int symbolColumns = 0; @@ -104,14 +101,14 @@ public class Settings { @Parameter(names = "--textabove", description = "Place human readable text above symbol", required = false) private boolean superHrt = false; + // Set Default True @Parameter(names = "--square", description = "Force Data Matrix symbols to be square", required = false) - private boolean makeSquare = false; + private boolean makeSquare = true; @Parameter(names = "--init", description = "Add reader initialisation code", required = false) private boolean addReaderInit = false; // --smalltext - @Parameter(names = "--batch", description = "Treat each line of input as a separate data set", required = false) private boolean batchMode = false; @@ -170,14 +167,12 @@ public int getSymbolHeight() { // public int getSymbolWhiteSpace() { // return symbolWhiteSpace; // } - // /** // * @return the symbolBorder // */ // public int getSymbolBorder() { // return symbolBorder; // } - // /** // * @return the addBox // */ @@ -191,7 +186,6 @@ public int getSymbolHeight() { // public boolean isAddBinding() { // return addBinding; // } - /** * @return the reverseColour */ @@ -246,7 +240,6 @@ public int getSymbolScale() { // public int getRotationAngle() { // return rotationAngle; // } - /** * @return the symbolColumns */ @@ -302,11 +295,11 @@ public boolean isDataBinaryMode() { public HumanReadableLocation getHrtPosition() { HumanReadableLocation temp = HumanReadableLocation.BOTTOM; - if(superHrt) { + if (superHrt) { temp = HumanReadableLocation.TOP; } - if(supressHrt) { + if (supressHrt) { temp = HumanReadableLocation.NONE; } @@ -334,4 +327,60 @@ public boolean isBatchMode() { return batchMode; } -} \ No newline at end of file + public int getSymbolWhiteSpace() { + return symbolWhiteSpace; + } + + public void setSymbolType(int symbolType) { + this.symbolType = symbolType; + } + + public void setSymbolHeight(int symbolHeight) { + this.symbolHeight = symbolHeight; + } + + public void setSymbolWhiteSpace(int symbolWhiteSpace) { + this.symbolWhiteSpace = symbolWhiteSpace; + } + + public void setSymbolScale(int symbolScale) { + this.symbolScale = symbolScale; + } + + public void setSymbolColumns(int symbolColumns) { + this.symbolColumns = symbolColumns; + } + + public void setSymbolVersion(int symbolVersion) { + this.symbolVersion = symbolVersion; + } + + public void setSymbolECC(int symbolECC) { + this.symbolECC = symbolECC; + } + + public void setDataGs1Mode(boolean dataGs1Mode) { + this.dataGs1Mode = dataGs1Mode; + } + + public boolean isSupressHrt() { + return supressHrt; + } + + public void setSupressHrt(boolean supressHrt) { + this.supressHrt = supressHrt; + } + + public boolean isSuperHrt() { + return superHrt; + } + + public void setSuperHrt(boolean superHrt) { + this.superHrt = superHrt; + } + + public void setMakeSquare(boolean makeSquare) { + this.makeSquare = makeSquare; + } + +} diff --git a/src/main/java/uk/org/okapibarcode/SymbolFormat.java b/src/main/java/uk/org/okapibarcode/SymbolFormat.java new file mode 100644 index 00000000..ae3b31e0 --- /dev/null +++ b/src/main/java/uk/org/okapibarcode/SymbolFormat.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 Robin Stuart + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.org.okapibarcode; + +/** + * Barcode Format. + * + * @author anyonetff + */ +public enum SymbolFormat { + + svg("svg", "image/svg+xml"), + eps("eps", "application/postscript"), + png("png", "image/png"), + jpg("jpg", "image/jpeg"), + gif("gif", "image/gif"), + bmp("bmp", "image/bmp"); + + private final String value; + + private final String contentType; + + private SymbolFormat(String value, String mime) { + this.value = value; + this.contentType = mime; + } + + public String getValue() { + return value; + } + + public String getContentType() { + return contentType; + } + +} diff --git a/src/main/java/uk/org/okapibarcode/SymbolType.java b/src/main/java/uk/org/okapibarcode/SymbolType.java new file mode 100644 index 00000000..c737528a --- /dev/null +++ b/src/main/java/uk/org/okapibarcode/SymbolType.java @@ -0,0 +1,246 @@ +/* + * Copyright 2015 Robin Stuart + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.org.okapibarcode; + +/** + * Barcode Type. + * + * @author anyonetff + */ +public enum SymbolType { + + code11(1, 1), + c25matrix(2, 2), + c25inter(3, 1), + c25iata(4, 1), + c25logic(6, 1), + c25ind(7, 1), + code39(8, 1), + excode39(9, 1), + /** + * EAN (Including EAN-8 and EAN-13). + */ + eanx(13, 1), + /** + * EAN13 + check digit. + */ + eanx_chk(14, 1), + /** + * EAN128/GS1-128. + *

+ * EAN-128,我国所推行的128码,根据EAN/UCC-128码定义标准将资料转变成条码符号,并采用128码逻辑,具有完整性、紧密性、连结性及高可靠度的特性。辨识范围涵盖生产过程中一些补充性质且易变动之资讯,如生产日期、批号、计量等。可应用于货运栈版标签、携带式资料库、连续性资料段、流通配送标签等。

+ *

+ * GS1 (Globe standard + * 1)是1973年由美国统一代码委员会建立的组织,该系统拥有全球跨行业的产品、运输单元、资产、位置和服务的标识标准体系和信息交换标准体系,使产品在全世界都能够被扫描和识读 + * 。

+ */ + ean128(16, 1), + codabar(18, 1), + /** + * Code 128. + *

+ * CODE128码是广泛应用在企业内部管理、生产流程、物流控制系统方面的条码码制,由于其优良的特性在管理信息系统的设计中被广泛使用,CODE128码是应用最广泛的条码码制之一。

+ *

+ * CODE128码是1981年引入的一种高密度条码,CODE128 码可表示从 ASCII 0 到ASCII 127 + * 共128个字符,故称128码。其中包含了数字、字母和符号字符。

+ */ + code128(20, 1), + dpleit(21, 1), + dpident(22, 1), + code16k(23, 1), + code49(24, 1), + code93(25, 1), + flat(28, 1), + rss14(29, 1), + rss_ltd(30, 1), + /** + * GS1 DataBar Expanded Omnidirectional. 扩展型GS1条码 + */ + rss_exp(31, 2), + telepen(32, 1), + upca(34, 1), + upca_chk(35, 1), + upce(37, 1), + upce_chk(38, 1), + postnet(40, 1), + msi_plessey(47, 1), + fim(49, 1), + logmars(50, 1), + pharma(51, 1), + pzn(521, 1), + pharma_two(53, 1), + /** + * PDF417. + *

+ * PDF417条码是一种高密度、高信息含量的便携式数据文件,是实现证件及卡片等大容量、高可靠性信息自动存储、携带并可用机器自动识读的理想手段。

+ */ + pdf417(55, 2), + pdf417trunc(56, 2), + maxicode(57, 2), + /** + * QR Code. + *

+ * QR + * Code码,是由Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大、可靠性高、可表示汉字及图象多种文字信息、保密防伪性强等优点。

+ */ + qrcode(58, 2), + code128b(60, 1), + auspost(63, 1), + ausreply(66, 1), + ausroute(67, 1), + ausredirect(68, 1), + /** + * ISBN (EAN-13 with verification stage). + *

+ * 全球书号及商品编码,商品包装上印刷的就是这个码。

+ */ + isbnx(69, 1), + rm4scc(70, 1), + /** + * Data Matrix. + *

+ * Datamatrix是二维码的一个成员,与1989年由美国国际资料公司发明,广泛用于商品的防伪、统筹标识。

+ */ + datamatrix(71, 2), + /** + * EAN14. + *

+ * 这个条码就是GS1的01段,报文为13位数值,条码生成时自动补1位。输出样式与EAN128仅含01段时一致

+ */ + ean14(72, 1), + vin(73, 1), + codablockf(74, 1), + nve18(75, 1), + japanpost(76, 1), + koreapost(77, 1), + /** + * GS1 DataBar Stacked. + *

+ * DataBar(前称RSS)是一种条码符号,可用于识别小件物品,比起目前的EAN/UPC条形码可携带更多信息。

+ *

+ * Stacked代表堆叠模式,可以压缩打印尺寸

+ */ + rss14stack(79, 2), + /** + * GS1 DataBar Stacked Omnidirection. + *

+ * Omnidirection代表全向扫描模式

+ */ + rss14stack_omni(80, 2), + /** + * GS1 DataBar Expanded Stacked Omnidirectional. + *

+ * 全向识别模式

+ */ + rss_expstack(81, 2), + planet(82, 1), + micropdf417(84, 1), + onecode(85, 2), + plessey(86, 1), + telepen_num(87, 1), + itf14(89, 1), + kix(90, 1), + /** + * Aztec Code. + *

+ * Aztec Code是1995年,由Hand HeldProducts公司的Dr. Andrew + * Longacre设计。它是一种高容量的二维条形码格式。它可以对ASCII和扩展ASCII码进行编码。当使用最高容量和25%的纠错级别的時候,Aztec可以对3000个字符或者3750个数字进行编码。

+ */ + aztec(92, 2), + daft(93, 1), + /** + * Micro QR Code. + *

+ * QR Code的一种,正方形,只有标准模式的四分之一尺寸。

+ */ + microqr(97, 2), + hibc_128(98, 1), + hibc_39(99, 1), + hibc_dm(102, 2), + hibc_qr(104, 2), + hibc_pdf(106, 2), + hibc_micpdf(108, 2), + hibc_blockf(110, 2), + hibc_aztec(112, 2), + /** + * DotCode. + *

+ * DotCode是一种大小可变、形状多样、识别效率极高的矩阵条码符号,设计的初衷为快速标记应用,在生产线不间断快速读取时尤见成效。

+ */ + dotcode(115, 2), + /** + * Han Xin (Chinese Sensible) Code. + *

+ * 汉信码由中国物品编码中心完成的国家“十五”重大科技专项——《二维条码新码制开发与关键技术标准研究》取得了突破性成果,研究成果包括:研究开发汉信码新码制、开发汉信码生成软件、开发汉信码识读技术及算法、汉信码硬件设备研发、汉信码装备研制、汉信码通讯技术研发以及编制汉信码国家标准等的系列工作。

+ */ + hanxin(116, 2), + mailmark(121, 1), + azrune(128, 1), + code32(129, 1), + eanx_cc(130, 1), + ean128_cc(131, 1), + rss14_cc(132, 1), + rss_ltd_cc(133, 1), + rss_exp_cc(134, 1), + upca_cc(135, 1), + upce_cc(136, 1), + rss14stack_cc(137, 1), + rss14_omni_cc(138, 1), + rss_expstack_cc(139, 1), + channel(140, 1), + /** + * Code One. + *

+ * Code One是一种用成像设备识别的矩阵式二维条码。Code One符号中包含可由快速性线性探测器识别的识别图案。每一模块的宽和高的尺寸为X。 + *

+ */ + codeone(141, 2), + gridmatrix(142, 2), + upnqr(143, 2), + ultra(144, 1), + /** + * Rectangular Micro QR Code. + *

+ * QR Code的一种,长方形,只有标准模式的一半尺寸。

+ */ + rmqr(145, 2); + + /** + * Zint Type Value. + */ + private final int value; + + /** + * 1D or 2D. + */ + private final int dimension; + + private SymbolType(int value, int dimension) { + this.value = value; + this.dimension = dimension; + } + + /** + * @return the value + */ + public int getValue() { + return value; + } + + public int getDimension() { + return dimension; + } +} diff --git a/src/test/java/uk/org/okapibarcode/MakeBarcodeTest.java b/src/test/java/uk/org/okapibarcode/MakeBarcodeTest.java new file mode 100644 index 00000000..9e596035 --- /dev/null +++ b/src/test/java/uk/org/okapibarcode/MakeBarcodeTest.java @@ -0,0 +1,229 @@ +/* + * Copyright 2018 Daniel Gredler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package uk.org.okapibarcode; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +/** + * + * @author tangfeifei + */ +public class MakeBarcodeTest { + + private final String data_na = "1234567890ABC"; + + private final String data_ean13 = "6901236348338"; + + private final String data_ean13_ = "690123634833"; + + private final String data_gs1 = "[01]06901236348338[10]batch666[11]231127[21]SERIAL999(30)5(3102)666999"; + + private final String data_gs1_ = "(01)06901236348338(10)batch666(11)231127(21)SERIAL999(30)5(3102)666999"; + + private final String data_unicode = "12345上山打老虎"; + + private final String svg_result = " Date: Wed, 11 Sep 2024 11:42:20 +0800 Subject: [PATCH 2/2] improvement: MakeBarcode easy to use as Java API 1. Rewrite process to fix some default value problem 2. Add processToStream to create OutputStream 3. Add processToByte to create byte[] 4. Add setters to Settings, add whiteSpace Support 5. Add SymbolType, SymbolFormat 6. Add WhiteSpace Support, Add BarHeight Support problem found: 1. the EAN-8, EAN13, data length validator is not smart --- src/main/java/uk/org/okapibarcode/MakeBarcode.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/uk/org/okapibarcode/MakeBarcode.java b/src/main/java/uk/org/okapibarcode/MakeBarcode.java index 86647228..6c27951e 100644 --- a/src/main/java/uk/org/okapibarcode/MakeBarcode.java +++ b/src/main/java/uk/org/okapibarcode/MakeBarcode.java @@ -87,14 +87,21 @@ public class MakeBarcode { public byte[] processToByte(Settings settings, String dataInput, String format) { + byte[] data = null; ByteArrayOutputStream out = (ByteArrayOutputStream) this.processToStream(settings, dataInput, format); if (out != null && out.size() > 0) { - return out.toByteArray(); + try { + data = out.toByteArray(); + + out.close(); + } catch (IOException ex) { + System.out.printf("Write Error\n"); + } } - return null; + return data; } public OutputStream processToStream(Settings settings, String dataInput, String format) {