pbock / ticket-parser

Parse the Aztec code in Deutsche Bahn's train tickets
MIT License
95 stars 4 forks source link

how to test this app? #1

Closed htrxuan closed 6 years ago

htrxuan commented 6 years ago

Hi,

could you give me requirement for this app? how could I test this?

pbock commented 6 years ago

You need to extract the data from the Aztec code on your ticket and then pass them into TicketParser.parse(). How you do that is up to you, you just need a parser that supports binary data. I've had success with zxing and built a Node.js wrapper around it, ZebraCrossing. It takes an image as input and tries to decode any 1d-/2d-barcodes in it.

If you have the original PDF that contained the ticket, then there's a single-purpose module for extracting the Aztec code from that too: ot-aztec-extract.

It’s been over a year since I last touched this code, but if you have the original ticket PDFs, you should be able to stick these three modules together quite neatly like this:

const extractAztec = require('ot-aztec-extract');
const zxing = require('zebra-crossing');
const TicketParser = require('ticket-parser');
const iconv = require('iconv-lite');
const fs = require('fs');

function read(pdfPath) {
  return extractAztec(fs.readFileSync(pdfPath))
  .then(png => zxing.read(png, { pureBarcode: true }))
  .then(data => iconv.encode(data.raw, 'latin1'))
  .then(TicketParser.parse)
}

I’ve been meaning to combine that into a proper npm module for ages and never got around to it. (I don’t remember why iconv is in there, but it’s something to do with the fact that the data is binary.)