From 9f0fcf7ce9a284aec7db01b4f6a1f921ef6bdfa8 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Fri, 30 Sep 2022 16:20:46 -0500 Subject: [PATCH] Begin porting qrcode --- src/qrcode/decoder/ErrorCorrectionLevel.java | 60 ----------------- ...e.java => ErrorCorrectionLevelTestCase.rs} | 43 +++++++----- src/qrcode/decoder/error_correction_level.rs | 67 +++++++++++++++++++ src/qrcode/decoder/mod.rs | 10 +++ src/qrcode/decoder/{Mode.java => mode.rs} | 2 - .../decoder/{Version.java => version.rs} | 9 +-- src/qrcode/detector/mod.rs | 0 src/qrcode/encoder/mod.rs | 3 + .../encoder/{QRCode.java => qr_code.rs} | 25 ++++--- src/qrcode/mod.rs | 3 + 10 files changed, 123 insertions(+), 99 deletions(-) delete mode 100644 src/qrcode/decoder/ErrorCorrectionLevel.java rename src/qrcode/decoder/{ErrorCorrectionLevelTestCase.java => ErrorCorrectionLevelTestCase.rs} (50%) create mode 100644 src/qrcode/decoder/error_correction_level.rs create mode 100644 src/qrcode/decoder/mod.rs rename src/qrcode/decoder/{Mode.java => mode.rs} (98%) rename src/qrcode/decoder/{Version.java => version.rs} (99%) create mode 100644 src/qrcode/detector/mod.rs create mode 100644 src/qrcode/encoder/mod.rs rename src/qrcode/encoder/{QRCode.java => qr_code.rs} (84%) diff --git a/src/qrcode/decoder/ErrorCorrectionLevel.java b/src/qrcode/decoder/ErrorCorrectionLevel.java deleted file mode 100644 index 0f1e113..0000000 --- a/src/qrcode/decoder/ErrorCorrectionLevel.java +++ /dev/null @@ -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; - -/** - *

See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels - * defined by the QR code standard.

- * - * @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]; - } - - -} diff --git a/src/qrcode/decoder/ErrorCorrectionLevelTestCase.java b/src/qrcode/decoder/ErrorCorrectionLevelTestCase.rs similarity index 50% rename from src/qrcode/decoder/ErrorCorrectionLevelTestCase.java rename to src/qrcode/decoder/ErrorCorrectionLevelTestCase.rs index 64b5f60..2465fa2 100644 --- a/src/qrcode/decoder/ErrorCorrectionLevelTestCase.java +++ b/src/qrcode/decoder/ErrorCorrectionLevelTestCase.rs @@ -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()); } diff --git a/src/qrcode/decoder/error_correction_level.rs b/src/qrcode/decoder/error_correction_level.rs new file mode 100644 index 0000000..ab25d70 --- /dev/null +++ b/src/qrcode/decoder/error_correction_level.rs @@ -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; + +/** + *

See ISO 18004:2006, 6.5.1. This enum encapsulates the four error correction levels + * defined by the QR code standard.

+ * + * @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 { + 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; diff --git a/src/qrcode/decoder/mod.rs b/src/qrcode/decoder/mod.rs new file mode 100644 index 0000000..c6e7c47 --- /dev/null +++ b/src/qrcode/decoder/mod.rs @@ -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::*; \ No newline at end of file diff --git a/src/qrcode/decoder/Mode.java b/src/qrcode/decoder/mode.rs similarity index 98% rename from src/qrcode/decoder/Mode.java rename to src/qrcode/decoder/mode.rs index b7e9ab3..9ad741c 100644 --- a/src/qrcode/decoder/Mode.java +++ b/src/qrcode/decoder/mode.rs @@ -14,8 +14,6 @@ * limitations under the License. */ -package com.google.zxing.qrcode.decoder; - /** *

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.

diff --git a/src/qrcode/decoder/Version.java b/src/qrcode/decoder/version.rs similarity index 99% rename from src/qrcode/decoder/Version.java rename to src/qrcode/decoder/version.rs index 169e3fd..93cdd57 100755 --- a/src/qrcode/decoder/Version.java +++ b/src/qrcode/decoder/version.rs @@ -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) { diff --git a/src/qrcode/detector/mod.rs b/src/qrcode/detector/mod.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/qrcode/encoder/mod.rs b/src/qrcode/encoder/mod.rs new file mode 100644 index 0000000..fa574a4 --- /dev/null +++ b/src/qrcode/encoder/mod.rs @@ -0,0 +1,3 @@ +mod qr_code; + +pub use qr_code::*; \ No newline at end of file diff --git a/src/qrcode/encoder/QRCode.java b/src/qrcode/encoder/qr_code.rs similarity index 84% rename from src/qrcode/encoder/QRCode.java rename to src/qrcode/encoder/qr_code.rs index 4298703..f577453 100644 --- a/src/qrcode/encoder/QRCode.java +++ b/src/qrcode/encoder/qr_code.rs @@ -14,27 +14,26 @@ * 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; + const NUM_MASK_PATTERNS : u32 = 8; /** * @author satorux@google.com (Satoru Takabayashi) - creator * @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; - private ErrorCorrectionLevel ecLevel; - private Version version; - private int maskPattern; - private ByteMatrix matrix; + mode:Mode, + ecLevel:ErrorCorrectionLevel, + version:Version, + maskPattern:i32, + matrix:ByteMatrix, - public QRCode() { +} + +impl QRCode { + pub fn new()->Self { maskPattern = -1; } diff --git a/src/qrcode/mod.rs b/src/qrcode/mod.rs index e69de29..7964688 100644 --- a/src/qrcode/mod.rs +++ b/src/qrcode/mod.rs @@ -0,0 +1,3 @@ +pub mod detector; +pub mod decoder; +pub mod encoder; \ No newline at end of file