diff --git a/src/qrcode/cpp_port/mod.rs b/src/qrcode/cpp_port/mod.rs index 2893c1b..a604bc6 100644 --- a/src/qrcode/cpp_port/mod.rs +++ b/src/qrcode/cpp_port/mod.rs @@ -1,5 +1,8 @@ mod data_mask; pub mod decoder; pub mod detector; +mod qr_cpp_reader; + +pub use qr_cpp_reader::QrReader; mod bitmatrix_parser; diff --git a/src/qrcode/cpp_port/qr_cpp_reader.rs b/src/qrcode/cpp_port/qr_cpp_reader.rs new file mode 100644 index 0000000..f912693 --- /dev/null +++ b/src/qrcode/cpp_port/qr_cpp_reader.rs @@ -0,0 +1,272 @@ +/* +* Copyright 2016 Nu-book Inc. +* Copyright 2016 ZXing authors +* Copyright 2022 Axel Waggershauser +*/ +// SPDX-License-Identifier: Apache-2.0 + +// #include "QRReader.h" + +// #include "BinaryBitmap.h" +// #include "ConcentricFinder.h" +// #include "DecodeHints.h" +// #include "DecoderResult.h" +// #include "DetectorResult.h" +// #include "LogMatrix.h" +// #include "QRDecoder.h" +// #include "QRDetector.h" +// #include "Result.h" + +// #include + +// namespace ZXing::QRCode { + +// Result Reader::decode(const BinaryBitmap& image) const +// { +// #if 1 +// if (!_hints.isPure()) +// return FirstOrDefault(decode(image, 1)); +// #endif + +// auto binImg = image.getBitMatrix(); +// if (binImg == nullptr) +// return {}; + +// DetectorResult detectorResult; +// if (_hints.hasFormat(BarcodeFormat::QRCode)) +// detectorResult = DetectPureQR(*binImg); +// if (_hints.hasFormat(BarcodeFormat::MicroQRCode) && !detectorResult.isValid()) +// detectorResult = DetectPureMQR(*binImg); + +// if (!detectorResult.isValid()) +// return {}; + +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); + +// return Result(std::move(decoderResult), std::move(position), +// detectorResult.bits().width() < 21 ? BarcodeFormat::MicroQRCode : BarcodeFormat::QRCode); +// } + +// void logFPSet(const FinderPatternSet& fps [[maybe_unused]]) +// { +// #ifdef PRINT_DEBUG +// auto drawLine = [](PointF a, PointF b) { +// int steps = maxAbsComponent(b - a); +// PointF dir = bresenhamDirection(PointF(b - a)); +// for (int i = 0; i < steps; ++i) +// log(centered(a + i * dir), 2); +// }; + +// drawLine(fps.bl, fps.tl); +// drawLine(fps.tl, fps.tr); +// drawLine(fps.tr, fps.bl); +// #endif +// } + +// Results Reader::decode(const BinaryBitmap& image, int maxSymbols) const +// { +// auto binImg = image.getBitMatrix(); +// if (binImg == nullptr) +// return {}; + +// #ifdef PRINT_DEBUG +// LogMatrixWriter lmw(log, *binImg, 5, "qr-log.pnm"); +// #endif + +// auto allFPs = FindFinderPatterns(*binImg, _hints.tryHarder()); + +// #ifdef PRINT_DEBUG +// printf("allFPs: %d\n", Size(allFPs)); +// #endif + +// std::vector usedFPs; +// Results results; + +// if (_hints.hasFormat(BarcodeFormat::QRCode)) { +// auto allFPSets = GenerateFinderPatternSets(allFPs); +// for (const auto& fpSet : allFPSets) { +// if (Contains(usedFPs, fpSet.bl) || Contains(usedFPs, fpSet.tl) || Contains(usedFPs, fpSet.tr)) +// continue; + +// logFPSet(fpSet); + +// auto detectorResult = SampleQR(*binImg, fpSet); +// if (detectorResult.isValid()) { +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); +// if (decoderResult.isValid()) { +// usedFPs.push_back(fpSet.bl); +// usedFPs.push_back(fpSet.tl); +// usedFPs.push_back(fpSet.tr); +// } +// if (decoderResult.isValid(_hints.returnErrors())) { +// results.emplace_back(std::move(decoderResult), std::move(position), BarcodeFormat::QRCode); +// if (maxSymbols && Size(results) == maxSymbols) +// break; +// } +// } +// } +// } + +// if (_hints.hasFormat(BarcodeFormat::MicroQRCode) && !(maxSymbols && Size(results) == maxSymbols)) { +// for (const auto& fp : allFPs) { +// if (Contains(usedFPs, fp)) +// continue; + +// auto detectorResult = SampleMQR(*binImg, fp); +// if (detectorResult.isValid()) { +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); +// if (decoderResult.isValid(_hints.returnErrors())) { +// results.emplace_back(std::move(decoderResult), std::move(position), BarcodeFormat::MicroQRCode); +// if (maxSymbols && Size(results) == maxSymbols) +// break; +// } + +// } +// } +// } + +// return results; +// } + +// } // namespace ZXing::QRCode + + +use crate::{Reader, DecodingHintDictionary, multi::MultipleBarcodeReader}; + +pub struct QrReader{} + +impl Reader for QrReader { + fn decode(&mut self, image: &mut crate::BinaryBitmap) -> crate::common::Result { + self.decode_with_hints(image, &DecodingHintDictionary::new()) + } + + fn decode_with_hints( + &mut self, + image: &mut crate::BinaryBitmap, + hints: &crate::DecodingHintDictionary, + ) -> crate::common::Result { + todo!() + // #if 1 +// if (!_hints.isPure()) +// return FirstOrDefault(decode(image, 1)); +// #endif + +// auto binImg = image.getBitMatrix(); +// if (binImg == nullptr) +// return {}; + +// DetectorResult detectorResult; +// if (_hints.hasFormat(BarcodeFormat::QRCode)) +// detectorResult = DetectPureQR(*binImg); +// if (_hints.hasFormat(BarcodeFormat::MicroQRCode) && !detectorResult.isValid()) +// detectorResult = DetectPureMQR(*binImg); + +// if (!detectorResult.isValid()) +// return {}; + +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); + +// return Result(std::move(decoderResult), std::move(position), +// detectorResult.bits().width() < 21 ? BarcodeFormat::MicroQRCode : BarcodeFormat::QRCode); +// } + +// void logFPSet(const FinderPatternSet& fps [[maybe_unused]]) +// { +// #ifdef PRINT_DEBUG +// auto drawLine = [](PointF a, PointF b) { +// int steps = maxAbsComponent(b - a); +// PointF dir = bresenhamDirection(PointF(b - a)); +// for (int i = 0; i < steps; ++i) +// log(centered(a + i * dir), 2); +// }; + +// drawLine(fps.bl, fps.tl); +// drawLine(fps.tl, fps.tr); +// drawLine(fps.tr, fps.bl); +// #endif + } +} + +impl MultipleBarcodeReader for QrReader { + fn decode_multiple( + &mut self, + image: &mut crate::BinaryBitmap, + ) -> crate::common::Result> { + self.decode_multiple_with_hints(image, &DecodingHintDictionary::new()) + } + + fn decode_multiple_with_hints( + &mut self, + image: &mut crate::BinaryBitmap, + hints: &DecodingHintDictionary, + ) -> crate::common::Result> { + todo!() + // auto binImg = image.getBitMatrix(); +// if (binImg == nullptr) +// return {}; + +// #ifdef PRINT_DEBUG +// LogMatrixWriter lmw(log, *binImg, 5, "qr-log.pnm"); +// #endif + +// auto allFPs = FindFinderPatterns(*binImg, _hints.tryHarder()); + +// #ifdef PRINT_DEBUG +// printf("allFPs: %d\n", Size(allFPs)); +// #endif + +// std::vector usedFPs; +// Results results; + +// if (_hints.hasFormat(BarcodeFormat::QRCode)) { +// auto allFPSets = GenerateFinderPatternSets(allFPs); +// for (const auto& fpSet : allFPSets) { +// if (Contains(usedFPs, fpSet.bl) || Contains(usedFPs, fpSet.tl) || Contains(usedFPs, fpSet.tr)) +// continue; + +// logFPSet(fpSet); + +// auto detectorResult = SampleQR(*binImg, fpSet); +// if (detectorResult.isValid()) { +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); +// if (decoderResult.isValid()) { +// usedFPs.push_back(fpSet.bl); +// usedFPs.push_back(fpSet.tl); +// usedFPs.push_back(fpSet.tr); +// } +// if (decoderResult.isValid(_hints.returnErrors())) { +// results.emplace_back(std::move(decoderResult), std::move(position), BarcodeFormat::QRCode); +// if (maxSymbols && Size(results) == maxSymbols) +// break; +// } +// } +// } +// } + +// if (_hints.hasFormat(BarcodeFormat::MicroQRCode) && !(maxSymbols && Size(results) == maxSymbols)) { +// for (const auto& fp : allFPs) { +// if (Contains(usedFPs, fp)) +// continue; + +// auto detectorResult = SampleMQR(*binImg, fp); +// if (detectorResult.isValid()) { +// auto decoderResult = Decode(detectorResult.bits()); +// auto position = detectorResult.position(); +// if (decoderResult.isValid(_hints.returnErrors())) { +// results.emplace_back(std::move(decoderResult), std::move(position), BarcodeFormat::MicroQRCode); +// if (maxSymbols && Size(results) == maxSymbols) +// break; +// } + +// } +// } +// } + +// return results; + } +} \ No newline at end of file