QrCode struct and associated types ported NOPASS

This commit is contained in:
Henry Schimke
2022-10-01 16:24:53 -05:00
parent afc7a20747
commit e5abca3884
15 changed files with 476 additions and 428 deletions

View File

@@ -1,99 +0,0 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.encoder;
import java.util.Arrays;
/**
* JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
* -1, 0, and 1, I'm going to use less memory and go with bytes.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
public final class ByteMatrix {
private final byte[][] bytes;
private final int width;
private final int height;
public ByteMatrix(int width, int height) {
bytes = new byte[height][width];
this.width = width;
this.height = height;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public byte get(int x, int y) {
return bytes[y][x];
}
/**
* @return an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
*/
public byte[][] getArray() {
return bytes;
}
public void set(int x, int y, byte value) {
bytes[y][x] = value;
}
public void set(int x, int y, int value) {
bytes[y][x] = (byte) value;
}
public void set(int x, int y, boolean value) {
bytes[y][x] = (byte) (value ? 1 : 0);
}
public void clear(byte value) {
for (byte[] aByte : bytes) {
Arrays.fill(aByte, value);
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder(2 * width * height + 2);
for (int y = 0; y < height; ++y) {
byte[] bytesY = bytes[y];
for (int x = 0; x < width; ++x) {
switch (bytesY[x]) {
case 0:
result.append(" 0");
break;
case 1:
result.append(" 1");
break;
default:
result.append(" ");
break;
}
}
result.append('\n');
}
return result.toString();
}
}

View File

@@ -1,128 +0,0 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.zxing.qrcode.encoder;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Mode;
import com.google.zxing.qrcode.decoder.Version;
import org.junit.Assert;
import org.junit.Test;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++
*/
public final class QRCodeTestCase extends Assert {
@Test
public void test() {
QRCode qrCode = new QRCode();
// First, test simple setters and getters.
// We use numbers of version 7-H.
qrCode.setMode(Mode.BYTE);
qrCode.setECLevel(ErrorCorrectionLevel.H);
qrCode.setVersion(Version.getVersionForNumber(7));
qrCode.setMaskPattern(3);
assertSame(Mode.BYTE, qrCode.getMode());
assertSame(ErrorCorrectionLevel.H, qrCode.getECLevel());
assertEquals(7, qrCode.getVersion().getVersionNumber());
assertEquals(3, qrCode.getMaskPattern());
// Prepare the matrix.
ByteMatrix matrix = new ByteMatrix(45, 45);
// Just set bogus zero/one values.
for (int y = 0; y < 45; ++y) {
for (int x = 0; x < 45; ++x) {
matrix.set(x, y, (y + x) % 2);
}
}
// Set the matrix.
qrCode.setMatrix(matrix);
assertSame(matrix, qrCode.getMatrix());
}
@Test
public void testToString1() {
QRCode qrCode = new QRCode();
String expected =
"<<\n" +
" mode: null\n" +
" ecLevel: null\n" +
" version: null\n" +
" maskPattern: -1\n" +
" matrix: null\n" +
">>\n";
assertEquals(expected, qrCode.toString());
}
@Test
public void testToString2() {
QRCode qrCode = new QRCode();
qrCode.setMode(Mode.BYTE);
qrCode.setECLevel(ErrorCorrectionLevel.H);
qrCode.setVersion(Version.getVersionForNumber(1));
qrCode.setMaskPattern(3);
ByteMatrix matrix = new ByteMatrix(21, 21);
for (int y = 0; y < 21; ++y) {
for (int x = 0; x < 21; ++x) {
matrix.set(x, y, (y + x) % 2);
}
}
qrCode.setMatrix(matrix);
String expected = "<<\n" +
" mode: BYTE\n" +
" ecLevel: H\n" +
" version: 1\n" +
" maskPattern: 3\n" +
" matrix:\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
" 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n" +
" 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n" +
">>\n";
assertEquals(expected, qrCode.toString());
}
@Test
public void testIsValidMaskPattern() {
assertFalse(QRCode.isValidMaskPattern(-1));
assertTrue(QRCode.isValidMaskPattern(0));
assertTrue(QRCode.isValidMaskPattern(7));
assertFalse(QRCode.isValidMaskPattern(8));
}
}

View File

