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

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

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 detector;