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.
*/
package com.google.zxing.qrcode.decoder;
import org.junit.Assert;
import org.junit.Test;
use crate::qrcode::decoder::ErrorCorrectionLevel;
/**
* @author Sean Owen
*/
public final class ErrorCorrectionLevelTestCase extends Assert {
@Test
public void testForBits() {
assertSame(ErrorCorrectionLevel.M, ErrorCorrectionLevel.forBits(0));
assertSame(ErrorCorrectionLevel.L, ErrorCorrectionLevel.forBits(1));
assertSame(ErrorCorrectionLevel.H, ErrorCorrectionLevel.forBits(2));
assertSame(ErrorCorrectionLevel.Q, ErrorCorrectionLevel.forBits(3));
}
@Test(expected = IllegalArgumentException.class)
public void testBadECLevel() {
ErrorCorrectionLevel.forBits(4);
}
#[test]
fn testForBits() {
assert_eq!(
ErrorCorrectionLevel::M,
ErrorCorrectionLevel::forBits(0).unwrap()
);
assert_eq!(
ErrorCorrectionLevel::L,
ErrorCorrectionLevel::forBits(1).unwrap()
);
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.
*/
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
* data can be encoded to bits in the QR code standard.</p>

View File

@@ -14,17 +14,12 @@
* 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
*
* @author Sean Owen
*/
public final class Version {
pub struct Version {
/**
* See ISO 18004:2006 Annex D.
@@ -47,6 +42,8 @@ public final class Version {
private final ECBlocks[] ecBlocks;
private final int totalCodewords;
}
impl Version {
private Version(int versionNumber,
int[] alignmentPatternCenters,
ECBlocks... ecBlocks) {