@@ -0,0 +1,128 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::qrcode::{decoder::{Mode, ErrorCorrectionLevel, Version}, encoder::ByteMatrix};
use super::QRCode;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
* @author mysen@google.com (Chris Mysen) - ported from C++
*/
#[test]
fn test() {
let mut qrCode = QRCode::new();
// First, test simple setters and getters.
// We use numbers of version 7-H.
qrCode.setMode(Mode::BYTE);
qrCode.setECLevel(ErrorCorrectionLevel::H);
qrCode.setVersion(Version::getVersionForNumber(7).expect("must exist"));
qrCode.setMaskPattern(3);
assert_eq!(&Mode::BYTE, qrCode.getMode().as_ref().unwrap());
assert_eq!(ErrorCorrectionLevel::H, qrCode.getECLevel().unwrap());
assert_eq!(7, qrCode.getVersion().as_ref().unwrap().getVersionNumber());
assert_eq!(3, qrCode.getMaskPattern());
// Prepare the matrix.
let mut matrix = ByteMatrix::new(45, 45);
// Just set bogus zero/one values.
for y in 0..45 {
// for (int y = 0; y < 45; ++y) {
for x in 0..45 {
// for (int x = 0; x < 45; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
}
// Set the matrix.
qrCode.setMatrix(matrix.clone());
assert_eq!(&matrix, qrCode.getMatrix().as_ref().unwrap());
}
#[test]
fn testToString1() {
let qrCode = QRCode::new();
let expected =
"<<\n\
mode: null\n\
ecLevel: null\n\
version: null\n\
maskPattern: -1\n\
matrix: null\n\
>>\n";
assert_eq!(expected, qrCode.to_string());
}
#[test]
fn testToString2() {
let mut qrCode = QRCode::new();
qrCode.setMode(Mode::BYTE);
qrCode.setECLevel(ErrorCorrectionLevel::H);
qrCode.setVersion(Version::getVersionForNumber(1).expect("predefined value must exist"));
qrCode.setMaskPattern(3);
let mut matrix = ByteMatrix::new(21, 21);
for y in 0..21 {
// for (int y = 0; y < 21; ++y) {
for x in 0..21 {
// for (int x = 0; x < 21; ++x) {
matrix.set(x, y, ((y + x) % 2) as u8);
}
}
qrCode.setMatrix(matrix);
let expected = "<<\n\
mode: BYTE\n\
ecLevel: H\n\
version: 1\n\
maskPattern: 3\n\
matrix:\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1\n\
0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0\n\
>>\n";
assert_eq!(expected, qrCode.to_string());
}
#[test]
fn testIsValidMaskPattern() {
assert!(!QRCode::isValidMaskPattern(-1));
assert!(QRCode::isValidMaskPattern(0));
assert!(QRCode::isValidMaskPattern(7));
assert!(!QRCode::isValidMaskPattern(8));
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2008 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use std::fmt;
/**
* JAVAPORT: The original code was a 2D array of ints, but since it only ever gets assigned
* -1, 0, and 1, I'm going to use less memory and go with bytes.
*
* @author dswitkin@google.com (Daniel Switkin)
*/
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ByteMatrix {
bytes: Vec<Vec<u8>>,
width: u32,
height: u32,
}
impl ByteMatrix {
pub fn new(width: u32, height: u32) -> Self {
Self {
bytes: vec![vec![0u8; width as usize]; height as usize],
width,
height,
}
// bytes = new byte[height][width];
}
pub fn getHeight(&self) -> u32 {
self.height
}
pub fn getWidth(&self) -> u32 {
self.width
}
pub fn get(&self, x: u32, y: u32) -> u8 {
self.bytes[y as usize][x as usize]
}
/**
* @return an internal representation as bytes, in row-major order. array[y][x] represents point (x,y)
*/
pub fn getArray(&self) -> &Vec<Vec<u8>> {
&self.bytes
}
pub fn set(&mut self, x: u32, y: u32, value: u8) {
self.bytes[y as usize][x as usize] = value;
}
// pub fn set(int x, int y, int value) {
// bytes[y][x] = (byte) value;
// }
pub fn set_bool(&mut self, x: u32, y: u32, value: bool) {
self.bytes[y as usize][x as usize] = if value { 1 } else { 0 };
}
pub fn clear(&mut self, value: u8) {
for row in self.bytes.iter_mut() {
*row = vec![value; row.len()];
}
// for (byte[] aByte : bytes) {
// Arrays.fill(aByte, value);
// }
}
}
impl fmt::Display for ByteMatrix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::with_capacity(2 * self.width as usize * self.height as usize + 2);
for y in 0..self.height as usize {
// for (int y = 0; y < height; ++y) {
let bytesY = &self.bytes[y];
for x in 0..self.width as usize {
// for (int x = 0; x < width; ++x) {
match bytesY[x] {
0 => result.push_str(" 0"),
1 => result.push_str(" 1"),
_ => result.push_str(" "),
};
// switch (bytesY[x]) {
// case 0:
// result.append(" 0");
// break;
// case 1:
// result.append(" 1");
// break;
// default:
// result.append(" ");
// break;
// }
}
result.push('\n');
}
write!(f, "{}", result)
}
}

