Commit a9e247b2 authored by Volker Krause's avatar Volker Krause
Browse files

Extract Aztec codes from DB PDF train tickets too

So far we only try to preserve them for the app, which in itself is tricky
due to them containing binary data, passed through a string-based API...
parent 4f56181d
......@@ -111,6 +111,7 @@ class BarcodeJsApi : public QObject
public:
Q_INVOKABLE QString decodePdf417(const QVariant &img) const;
Q_INVOKABLE QString decodeAztec(const QVariant &img) const;
Q_INVOKABLE QString decodeAztecBinary(const QVariant &img) const;
};
QString BarcodeJsApi::decodePdf417(const QVariant &img) const
......@@ -129,6 +130,16 @@ QString BarcodeJsApi::decodeAztec(const QVariant &img) const
return {};
}
QString BarcodeJsApi::decodeAztecBinary(const QVariant &img) const
{
if (img.userType() == qMetaTypeId<PdfImage>()) {
const auto b = BarcodeDecoder::decodeAztecBinary(img.value<PdfImage>().image());
// ugly, but this at least makes js preserve \0 bytes it seems...
return QString::fromLatin1(b.constData(), b.size());
}
return {};
}
class ContextObject : public QObject
{
Q_OBJECT
......
......@@ -21,25 +21,13 @@ function isHeaderOrFooter(line) {
return line.search(/(Ihre Reiseverbindung|Wichtige Nutzungshinweise|Hinweise:|Seite \d \/ \d)/) >= 0;
}
function createSeat(res)
{
if (!res.reservedTicket)
res.reservedTicket = JsonLd.newObject("Ticket");
if (!res.reservedTicket.ticketedSeat)
res.reservedTicket.ticketedSeat = JsonLd.newObject("Seat");
}
function parseSeat(res, text) {
var coach = text.match(/Wg. (\d+)/);
if (coach) {
createSeat(res);
if (coach)
res.reservedTicket.ticketedSeat.seatSection = coach[1];
}
var seat = text.match(/Pl. (\d+)/);
if (seat) {
createSeat(res);
if (seat)
res.reservedTicket.ticketedSeat.seatNumber = seat[1];
}
}
function parseDeparture(res, line, year, compact) {
......@@ -97,6 +85,8 @@ function parseLegs(text, year, compact) {
while (depIdx < lines.length) {
var res = JsonLd.newObject("TrainReservation");
res.reservationFor = JsonLd.newObject("TrainTrip");
res.reservedTicket = JsonLd.newObject("Ticket");
res.reservedTicket.ticketedSeat = JsonLd.newObject("Seat");
// stop when reaching the footer or the next itinerary header
if (isHeaderOrFooter(lines[depIdx]))
......@@ -116,7 +106,7 @@ function parseLegs(text, year, compact) {
return reservations;
}
function main(text) {
function parseText(text) {
var reservations = new Array();
var pos = 0;
while (true) {
......@@ -152,3 +142,19 @@ function main(text) {
reservations[i].reservationNumber = bookingRef[1];
return reservations;
}
function parsePdf(pdf) {
// try to find the UIC918.3 barcode
var images = pdf.pages[0].imagesInRect(0.6, 0, 1, 1);
var barcode = "";
for (var i = 0; i < images.length && barcode == ""; ++i) {
if (images[i].width == images[i].height)
barcode = Barcode.decodeAztecBinary(images[i]);
}
var reservations = parseText(pdf.text);
for (var i = 0; i < reservations.length && barcode != ""; ++i) {
reservations[i].reservedTicket.ticketToken = "aztecCode:" + barcode;
}
return reservations;
}
{
"filter": [
{ "header": "From", "match": "buchungsbestaetigung@bahn.de" }
],
"script": "deutschebahn.js"
}
[
{
"type": "pdf",
"filter": [ { "header": "From", "match": "buchungsbestaetigung@bahn.de" } ],
"script": "deutschebahn.js",
"function": "parsePdf"
},
{
"type": "text",
"filter": [ { "header": "From", "match": "buchungsbestaetigung@bahn.de" } ],
"script": "deutschebahn.js",
"function": "parseText"
}
]
......@@ -88,7 +88,6 @@ function parsePdf(pdf) {
var barcode = "";
var images = page.imagesInRect(0.75, 0, 1, 0.75);
console.log(images);
for (var j = 0; j < images.length && barcode == ""; ++j) {
if (Math.abs(images[j].width - images[j].height) < 10) // almost square
barcode = Barcode.decodeAztec(images[j]);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment