add ReadQRCodewords

This commit is contained in:
Henry Schimke
2023-03-30 10:32:42 -05:00
parent af626afc15
commit ae5539e270
4 changed files with 120 additions and 36 deletions

View File

@@ -24,7 +24,7 @@ impl FormatInformation {
pub fn DecodeMQR(formatInfoBits: u32) -> Self { pub fn DecodeMQR(formatInfoBits: u32) -> Self {
todo!() todo!()
// // We don't use the additional masking (with 0x4445) to work around potentially non complying MicroQRCode encoders // // We don't use the additional masking (with 0x4445) to work around potentially non complying MicroQRCode encoders
// auto fi = FindBestFormatInfo(0, FORMAT_INFO_DECODE_LOOKUP_MICRO, {formatInfoBits, MirrorBits(formatInfoBits)}); // let fi = FindBestFormatInfo(0, FORMAT_INFO_DECODE_LOOKUP_MICRO, {formatInfoBits, MirrorBits(formatInfoBits)});
// constexpr uint8_t BITS_TO_VERSION[] = {1, 2, 2, 3, 3, 4, 4, 4}; // constexpr uint8_t BITS_TO_VERSION[] = {1, 2, 2, 3, 3, 4, 4, 4};
@@ -37,6 +37,30 @@ impl FormatInformation {
// return fi; // return fi;
} }
pub fn MirrorBits(bits: u32) -> u32 {
todo!()
// return BitHacks::Reverse(bits) >> 17;
}
pub fn FindBestFormatInfo(mask: u32, lookup: [(u32, u32); 32], bits: &[u32]) -> Self {
todo!()
// FormatInformation fi;
// // Some QR codes apparently do not apply the XOR mask. Try without and with additional masking.
// for (auto mask : {0, mask})
// for (int bitsIndex = 0; bitsIndex < Size(bits); ++bitsIndex)
// for (const auto& [pattern, index] : lookup) {
// // Find the int in lookup with fewest bits differing
// if (int hammingDist = BitHacks::CountBitsSet((bits[bitsIndex] ^ mask) ^ pattern); hammingDist < fi.hammingDistance) {
// fi.index = index;
// fi.hammingDistance = hammingDist;
// fi.bitsIndex = bitsIndex;
// }
// }
// return fi;
}
pub fn isValid(&self) -> bool { pub fn isValid(&self) -> bool {
todo!() todo!()
} }

View File

@@ -10,7 +10,7 @@ use crate::{
Exceptions, Exceptions,
}; };
use super::detector::AppendBit; use super::{data_mask::GetDataMaskBit, detector::AppendBit};
pub fn getBit(bitMatrix: &BitMatrix, x: u32, y: u32, mirrored: Option<bool>) -> bool { pub fn getBit(bitMatrix: &BitMatrix, x: u32, y: u32, mirrored: Option<bool>) -> bool {
let mirrored = mirrored.unwrap_or(false); let mirrored = mirrored.unwrap_or(false);
@@ -121,42 +121,54 @@ pub fn ReadQRCodewords(
version: VersionRef, version: VersionRef,
formatInfo: &FormatInformation, formatInfo: &FormatInformation,
) -> Result<Vec<u8>> { ) -> Result<Vec<u8>> {
todo!() let functionPattern: BitMatrix = version.buildFunctionPattern()?;
// BitMatrix functionPattern = version.buildFunctionPattern();
// ByteArray result; let mut result = Vec::new();
// result.reserve(version.totalCodewords()); result.reserve(version.getTotalCodewords() as usize);
// uint8_t currentByte = 0; let mut currentByte = 0;
// bool readingUp = true; let mut readingUp = true;
// int bitsRead = 0; let mut bitsRead = 0;
// int dimension = bitMatrix.height(); let dimension = bitMatrix.height();
// // Read columns in pairs, from right to left // Read columns in pairs, from right to left
// for (int x = dimension - 1; x > 0; x -= 2) { let mut x = dimension - 1;
// // Skip whole column with vertical timing pattern. while x > 0 {
// if (x == 6) // for (int x = dimension - 1; x > 0; x -= 2) {
// x--; // Skip whole column with vertical timing pattern.
// // Read alternatingly from bottom to top then top to bottom if (x == 6) {
// for (int row = 0; row < dimension; row++) { x -= 1;
// int y = readingUp ? dimension - 1 - row : row; }
// for (int col = 0; col < 2; col++) { // Read alternatingly from bottom to top then top to bottom
// int xx = x - col; for row in 0..dimension {
// // Ignore bits covered by the function pattern // for (int row = 0; row < dimension; row++) {
// if (!functionPattern.get(xx, y)) { let y = if readingUp { dimension - 1 - row } else { row };
// // Read a bit for col in 0..2 {
// AppendBit(currentByte, // for (int col = 0; col < 2; col++) {
// GetDataMaskBit(formatInfo.dataMask, xx, y) != getBit(bitMatrix, xx, y, formatInfo.isMirrored)); let xx = x - col;
// // If we've made a whole byte, save it off // Ignore bits covered by the function pattern
// if (++bitsRead % 8 == 0) if (!functionPattern.get(xx, y)) {
// result.push_back(std::exchange(currentByte, 0)); // Read a bit
// } AppendBit(
// } &mut currentByte,
// } GetDataMaskBit(formatInfo.data_mask as u32, xx, y, None)
// readingUp = !readingUp; // switch directions != getBit(bitMatrix, xx, y, Some(formatInfo.isMirrored)),
// } );
// if (Size(result) != version.totalCodewords()) // If we've made a whole byte, save it off
// return {}; bitsRead += 1;
if (bitsRead % 8 == 0) {
result.push(std::mem::take(&mut currentByte));
}
}
}
}
readingUp = !readingUp; // switch directions
// return result; x -= 2;
}
if ((result.len()) != version.getTotalCodewords() as usize) {
return Err(Exceptions::FORMAT);
}
Ok(result.iter().copied().map(|x| x as u8).collect())
} }
pub fn ReadMQRCodewords( pub fn ReadMQRCodewords(

View File

@@ -0,0 +1,47 @@
/*
* Copyright 2016 Nu-book Inc.
* Copyright 2016 ZXing authors
*/
// SPDX-License-Identifier: Apache-2.0
use crate::common::BitMatrix;
/**
* <p>Encapsulates data masks for the data bits in a QR and micro QR code, per ISO 18004:2006 6.8.</p>
*
* <p>Note that the diagram in section 6.8.1 is misleading since it indicates that i is column position
* and j is row position. In fact, as the text says, i is row position and j is column position.</p>
*/
pub fn GetDataMaskBit(maskIndex: u32, x: u32, y: u32, isMicro: Option<bool>) -> bool {
let isMicro = isMicro.unwrap_or(false);
todo!()
// if (isMicro) {
// if (maskIndex < 0 || maskIndex >= 4)
// throw std::invalid_argument("QRCode maskIndex out of range");
// maskIndex = std::array{1, 4, 6, 7}[maskIndex]; // map from MQR to QR indices
// }
// switch (maskIndex) {
// case 0: return (y + x) % 2 == 0;
// case 1: return y % 2 == 0;
// case 2: return x % 3 == 0;
// case 3: return (y + x) % 3 == 0;
// case 4: return ((y / 2) + (x / 3)) % 2 == 0;
// case 5: return (y * x) % 6 == 0;
// case 6: return ((y * x) % 6) < 3;
// case 7: return (y + x + ((y * x) % 3)) % 2 == 0;
// }
// throw std::invalid_argument("QRCode maskIndex out of range");
}
pub fn GetMaskedBit(
bits: &BitMatrix,
x: u32,
y: u32,
maskIndex: u32,
isMicro: Option<bool>,
) -> bool {
return GetDataMaskBit(maskIndex, x, y, isMicro) != bits.get(x, y);
}

View File

@@ -1,3 +1,4 @@
mod data_mask;
pub mod decoder; pub mod decoder;
pub mod detector; pub mod detector;