简体   繁体   中英

Barcode4J pdf417 module is giving error

I am trying to convert binary data to pdf417 barcode using pdf417bean class of barcode4J. But it is giving me following error

java.lang.IllegalArgumentException: Non-encodable character detected: Í (Unicode: 205)
    org.krysalis.barcode4j.impl.pdf417.PDF417HighLevelEncoder.determineConsecutiveBinaryCount(PDF417HighLevelEncoder.java:468)
    org.krysalis.barcode4j.impl.pdf417.PDF417HighLevelEncoder.encodeHighLevel(PDF417HighLevelEncoder.java:108)
    org.krysalis.barcode4j.impl.pdf417.PDF417LogicImpl.generateBarcodeLogic(PDF417LogicImpl.java:193)
    org.krysalis.barcode4j.impl.pdf417.PDF417Bean.generateBarcode(PDF417Bean.java:79)
    com.pb.iop.labelgen.impl.BarcodeService.generateBarcodecodePDF417(BarcodeService.java:244)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:165)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:276)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1171)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1103)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1053)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1043)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:406)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:477)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

Here's a part of codebase I am using

        PDF417Bean bean = new PDF417Bean();

        bean.doQuietZone(true);
        bean.setModuleWidth(UnitConv.in2mm(moduleWidthInches));
        bean.setQuietZone(UnitConv.in2mm(quietZoneInches));
        bean.setBarHeight(height);

        boolean antiAlias = false;
        int orientation = 0;

        BitmapCanvasProvider canvas = new BitmapCanvasProvider(
                dpi, BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);

        indiciaData = indiciaData.replaceAll("\\s+", "");
        byte[] binaryData = Base64.decodeBase64(indiciaData.getBytes());
        String base64DecodedMsg = StringUtils.newStringIso8859_1(binaryData);

        bean.generateBarcode(canvas, base64DecodedMsg);
        canvas.finish();


        String mime = MimeTypes.MIME_BMP;
        out = new ByteArrayOutputStream();

        final BitmapEncoder encoder = BitmapEncoderRegistry.getInstance(mime);
        encoder.encode(canvas.getBufferedImage(), out, mime, dpi);

I am using apache commons for conversion from base 64

Jeremias pointed out the solution, just for the simplicity use:

new String("your string goes here written in UTF-8".getBytes("UTF-8"), "Cp437")

for example:

new String("ČĆŽŠĐ čćžšđ or Í".getBytes("UTF-8"), "Cp437")

you can check out barcode decoding online: http://online-barcode-reader.inliteresearch.com/default.aspx

It works fine for Croatian diacritic characters, and it sohoud work for any UTF-8 character. Srdačno.

You need to use new String(binaryData, "Cp437") instead of ISO-8859-1. The page on PDF417 says to use that encoding. Other encodings are not currently possible. Unfortunately, the "Í" is not in Cp437 so can't be used. If you download Barcode4J from CVS HEAD and compile it yourself, you can use RFC 2397 data URLs to use binary data: PDF417 description for development version .

I tried changing Barcode4J PDF417HighLevelEncoder class and changed encoding to ISO-8859-1 in place of Cp437. I generated a handful of barcodes and they are getting scanned correctly. Looks like it can be changed to support ISO 8859-1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM