PDF417Reader.cpp Example File
appdemos/qtws/QZXing/zxing/zxing/pdf417/PDF417Reader.cpp
#include <zxing/pdf417/PDF417Reader.h>
#include <zxing/pdf417/detector/Detector.h>
using zxing::Ref;
using zxing::Result;
using zxing::BitMatrix;
using zxing::pdf417::PDF417Reader;
using zxing::pdf417::detector::Detector;
using zxing::ArrayRef;
using zxing::BinaryBitmap;
using zxing::DecodeHints;
Ref<Result> PDF417Reader::decode(Ref<BinaryBitmap> image, DecodeHints hints) {
Ref<DecoderResult> decoderResult;
Detector detector(image);
Ref<DetectorResult> detectorResult = detector.detect(hints);
ArrayRef< Ref<ResultPoint> > points(detectorResult->getPoints());
if (!hints.isEmpty()) {
Ref<ResultPointCallback> rpcb = hints.getResultPointCallback();
if (rpcb != NULL) {
for (int i = 0; i < points->size(); i++) {
rpcb->foundPossibleResultPoint(*points[i]);
}
}
}
decoderResult = decoder.decode(detectorResult->getBits(),hints);
Ref<Result> r(new Result(decoderResult->getText(), decoderResult->getRawBytes(), points,
BarcodeFormat::PDF_417));
return r;
}
void PDF417Reader::reset() {
}
Ref<BitMatrix> PDF417Reader::extractPureBits(Ref<BitMatrix> image) {
ArrayRef<int> leftTopBlack = image->getTopLeftOnBit();
ArrayRef<int> rightBottomBlack = image->getBottomRightOnBit();
int nModuleSize = moduleSize(leftTopBlack, image);
int top = leftTopBlack[1];
int bottom = rightBottomBlack[1];
int left = findPatternStart(leftTopBlack[0], top, image);
int right = findPatternEnd(leftTopBlack[0], top, image);
int matrixWidth = (right - left + 1) / nModuleSize;
int matrixHeight = (bottom - top + 1) / nModuleSize;
if (matrixWidth <= 0 || matrixHeight <= 0) {
throw NotFoundException("PDF417Reader::extractPureBits: no matrix found!");
}
int nudge = nModuleSize >> 1;
top += nudge;
left += nudge;
Ref<BitMatrix> bits(new BitMatrix(matrixWidth, matrixHeight));
for (int y = 0; y < matrixHeight; y++) {
int iOffset = top + y * nModuleSize;
for (int x = 0; x < matrixWidth; x++) {
if (image->get(left + x * nModuleSize, iOffset)) {
bits->set(x, y);
}
}
}
return bits;
}
int PDF417Reader::moduleSize(ArrayRef<int> leftTopBlack, Ref<BitMatrix> image) {
int x = leftTopBlack[0];
int y = leftTopBlack[1];
int width = image->getWidth();
while (x < width && image->get(x, y)) {
x++;
}
if (x == width) {
throw NotFoundException("PDF417Reader::moduleSize: not found!");
}
int moduleSize = (int)(((unsigned)(x - leftTopBlack[0])) >> 3);
if (moduleSize == 0) {
throw NotFoundException("PDF417Reader::moduleSize: is zero!");
}
return moduleSize;
}
int PDF417Reader::findPatternStart(int x, int y, Ref<BitMatrix> image) {
int width = image->getWidth();
int start = x;
int transitions = 0;
bool black = true;
while (start < width - 1 && transitions < 8) {
start++;
bool newBlack = image->get(start, y);
if (black != newBlack) {
transitions++;
}
black = newBlack;
}
if (start == width - 1) {
throw NotFoundException("PDF417Reader::findPatternStart: no pattern start found!");
}
return start;
}
int PDF417Reader::findPatternEnd(int x, int y, Ref<BitMatrix> image) {
int width = image->getWidth();
int end = width - 1;
while (end > x && !image->get(end, y)) {
end--;
}
int transitions = 0;
bool black = true;
while (end > x && transitions < 9) {
end--;
bool newBlack = image->get(end, y);
if (black != newBlack) {
transitions++;
}
black = newBlack;
}
if (end == x) {
throw NotFoundException("PDF417Reader::findPatternEnd: no pattern end found!");
}
return end;
}