View File

@@ -1,3 +1,8 @@
mod qr_code;
mod byte_matrix;
pub use qr_code::*;
pub use qr_code::*;
pub use byte_matrix::*;
#[cfg(test)]
mod QRCodeTestCase;

View File

@@ -14,9 +14,13 @@
* limitations under the License.
*/
use std::fmt;
use crate::qrcode::decoder::{Mode, ErrorCorrectionLevel, Version};
const NUM_MASK_PATTERNS : u32 = 8;
use super::ByteMatrix;
const NUM_MASK_PATTERNS : i32 = 8;
/**
* @author satorux@google.com (Satoru Takabayashi) - creator
@@ -26,87 +30,97 @@ pub struct QRCode {
// public static final int NUM_MASK_PATTERNS = 8;
mode:Mode,
ecLevel:ErrorCorrectionLevel,
version:Version,
mode:Option<Mode>,
ecLevel:Option<ErrorCorrectionLevel>,
version:Option<&'static Version>,
maskPattern:i32,
matrix:ByteMatrix,
matrix:Option<ByteMatrix>,
}
impl QRCode {
pub fn new()->Self {
maskPattern = -1;
Self {
mode: None,
ecLevel: None,
version: None,
maskPattern: -1,
matrix: None,
}
}
/**
* @return the mode. Not relevant if {@link com.google.zxing.EncodeHintType#QR_COMPACT} is selected.
*/
public Mode getMode() {
return mode;
pub fn getMode(&self) -> &Option<Mode>{
&self.mode
}
public ErrorCorrectionLevel getECLevel() {
return ecLevel;
pub fn getECLevel(&self) -> &Option<ErrorCorrectionLevel>{
&self.ecLevel
}
public Version getVersion() {
return version;
pub fn getVersion(&self) -> &Option<&'static Version>{
&self.version
}
public int getMaskPattern() {
return maskPattern;
pub fn getMaskPattern(&self) -> i32{
self.maskPattern
}
public ByteMatrix getMatrix() {
return matrix;
pub fn getMatrix(&self) ->&Option<ByteMatrix>{
&self.matrix
}
@Override
public String toString() {
StringBuilder result = new StringBuilder(200);
result.append("<<\n");
result.append(" mode: ");
result.append(mode);
result.append("\n ecLevel: ");
result.append(ecLevel);
result.append("\n version: ");
result.append(version);
result.append("\n maskPattern: ");
result.append(maskPattern);
if (matrix == null) {
result.append("\n matrix: null\n");
} else {
result.append("\n matrix:\n");
result.append(matrix);
}
result.append(">>\n");
return result.toString();
pub fn setMode(&mut self, value:Mode) {
self.mode = Some(value);
}
public void setMode(Mode value) {
mode = value;
pub fn setECLevel(&mut self, value:ErrorCorrectionLevel) {
self.ecLevel = Some(value);
}
public void setECLevel(ErrorCorrectionLevel value) {
ecLevel = value;
pub fn setVersion(&mut self, version:&'static Version) {
self.version = Some(version);
}
public void setVersion(Version version) {
this.version = version;
pub fn setMaskPattern(&mut self, value:i32) {
self.maskPattern = value;
}
public void setMaskPattern(int value) {
maskPattern = value;
}
public void setMatrix(ByteMatrix value) {
matrix = value;
pub fn setMatrix(&mut self, value:ByteMatrix) {
self.matrix = Some(value);
}
// Check if "mask_pattern" is valid.
public static boolean isValidMaskPattern(int maskPattern) {
return maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS;
pub fn isValidMaskPattern( maskPattern:i32) -> bool{
maskPattern >= 0 && maskPattern < NUM_MASK_PATTERNS
}
}
impl fmt::Display for QRCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut result = String::with_capacity(200);
result.push_str("<<\n");
result.push_str(" mode: ");
result.push_str(&format!("{:?}", self.mode));
result.push_str("\n ecLevel: ");
result.push_str(&format!("{:?}", self.ecLevel));
result.push_str("\n version: ");
result.push_str(&format!("{}",self.version.as_ref().unwrap()));
result.push_str("\n maskPattern: ");
result.push_str(&format!("{}",self.maskPattern));
if self.matrix.is_none() {
result.push_str("\n matrix: null\n");
} else {
result.push_str("\n matrix:\n");
result.push_str(&format!("{}",self.matrix.as_ref().unwrap()));
}
result.push_str(">>\n");
write!(f, "{}", result)
}
}