mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
Merge branch 'main' into port_cpp_qrcode
This commit is contained in:
@@ -21,7 +21,7 @@
|
|||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
use crate::common::Result;
|
use crate::common::Result;
|
||||||
use crate::{Exceptions, Point};
|
use crate::{point, Exceptions, Point};
|
||||||
|
|
||||||
use super::BitArray;
|
use super::BitArray;
|
||||||
|
|
||||||
@@ -576,7 +576,7 @@ impl BitMatrix {
|
|||||||
*
|
*
|
||||||
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
|
* @return {@code x,y} coordinate of top-left-most 1 bit, or null if it is all white
|
||||||
*/
|
*/
|
||||||
pub fn getTopLeftOnBit(&self) -> Option<Vec<u32>> {
|
pub fn getTopLeftOnBit(&self) -> Option<Point> {
|
||||||
let mut bitsOffset = 0;
|
let mut bitsOffset = 0;
|
||||||
while bitsOffset < self.bits.len() && self.bits[bitsOffset] == 0 {
|
while bitsOffset < self.bits.len() && self.bits[bitsOffset] == 0 {
|
||||||
bitsOffset += 1;
|
bitsOffset += 1;
|
||||||
@@ -593,10 +593,10 @@ impl BitMatrix {
|
|||||||
bit += 1;
|
bit += 1;
|
||||||
}
|
}
|
||||||
x += bit;
|
x += bit;
|
||||||
Some(vec![x as u32, y as u32])
|
Some(point(x as f32, y as f32))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn getBottomRightOnBit(&self) -> Option<[u32; 2]> {
|
pub fn getBottomRightOnBit(&self) -> Option<Point> {
|
||||||
let mut bitsOffset = self.bits.len() as i64 - 1;
|
let mut bitsOffset = self.bits.len() as i64 - 1;
|
||||||
while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 {
|
while bitsOffset >= 0 && self.bits[bitsOffset as usize] == 0 {
|
||||||
bitsOffset -= 1;
|
bitsOffset -= 1;
|
||||||
@@ -615,7 +615,7 @@ impl BitMatrix {
|
|||||||
}
|
}
|
||||||
x += bit;
|
x += bit;
|
||||||
|
|
||||||
Some([x as u32, y as u32])
|
Some(point(x as f32, y as f32))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -27,6 +27,8 @@
|
|||||||
// */
|
// */
|
||||||
// public final class BitMatrixTestCase extends Assert {
|
// public final class BitMatrixTestCase extends Assert {
|
||||||
|
|
||||||
|
use crate::point;
|
||||||
|
|
||||||
use super::BitMatrix;
|
use super::BitMatrix;
|
||||||
|
|
||||||
static BIT_MATRIX_POINTS: [u32; 6] = [1, 2, 2, 0, 3, 1];
|
static BIT_MATRIX_POINTS: [u32; 6] = [1, 2, 2, 0, 3, 1];
|
||||||
@@ -87,14 +89,14 @@ fn test_on_bit() {
|
|||||||
assert!(matrix.getTopLeftOnBit().is_none());
|
assert!(matrix.getTopLeftOnBit().is_none());
|
||||||
assert!(matrix.getBottomRightOnBit().is_none());
|
assert!(matrix.getBottomRightOnBit().is_none());
|
||||||
matrix.setRegion(1, 1, 1, 1).expect("must set");
|
matrix.setRegion(1, 1, 1, 1).expect("must set");
|
||||||
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
|
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
|
||||||
assert_eq!(vec![1, 1], matrix.getBottomRightOnBit().unwrap());
|
assert_eq!(point(1.0, 1.0), matrix.getBottomRightOnBit().unwrap());
|
||||||
matrix.setRegion(1, 1, 3, 2).expect("must set");
|
matrix.setRegion(1, 1, 3, 2).expect("must set");
|
||||||
assert_eq!(vec![1, 1], matrix.getTopLeftOnBit().unwrap());
|
assert_eq!(point(1.0, 1.0), matrix.getTopLeftOnBit().unwrap());
|
||||||
assert_eq!(vec![3, 2], matrix.getBottomRightOnBit().unwrap());
|
assert_eq!(point(3.0, 2.0), matrix.getBottomRightOnBit().unwrap());
|
||||||
matrix.setRegion(0, 0, 5, 5).expect("must set");
|
matrix.setRegion(0, 0, 5, 5).expect("must set");
|
||||||
assert_eq!(vec![0, 0], matrix.getTopLeftOnBit().unwrap());
|
assert_eq!(point(0.0, 0.0), matrix.getTopLeftOnBit().unwrap());
|
||||||
assert_eq!(vec![4, 4], matrix.getBottomRightOnBit().unwrap());
|
assert_eq!(point(4.0, 4.0), matrix.getBottomRightOnBit().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
|
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
|
||||||
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, RXingResult,
|
point, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
|
||||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -187,12 +187,12 @@ impl DataMatrixReader {
|
|||||||
return Err(Exceptions::NOT_FOUND)
|
return Err(Exceptions::NOT_FOUND)
|
||||||
};
|
};
|
||||||
|
|
||||||
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
|
let moduleSize = Self::moduleSize(leftTopBlack, image)?;
|
||||||
|
|
||||||
let mut top = leftTopBlack[1];
|
let mut top = leftTopBlack.y;
|
||||||
let bottom = rightBottomBlack[1];
|
let bottom = rightBottomBlack.y;
|
||||||
let mut left = leftTopBlack[0];
|
let mut left = leftTopBlack.x;
|
||||||
let right = rightBottomBlack[0];
|
let right = rightBottomBlack.x;
|
||||||
|
|
||||||
let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
|
let matrixWidth = (right as i32 - left as i32 + 1) / moduleSize as i32;
|
||||||
let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
|
let matrixHeight = (bottom as i32 - top as i32 + 1) / moduleSize as i32;
|
||||||
@@ -207,7 +207,7 @@ impl DataMatrixReader {
|
|||||||
// Push in the "border" by half the module width so that we start
|
// Push in the "border" by half the module width so that we start
|
||||||
// sampling in the middle of the module. Just in case the image is a
|
// sampling in the middle of the module. Just in case the image is a
|
||||||
// little off, this will help recover.
|
// little off, this will help recover.
|
||||||
let nudge = moduleSize / 2;
|
let nudge = moduleSize as f32 / 2.0;
|
||||||
top += nudge;
|
top += nudge;
|
||||||
left += nudge;
|
left += nudge;
|
||||||
|
|
||||||
@@ -215,10 +215,10 @@ impl DataMatrixReader {
|
|||||||
let mut bits = BitMatrix::new(matrixWidth, matrixHeight)?;
|
let mut bits = BitMatrix::new(matrixWidth, matrixHeight)?;
|
||||||
for y in 0..matrixHeight {
|
for y in 0..matrixHeight {
|
||||||
// for (int y = 0; y < matrixHeight; y++) {
|
// for (int y = 0; y < matrixHeight; y++) {
|
||||||
let iOffset = top + y * moduleSize;
|
let iOffset = top + y as f32 * moduleSize as f32;
|
||||||
for x in 0..matrixWidth {
|
for x in 0..matrixWidth {
|
||||||
// for (int x = 0; x < matrixWidth; x++) {
|
// for (int x = 0; x < matrixWidth; x++) {
|
||||||
if image.get(left + x * moduleSize, iOffset) {
|
if image.get_point(point(left as f32 + x as f32 * moduleSize as f32, iOffset)) {
|
||||||
bits.set(x, y);
|
bits.set(x, y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,10 +226,10 @@ impl DataMatrixReader {
|
|||||||
Ok(bits)
|
Ok(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<u32> {
|
fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<u32> {
|
||||||
let width = image.getWidth();
|
let width = image.getWidth();
|
||||||
let mut x = leftTopBlack[0];
|
let mut x = leftTopBlack.x as u32;
|
||||||
let y = leftTopBlack[1];
|
let y = leftTopBlack.y as u32;
|
||||||
while x < width && image.get(x, y) {
|
while x < width && image.get(x, y) {
|
||||||
x += 1;
|
x += 1;
|
||||||
}
|
}
|
||||||
@@ -237,7 +237,7 @@ impl DataMatrixReader {
|
|||||||
return Err(Exceptions::NOT_FOUND);
|
return Err(Exceptions::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
let moduleSize = x - leftTopBlack[0];
|
let moduleSize = x - leftTopBlack.x as u32;
|
||||||
if moduleSize == 0 {
|
if moduleSize == 0 {
|
||||||
return Err(Exceptions::NOT_FOUND);
|
return Err(Exceptions::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ use std::collections::HashMap;
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
|
common::{BitMatrix, DecoderRXingResult, DetectorRXingResult, Result},
|
||||||
BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point, RXingResult,
|
point, BarcodeFormat, Binarizer, DecodeHintType, DecodeHintValue, Exceptions, Point,
|
||||||
RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
RXingResult, RXingResultMetadataType, RXingResultMetadataValue, Reader,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
@@ -156,12 +156,12 @@ impl QRCodeReader {
|
|||||||
let leftTopBlack = leftTopBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
|
let leftTopBlack = leftTopBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
|
||||||
let rightBottomBlack = rightBottomBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
|
let rightBottomBlack = rightBottomBlack.ok_or(Exceptions::INDEX_OUT_OF_BOUNDS)?;
|
||||||
|
|
||||||
let moduleSize = Self::moduleSize(&leftTopBlack, image)?;
|
let moduleSize = Self::moduleSize(leftTopBlack, image)?;
|
||||||
|
|
||||||
let mut top = leftTopBlack[1] as i32;
|
let mut top = leftTopBlack.y as i32;
|
||||||
let bottom = rightBottomBlack[1] as i32;
|
let bottom = rightBottomBlack.y as i32;
|
||||||
let mut left = leftTopBlack[0] as i32;
|
let mut left = leftTopBlack.x as i32;
|
||||||
let mut right = rightBottomBlack[0] as i32;
|
let mut right = rightBottomBlack.x as i32;
|
||||||
|
|
||||||
// Sanity check!
|
// Sanity check!
|
||||||
if left >= right || top >= bottom {
|
if left >= right || top >= bottom {
|
||||||
@@ -229,27 +229,27 @@ impl QRCodeReader {
|
|||||||
Ok(bits)
|
Ok(bits)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn moduleSize(leftTopBlack: &[u32], image: &BitMatrix) -> Result<f32> {
|
fn moduleSize(leftTopBlack: Point, image: &BitMatrix) -> Result<f32> {
|
||||||
let height = image.getHeight();
|
let height = image.getHeight() as f32;
|
||||||
let width = image.getWidth();
|
let width = image.getWidth() as f32;
|
||||||
let mut x = leftTopBlack[0];
|
let mut x = leftTopBlack.x;
|
||||||
let mut y = leftTopBlack[1];
|
let mut y = leftTopBlack.y;
|
||||||
let mut inBlack = true;
|
let mut inBlack = true;
|
||||||
let mut transitions = 0;
|
let mut transitions = 0;
|
||||||
while x < width && y < height {
|
while x < width && y < height {
|
||||||
if inBlack != image.get(x, y) {
|
if inBlack != image.get_point(point(x, y)) {
|
||||||
transitions += 1;
|
transitions += 1;
|
||||||
if transitions == 5 {
|
if transitions == 5 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
inBlack = !inBlack;
|
inBlack = !inBlack;
|
||||||
}
|
}
|
||||||
x += 1;
|
x += 1.0;
|
||||||
y += 1;
|
y += 1.0;
|
||||||
}
|
}
|
||||||
if x == width || y == height {
|
if x == width || y == height {
|
||||||
return Err(Exceptions::NOT_FOUND);
|
return Err(Exceptions::NOT_FOUND);
|
||||||
}
|
}
|
||||||
Ok((x - leftTopBlack[0]) as f32 / 7.0)
|
Ok((x - leftTopBlack.x) as f32 / 7.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user