androidxmlzxingpdf417

Android ZXing Generate PDF417 Barcode on large XML dataset


I have a fairly large dataset of XML, for example:

  <book id=“…” serial_no="8472385" height="210" width="600" books="1">
  <content>
     <barcode encoding="code128c" data1="22" data2="50" description="Barcode 1”>…</barcode>
     <barcode encoding="code128c" data1="510" data2="75" description="Barcode 2”>…</barcode>
     <chapter sections="8" data1="214" data2="12" description=“Info 1”>…</chapter>
     <chapter sections="11" data1="88" data2="63" description=“Info 2”>…</chapter>
     <chapter sections="16" data1="88" data2="42" description=“Info 3”>…</chapter>
     <chapter sections="13" data1="88" data2="83" description=“Info 4”>…</chapter>
     <chapter sections="11" data1="88" data2="105" description=“Info 5“>Mon, 24 Aug 2015, 8:30am</chapter>
     <chapter sections="18" data1="286" data2="120" description=“Info 6”>…</chapter>
     <chapter sections="19" data1="466" data2="12" description=“Info 7“>…</chapter>
     <chapter sections="11" data1="496" data2="26" description=“Info 8“>…</chapter>
     <chapter sections="9" data1="531" data2="28" description=“Info 9“>…</chapter>
     <chapter sections="11" data1="497" data2="12" description=“Info 10“>8:30am</chapter>
     <chapter sections="12" data1="88" data2="147" description=“Info 11“>…</chapter>
     <chapter sections="10" data1="88" data2="125" description=“Info 12”>…</chapter>
     <chapter sections="13" data1="338" data2="158" description=“Info 13”>…</chapter>
     <chapter sections="9" data1="81" data2="192" description="book Id”>…</chapter>
     <chapter sections="8" data1="394" data2="192" description="book Id”>…</chapter>
     <chapter sections="8" data1="140" data2="12" description=“Info 14”>…</chapter>
     <chapter sections="9" data1="132" data2="192" description="" />
     <chapter sections="10" data1="88" data2="177" description=“Info 15“ />
     <chapter sections="12" data1="88" data2="163" description="Some sample data”>…</chapter>
     <chapter sections="8" data1="286" data2="192" description="Time Stamp">2015-08-19 10:00:55</chapter>
     <logo data1="402" data2="37" description="Logo">cache/images/logoSample.pcx</logo>
     <logo_gif data1="402" data2="37" description="Logo">cache/images/book_….gif</logo_gif>
     <chapter sections="12" data1="446" data2="187" description="Price">$100.00</chapter>
  </content>
  </book>

And I want to encode this into a PDF417 image and display it on an android device device. I have imported the ZXing framework and I am currently attempting something like this:

    ImageView book = (ImageView) mRootView.findViewById(R.id.book_image);
    Display display = getActivity().getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    int height = size.x / 2;
    String raw = bookXmlString.replace("<","&lt;").replace(">","&gt;");

    MultiFormatWriter writer = new MultiFormatWriter();
    try {
        Log.e("PDF_417", String.format("w: %d, h: %d, d: %s", width, height, raw));
        BitMatrix bm = writer.encode(raw, BarcodeFormat.PDF_417, width, height);
        Bitmap ImageBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        for (int i = 0; i < width; i++) {//width
            for (int j = 0; j < height; j++) {//height
                ImageBitmap.setPixel(i, j, bm.get(i, j) ? Color.BLACK : Color.WHITE);
            }
        }            
        book.setImageBitmap(ImageBitmap);
    } catch (WriterException e) {
        Log.e("PDF_417", e.getMessage());
    }

But I keep on getting an error like this:

Unable to fit message in columns

I keep on crashing sites like these: - http://www.racoindustries.com/barcodegenerator/2d/pdf417.aspx - http://generator.onbarcode.com/online-pdf417-barcode-generator.aspx

When I encode less data I get the PDF417 without problems. I also have no idea how to set the error correction level.

I have also implemented the methods here to try and find out how best to set the width and height, with no real luck: In a PDF417 barcode where number of columns are fixed, how would I calculate the number of rows required for some text?

In summary I am asking:

  1. How to encode this data as a PDF417 barcode, given that the string length may vary?
  2. How do I set the error correction level?
  3. What is the best way to set the width / height AND the number of rows / columns?

Solution

  • Your sample is 2300+ characters long, which exceeds the limit for text - 2 characters x 900 codewords = 1800 text characters. See https://en.wikipedia.org/wiki/PDF417

    You may want to consider using a less verbose format than XML for data transfer.

    Just additional info - you can search for 'PDF417 stitching' if you need to encode large data sets. All this means is you'll have multiple PDF barcodes to hold a document and requires the reader to put it together.