Begin porting qrcode

This commit is contained in:
Henry Schimke
2022-09-30 16:20:46 -05:00
parent 5436b8d9f3
commit 9f0fcf7ce9
10 changed files with 123 additions and 99 deletions

View File

@@ -1,60 +0,0 @@
/*
* Copyright 2007 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.decoder;
/**
* <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
* defined by the QR code standard.</p>
*
* @author Sean Owen
*/
public enum ErrorCorrectionLevel {
/** L = ~7% correction */
L(0x01),
/** M = ~15% correction */
M(0x00),
/** Q = ~25% correction */
Q(0x03),
/** H = ~30% correction */
H(0x02);
private static final ErrorCorrectionLevel[] FOR_BITS = {M, L, H, Q};
private final int bits;
ErrorCorrectionLevel(int bits) {
this.bits = bits;
}
public int getBits() {
return bits;
}
/**
* @param bits int containing the two bits encoding a QR Code's error correction level
* @return ErrorCorrectionLevel representing the encoded error correction level
*/
public static ErrorCorrectionLevel forBits(int bits) {
if (bits < 0 || bits >= FOR_BITS.length) {
throw new IllegalArgumentException();
}
return FOR_BITS[bits];
}
}

View File

@@ -14,27 +14,34 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.qrcode.decoder; use crate::qrcode::decoder::ErrorCorrectionLevel;
import org.junit.Assert;
import org.junit.Test;
/** /**
* @author Sean Owen * @author Sean Owen
*/ */
public final class ErrorCorrectionLevelTestCase extends Assert {
@Test #[test]
public void testForBits() { fn testForBits() {
assertSame(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0)); assert_eq!(
assertSame(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1)); ErrorCorrectionLevel::M,
assertSame(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2)); ErrorCorrectionLevel::forBits(0).unwrap()
assertSame(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3)); );
} assert_eq!(
ErrorCorrectionLevel::L,
@Test(expected = IllegalArgumentException.class) ErrorCorrectionLevel::forBits(1).unwrap()
public void testBadECLevel() { );
ErrorCorrectionLevel.forBits(4); assert_eq!(
ErrorCorrectionLevel::H,
ErrorCorrectionLevel::forBits(2).unwrap()
);
assert_eq!(
ErrorCorrectionLevel::Q,
ErrorCorrectionLevel::forBits(3).unwrap()
);
} }
#[test]
#[should_panic]
fn testBadECLevel() {
assert!(ErrorCorrectionLevel::forBits(4).is_ok());
} }

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2007 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::Exceptions;
/**
* <p>See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels
* defined by the QR code standard.</p>
*
* @author Sean Owen
*/
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ErrorCorrectionLevel {
/** L = ~7% correction */
L, //0x01
/** M = ~15% correction */
M, //0x00
/** Q = ~25% correction */
Q, //0x03
/** H = ~30% correction */
H, //0x02
}
impl ErrorCorrectionLevel {
/**
* @param bits int containing the two bits encoding a QR Code's error correction level
* @return ErrorCorrectionLevel representing the encoded error correction level
*/
pub fn forBits(bits: u32) -> Result<Self, Exceptions> {
match bits {
0 => Ok(Self::M),
1 => Ok(Self::L),
2 => Ok(Self::H),
3 => Ok(Self::Q),
_ => Err(Exceptions::IllegalArgumentException(format!(
"{} is not a valid bit selection",
bits
))),
}
}
pub fn get_value(&self) -> u8 {
match self {
ErrorCorrectionLevel::L => 0x01,
ErrorCorrectionLevel::M => 0x00,
ErrorCorrectionLevel::Q => 0x03,
ErrorCorrectionLevel::H => 0x02,
}
}
}
//private static final ErrorCorrectionLevel[] FOR_BITS = {M, L, H, Q};
//private final int bits;

10
src/qrcode/decoder/mod.rs Normal file
View File

@@ -0,0 +1,10 @@
mod version;
mod mode;
mod error_correction_level;
#[cfg(test)]
mod ErrorCorrectionLevelTestCase;
pub use version::*;
pub use mode::*;
pub use error_correction_level::*;

View File

@@ -14,8 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.qrcode.decoder;
/** /**
* <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which * <p>See ISO 18004:2006, 6.4.1, Tables 2 and 3. This enum encapsulates the various modes in which
* data can be encoded to bits in the QR code standard.</p> * data can be encoded to bits in the QR code standard.</p>

View File

@@ -14,17 +14,12 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.qrcode.decoder;
import com.google.zxing.FormatException;
import com.google.zxing.common.BitMatrix;
/** /**
* See ISO 18004:2006 Annex D * See ISO 18004:2006 Annex D
* *
* @author Sean Owen * @author Sean Owen
*/ */
public final class Version { pub struct Version {
/** /**
* See ISO 18004:2006 Annex D. * See ISO 18004:2006 Annex D.
@@ -47,6 +42,8 @@ public final class Version {
private final ECBlocks[] ecBlocks; private final ECBlocks[] ecBlocks;
private final int totalCodewords; private final int totalCodewords;
}
impl Version {
private Version(int versionNumber, private Version(int versionNumber,
int[] alignmentPatternCenters, int[] alignmentPatternCenters,
ECBlocks... ecBlocks) { ECBlocks... ecBlocks) {

View File

View File

@@ -0,0 +1,3 @@
mod qr_code;
pub use qr_code::*;

View File

@@ -14,27 +14,26 @@
* limitations under the License. * limitations under the License.
*/ */
package com.google.zxing.qrcode.encoder; const NUM_MASK_PATTERNS : u32 = 8;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.decoder.Mode;
import com.google.zxing.qrcode.decoder.Version;
/** /**
* @author satorux@google.com (Satoru Takabayashi) - creator * @author satorux@google.com (Satoru Takabayashi) - creator
* @author dswitkin@google.com (Daniel Switkin) - ported from C++ * @author dswitkin@google.com (Daniel Switkin) - ported from C++
*/ */
public final class QRCode { pub struct QRCode {
public static final int NUM_MASK_PATTERNS = 8; // public static final int NUM_MASK_PATTERNS = 8;
private Mode mode; mode:Mode,
private ErrorCorrectionLevel ecLevel; ecLevel:ErrorCorrectionLevel,
private Version version; version:Version,
private int maskPattern; maskPattern:i32,
private ByteMatrix matrix; matrix:ByteMatrix,
public QRCode() { }
impl QRCode {
pub fn new()->Self {
maskPattern = -1; maskPattern = -1;
} }

View File

@@ -0,0 +1,3 @@
pub mod detector;
pub mod decoder;
pub mod encoder;