mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
incomplete port of aztec detector
This commit is contained in:
@@ -14,53 +14,54 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.google.zxing.aztec.detector;
|
// package com.google.zxing.aztec.detector;
|
||||||
|
|
||||||
import com.google.zxing.NotFoundException;
|
// import com.google.zxing.NotFoundException;
|
||||||
import com.google.zxing.aztec.AztecDetectorRXingResult;
|
// import com.google.zxing.aztec.AztecDetectorRXingResult;
|
||||||
import com.google.zxing.aztec.decoder.Decoder;
|
// import com.google.zxing.aztec.decoder.Decoder;
|
||||||
import com.google.zxing.aztec.detector.Detector.Point;
|
// import com.google.zxing.aztec.detector.Detector.Point;
|
||||||
import com.google.zxing.aztec.encoder.AztecCode;
|
// import com.google.zxing.aztec.encoder.AztecCode;
|
||||||
import com.google.zxing.aztec.encoder.Encoder;
|
// import com.google.zxing.aztec.encoder.Encoder;
|
||||||
import com.google.zxing.common.BitMatrix;
|
// import com.google.zxing.common.BitMatrix;
|
||||||
import com.google.zxing.common.DecoderRXingResult;
|
// import com.google.zxing.common.DecoderRXingResult;
|
||||||
import org.junit.Assert;
|
// import org.junit.Assert;
|
||||||
import org.junit.Test;
|
// import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
// import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
// import java.util.Arrays;
|
||||||
import java.util.Collection;
|
// import java.util.Collection;
|
||||||
import java.util.List;
|
// import java.util.List;
|
||||||
import java.util.Random;
|
// import java.util.Random;
|
||||||
import java.util.TreeSet;
|
// import java.util.TreeSet;
|
||||||
|
|
||||||
|
use crate::common::BitMatrix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for the Detector
|
* Tests for the Detector
|
||||||
*
|
*
|
||||||
* @author Frank Yellin
|
* @author Frank Yellin
|
||||||
*/
|
*/
|
||||||
public final class DetectorTest extends Assert {
|
|
||||||
|
|
||||||
@Test
|
#[test]
|
||||||
public void testErrorInParameterLocatorZeroZero() throws Exception {
|
fn testErrorInParameterLocatorZeroZero() {
|
||||||
// Layers=1, CodeWords=1. So the parameter info and its Reed-Solomon info
|
// Layers=1, CodeWords=1. So the parameter info and its Reed-Solomon info
|
||||||
// will be completely zero!
|
// will be completely zero!
|
||||||
testErrorInParameterLocator("X");
|
testErrorInParameterLocator("X");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
#[test]
|
||||||
public void testErrorInParameterLocatorCompact() throws Exception {
|
fn testErrorInParameterLocatorCompact() {
|
||||||
testErrorInParameterLocator("This is an example Aztec symbol for Wikipedia.");
|
testErrorInParameterLocator("This is an example Aztec symbol for Wikipedia.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
#[test]
|
||||||
public void testErrorInParameterLocatorNotCompact() throws Exception {
|
fn testErrorInParameterLocatorNotCompact() {
|
||||||
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz";
|
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz";
|
||||||
testErrorInParameterLocator(alphabet + alphabet + alphabet);
|
testErrorInParameterLocator(alphabet + alphabet + alphabet);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test that we can tolerate errors in the parameter locator bits
|
// Test that we can tolerate errors in the parameter locator bits
|
||||||
private static void testErrorInParameterLocator(String data) throws Exception {
|
fn testErrorInParameterLocator( data:&str) {
|
||||||
AztecCode aztec = Encoder.encode(data, 25, Encoder.DEFAULT_AZTEC_LAYERS);
|
AztecCode aztec = Encoder.encode(data, 25, Encoder.DEFAULT_AZTEC_LAYERS);
|
||||||
Random random = new Random(aztec.getMatrix().hashCode()); // pseudo-random, but deterministic
|
Random random = new Random(aztec.getMatrix().hashCode()); // pseudo-random, but deterministic
|
||||||
int layers = aztec.getLayers();
|
int layers = aztec.getLayers();
|
||||||
@@ -109,9 +110,9 @@ public final class DetectorTest extends Assert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Zooms a bit matrix so that each bit is factor x factor
|
// Zooms a bit matrix so that each bit is factor x factor
|
||||||
private static BitMatrix makeLarger(BitMatrix input, int factor) {
|
fn makeLarger( input:&BitMatrix, factor:u32) -> BitMatrix{
|
||||||
int width = input.getWidth();
|
let width = input.getWidth();
|
||||||
BitMatrix output = new BitMatrix(width * factor);
|
let output = BitMatrix::new(width * factor);
|
||||||
for (int inputY = 0; inputY < width; inputY++) {
|
for (int inputY = 0; inputY < width; inputY++) {
|
||||||
for (int inputX = 0; inputX < width; inputX++) {
|
for (int inputX = 0; inputX < width; inputX++) {
|
||||||
if (input.get(inputX, inputY)) {
|
if (input.get(inputX, inputY)) {
|
||||||
@@ -123,7 +124,7 @@ public final class DetectorTest extends Assert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Returns a list of the four rotations of the BitMatrix.
|
// Returns a list of the four rotations of the BitMatrix.
|
||||||
private static Iterable<BitMatrix> getRotations(BitMatrix matrix0) {
|
fn getRotations( matrix0:&BitMatrix)-> Vec<BitMatrix> {
|
||||||
BitMatrix matrix90 = rotateRight(matrix0);
|
BitMatrix matrix90 = rotateRight(matrix0);
|
||||||
BitMatrix matrix180 = rotateRight(matrix90);
|
BitMatrix matrix180 = rotateRight(matrix90);
|
||||||
BitMatrix matrix270 = rotateRight(matrix180);
|
BitMatrix matrix270 = rotateRight(matrix180);
|
||||||
@@ -131,7 +132,7 @@ public final class DetectorTest extends Assert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Rotates a square BitMatrix to the right by 90 degrees
|
// Rotates a square BitMatrix to the right by 90 degrees
|
||||||
private static BitMatrix rotateRight(BitMatrix input) {
|
fn rotateRight( input:&BitMatrix) -> BitMatrix{
|
||||||
int width = input.getWidth();
|
int width = input.getWidth();
|
||||||
BitMatrix result = new BitMatrix(width);
|
BitMatrix result = new BitMatrix(width);
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
@@ -146,8 +147,8 @@ public final class DetectorTest extends Assert {
|
|||||||
|
|
||||||
// Returns the transpose of a bit matrix, which is equivalent to rotating the
|
// Returns the transpose of a bit matrix, which is equivalent to rotating the
|
||||||
// matrix to the right, and then flipping it left-to-right
|
// matrix to the right, and then flipping it left-to-right
|
||||||
private static BitMatrix transpose(BitMatrix input) {
|
fn transpose( input:&BitMatrix) -> BitMatrix {
|
||||||
int width = input.getWidth();
|
let width = input.getWidth();
|
||||||
BitMatrix result = new BitMatrix(width);
|
BitMatrix result = new BitMatrix(width);
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
for (int y = 0; y < width; y++) {
|
for (int y = 0; y < width; y++) {
|
||||||
@@ -159,7 +160,7 @@ public final class DetectorTest extends Assert {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BitMatrix clone(BitMatrix input) {
|
fn clone( input:&BitMatrix) -> BitMatrix {
|
||||||
int width = input.getWidth();
|
int width = input.getWidth();
|
||||||
BitMatrix result = new BitMatrix(width);
|
BitMatrix result = new BitMatrix(width);
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
@@ -172,7 +173,7 @@ public final class DetectorTest extends Assert {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static List<Point> getOrientationPoints(AztecCode code) {
|
fn getOrientationPoints( code::&AztecCode) -> Vec<Point> {
|
||||||
int center = code.getMatrix().getWidth() / 2;
|
int center = code.getMatrix().getWidth() / 2;
|
||||||
int offset = code.isCompact() ? 5 : 7;
|
int offset = code.isCompact() ? 5 : 7;
|
||||||
List<Point> result = new ArrayList<>();
|
List<Point> result = new ArrayList<>();
|
||||||
@@ -185,5 +186,3 @@ public final class DetectorTest extends Assert {
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
@@ -27,9 +27,21 @@
|
|||||||
// import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
|
// import com.google.zxing.common.reedsolomon.ReedSolomonDecoder;
|
||||||
// import com.google.zxing.common.reedsolomon.ReedSolomonException;
|
// import com.google.zxing.common.reedsolomon.ReedSolomonException;
|
||||||
|
|
||||||
use crate::common::BitMatrix;
|
use std::fmt;
|
||||||
|
|
||||||
const EXPECTED_CORNER_BITS : [u16;4]= [
|
use crate::{
|
||||||
|
common::{
|
||||||
|
detector::{MathUtils, WhiteRectangleDetector},
|
||||||
|
reedsolomon::{self, GenericGF, ReedSolomonDecoder},
|
||||||
|
BitMatrix, DefaultGridSampler, GridSampler,
|
||||||
|
},
|
||||||
|
exceptions::Exceptions,
|
||||||
|
RXingResultPoint,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::AztecDetectorResult::AztecDetectorRXingResult;
|
||||||
|
|
||||||
|
const EXPECTED_CORNER_BITS: [u32; 4] = [
|
||||||
0xee0, // 07340 XXX .XX X.. ...
|
0xee0, // 07340 XXX .XX X.. ...
|
||||||
0x1dc, // 00734 ... XXX .XX X..
|
0x1dc, // 00734 ... XXX .XX X..
|
||||||
0x83b, // 04073 X.. ... XXX .XX
|
0x83b, // 04073 X.. ... XXX .XX
|
||||||
@@ -44,19 +56,18 @@ const EXPECTED_CORNER_BITS : [u16;4]= [
|
|||||||
* @author Frank Yellin
|
* @author Frank Yellin
|
||||||
*/
|
*/
|
||||||
pub struct Detector {
|
pub struct Detector {
|
||||||
image:BitMatrix,
|
image: BitMatrix,
|
||||||
|
|
||||||
compact:bool,
|
compact: bool,
|
||||||
nbLayers:u32,
|
nbLayers: u32,
|
||||||
nbDataBlocks:u32,
|
nbDataBlocks: u32,
|
||||||
nbCenterLayers:u32,
|
nbCenterLayers: u32,
|
||||||
shift:u32,
|
shift: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Detector {
|
impl Detector {
|
||||||
|
pub fn new(image: BitMatrix) -> Self {
|
||||||
pub fn new( image:BitMatrix) -> Self{
|
Self {
|
||||||
Self{
|
|
||||||
image,
|
image,
|
||||||
compact: false,
|
compact: false,
|
||||||
nbLayers: 0,
|
nbLayers: 0,
|
||||||
@@ -66,8 +77,8 @@ impl Detector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public AztecDetectorRXingResult detect() throws NotFoundException {
|
pub fn detect_false(&mut self) -> Result<AztecDetectorRXingResult, Exceptions> {
|
||||||
return detect(false);
|
self.detect(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -77,35 +88,42 @@ impl Detector {
|
|||||||
* @return {@link AztecDetectorRXingResult} encapsulating results of detecting an Aztec Code
|
* @return {@link AztecDetectorRXingResult} encapsulating results of detecting an Aztec Code
|
||||||
* @throws NotFoundException if no Aztec Code can be found
|
* @throws NotFoundException if no Aztec Code can be found
|
||||||
*/
|
*/
|
||||||
public AztecDetectorRXingResult detect(boolean isMirror) throws NotFoundException {
|
pub fn detect(&mut self, is_mirror: bool) -> Result<AztecDetectorRXingResult, Exceptions> {
|
||||||
|
|
||||||
// 1. Get the center of the aztec matrix
|
// 1. Get the center of the aztec matrix
|
||||||
Point pCenter = getMatrixCenter();
|
let pCenter = self.getMatrixCenter();
|
||||||
|
|
||||||
// 2. Get the center points of the four diagonal points just outside the bull's eye
|
// 2. Get the center points of the four diagonal points just outside the bull's eye
|
||||||
// [topRight, bottomRight, bottomLeft, topLeft]
|
// [topRight, bottomRight, bottomLeft, topLeft]
|
||||||
RXingResultPoint[] bullsEyeCorners = getBullsEyeCorners(pCenter);
|
let mut bullsEyeCorners = self.getBullsEyeCorners(pCenter)?;
|
||||||
|
|
||||||
if (isMirror) {
|
if is_mirror {
|
||||||
RXingResultPoint temp = bullsEyeCorners[0];
|
let temp = bullsEyeCorners[0];
|
||||||
bullsEyeCorners[0] = bullsEyeCorners[2];
|
bullsEyeCorners[0] = bullsEyeCorners[2];
|
||||||
bullsEyeCorners[2] = temp;
|
bullsEyeCorners[2] = temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Get the size of the matrix and other parameters from the bull's eye
|
// 3. Get the size of the matrix and other parameters from the bull's eye
|
||||||
extractParameters(bullsEyeCorners);
|
self.extractParameters(&bullsEyeCorners);
|
||||||
|
|
||||||
// 4. Sample the grid
|
// 4. Sample the grid
|
||||||
BitMatrix bits = sampleGrid(image,
|
let bits = self.sampleGrid(
|
||||||
bullsEyeCorners[shift % 4],
|
&self.image,
|
||||||
bullsEyeCorners[(shift + 1) % 4],
|
&bullsEyeCorners[self.shift as usize % 4],
|
||||||
bullsEyeCorners[(shift + 2) % 4],
|
&bullsEyeCorners[(self.shift as usize + 1) % 4],
|
||||||
bullsEyeCorners[(shift + 3) % 4]);
|
&bullsEyeCorners[(self.shift as usize + 2) % 4],
|
||||||
|
&bullsEyeCorners[(self.shift as usize + 3) % 4],
|
||||||
|
)?;
|
||||||
|
|
||||||
// 5. Get the corners of the matrix.
|
// 5. Get the corners of the matrix.
|
||||||
RXingResultPoint[] corners = getMatrixCornerPoints(bullsEyeCorners);
|
let corners = self.getMatrixCornerPoints(&bullsEyeCorners);
|
||||||
|
|
||||||
return new AztecDetectorRXingResult(bits, corners, compact, nbDataBlocks, nbLayers);
|
Ok(AztecDetectorRXingResult::new(
|
||||||
|
bits,
|
||||||
|
corners,
|
||||||
|
self.compact,
|
||||||
|
self.nbDataBlocks,
|
||||||
|
self.nbLayers,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,57 +132,66 @@ impl Detector {
|
|||||||
* @param bullsEyeCorners the array of bull's eye corners
|
* @param bullsEyeCorners the array of bull's eye corners
|
||||||
* @throws NotFoundException in case of too many errors or invalid parameters
|
* @throws NotFoundException in case of too many errors or invalid parameters
|
||||||
*/
|
*/
|
||||||
private void extractParameters(RXingResultPoint[] bullsEyeCorners) throws NotFoundException {
|
fn extractParameters(
|
||||||
if (!isValid(bullsEyeCorners[0]) || !isValid(bullsEyeCorners[1]) ||
|
&mut self,
|
||||||
!isValid(bullsEyeCorners[2]) || !isValid(bullsEyeCorners[3])) {
|
bullsEyeCorners: &[RXingResultPoint],
|
||||||
throw NotFoundException.getNotFoundInstance();
|
) -> Result<(), Exceptions> {
|
||||||
|
if !self.isValid(&bullsEyeCorners[0])
|
||||||
|
|| !self.isValid(&bullsEyeCorners[1])
|
||||||
|
|| !self.isValid(&bullsEyeCorners[2])
|
||||||
|
|| !self.isValid(&bullsEyeCorners[3])
|
||||||
|
{
|
||||||
|
return Err(Exceptions::NotFoundException("no valid points".to_owned()));
|
||||||
}
|
}
|
||||||
int length = 2 * nbCenterLayers;
|
let length = 2 * self.nbCenterLayers;
|
||||||
// Get the bits around the bull's eye
|
// Get the bits around the bull's eye
|
||||||
int[] sides = {
|
let sides = [
|
||||||
sampleLine(bullsEyeCorners[0], bullsEyeCorners[1], length), // Right side
|
self.sampleLine(&bullsEyeCorners[0], &bullsEyeCorners[1], length), // Right side
|
||||||
sampleLine(bullsEyeCorners[1], bullsEyeCorners[2], length), // Bottom
|
self.sampleLine(&bullsEyeCorners[1], &bullsEyeCorners[2], length), // Bottom
|
||||||
sampleLine(bullsEyeCorners[2], bullsEyeCorners[3], length), // Left side
|
self.sampleLine(&bullsEyeCorners[2], &bullsEyeCorners[3], length), // Left side
|
||||||
sampleLine(bullsEyeCorners[3], bullsEyeCorners[0], length) // Top
|
self.sampleLine(&bullsEyeCorners[3], &bullsEyeCorners[0], length), // Top
|
||||||
};
|
];
|
||||||
|
|
||||||
// bullsEyeCorners[shift] is the corner of the bulls'eye that has three
|
// bullsEyeCorners[shift] is the corner of the bulls'eye that has three
|
||||||
// orientation marks.
|
// orientation marks.
|
||||||
// sides[shift] is the row/column that goes from the corner with three
|
// sides[shift] is the row/column that goes from the corner with three
|
||||||
// orientation marks to the corner with two.
|
// orientation marks to the corner with two.
|
||||||
shift = getRotation(sides, length);
|
self.shift = Self::getRotation(&sides, length)?;
|
||||||
|
|
||||||
// Flatten the parameter bits into a single 28- or 40-bit long
|
// Flatten the parameter bits into a single 28- or 40-bit long
|
||||||
long parameterData = 0;
|
let mut parameterData = 0u64;
|
||||||
for (int i = 0; i < 4; i++) {
|
for i in 0..4 {
|
||||||
int side = sides[(shift + i) % 4];
|
// for (int i = 0; i < 4; i++) {
|
||||||
if (compact) {
|
let side = sides[(self.shift + i) as usize % 4];
|
||||||
|
if self.compact {
|
||||||
// Each side of the form ..XXXXXXX. where Xs are parameter data
|
// Each side of the form ..XXXXXXX. where Xs are parameter data
|
||||||
parameterData <<= 7;
|
parameterData <<= 7;
|
||||||
parameterData += (side >> 1) & 0x7F;
|
parameterData += (side as u64 >> 1) & 0x7F;
|
||||||
} else {
|
} else {
|
||||||
// Each side of the form ..XXXXX.XXXXX. where Xs are parameter data
|
// Each side of the form ..XXXXX.XXXXX. where Xs are parameter data
|
||||||
parameterData <<= 10;
|
parameterData <<= 10;
|
||||||
parameterData += ((side >> 2) & (0x1f << 5)) + ((side >> 1) & 0x1F);
|
parameterData += ((side as u64 >> 2) & (0x1f << 5)) + ((side as u64 >> 1) & 0x1F);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Corrects parameter data using RS. Returns just the data portion
|
// Corrects parameter data using RS. Returns just the data portion
|
||||||
// without the error correction.
|
// without the error correction.
|
||||||
int correctedData = getCorrectedParameterData(parameterData, compact);
|
let correctedData = Self::getCorrectedParameterData(parameterData, self.compact)?;
|
||||||
|
|
||||||
if (compact) {
|
if self.compact {
|
||||||
// 8 bits: 2 bits layers and 6 bits data blocks
|
// 8 bits: 2 bits layers and 6 bits data blocks
|
||||||
nbLayers = (correctedData >> 6) + 1;
|
self.nbLayers = (correctedData >> 6) + 1;
|
||||||
nbDataBlocks = (correctedData & 0x3F) + 1;
|
self.nbDataBlocks = (correctedData & 0x3F) + 1;
|
||||||
} else {
|
} else {
|
||||||
// 16 bits: 5 bits layers and 11 bits data blocks
|
// 16 bits: 5 bits layers and 11 bits data blocks
|
||||||
nbLayers = (correctedData >> 11) + 1;
|
self.nbLayers = (correctedData >> 11) + 1;
|
||||||
nbDataBlocks = (correctedData & 0x7FF) + 1;
|
self.nbDataBlocks = (correctedData & 0x7FF) + 1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getRotation(int[] sides, int length) throws NotFoundException {
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getRotation(sides: &[u32], length: u32) -> Result<u32, Exceptions> {
|
||||||
// In a normal pattern, we expect to See
|
// In a normal pattern, we expect to See
|
||||||
// ** .* D A
|
// ** .* D A
|
||||||
// * *
|
// * *
|
||||||
@@ -174,10 +201,11 @@ impl Detector {
|
|||||||
//
|
//
|
||||||
// Grab the 3 bits from each of the sides the form the locator pattern and concatenate
|
// Grab the 3 bits from each of the sides the form the locator pattern and concatenate
|
||||||
// into a 12-bit integer. Start with the bit at A
|
// into a 12-bit integer. Start with the bit at A
|
||||||
int cornerBits = 0;
|
let mut cornerBits = 0;
|
||||||
for (int side : sides) {
|
for side in sides {
|
||||||
|
// for (int side : sides) {
|
||||||
// XX......X where X's are orientation marks
|
// XX......X where X's are orientation marks
|
||||||
int t = ((side >> (length - 2)) << 1) + (side & 1);
|
let t = ((side >> (length - 2)) << 1) + (side & 1);
|
||||||
cornerBits = (cornerBits << 3) + t;
|
cornerBits = (cornerBits << 3) + t;
|
||||||
}
|
}
|
||||||
// Mov the bottom bit to the top, so that the three bits of the locator pattern at A are
|
// Mov the bottom bit to the top, so that the three bits of the locator pattern at A are
|
||||||
@@ -187,12 +215,14 @@ impl Detector {
|
|||||||
// The result shift indicates which element of BullsEyeCorners[] goes into the top-left
|
// The result shift indicates which element of BullsEyeCorners[] goes into the top-left
|
||||||
// corner. Since the four rotation values have a Hamming distance of 8, we
|
// corner. Since the four rotation values have a Hamming distance of 8, we
|
||||||
// can easily tolerate two errors.
|
// can easily tolerate two errors.
|
||||||
for (int shift = 0; shift < 4; shift++) {
|
for shift in 0..4 {
|
||||||
if (Integer.bitCount(cornerBits ^ EXPECTED_CORNER_BITS[shift]) <= 2) {
|
// for (int shift = 0; shift < 4; shift++) {
|
||||||
return shift;
|
if (cornerBits ^ EXPECTED_CORNER_BITS[shift as usize]).count_ones() <= 2 {
|
||||||
|
// if (Integer.bitCount(cornerBits ^ EXPECTED_CORNER_BITS[shift]) <= 2) {
|
||||||
|
return Ok(shift);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw NotFoundException.getNotFoundInstance();
|
Err(Exceptions::NotFoundException("rotation failure".to_owned()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -202,11 +232,13 @@ impl Detector {
|
|||||||
* @param compact true if this is a compact Aztec code
|
* @param compact true if this is a compact Aztec code
|
||||||
* @throws NotFoundException if the array contains too many errors
|
* @throws NotFoundException if the array contains too many errors
|
||||||
*/
|
*/
|
||||||
private static int getCorrectedParameterData(long parameterData, boolean compact) throws NotFoundException {
|
fn getCorrectedParameterData(parameterData: u64, compact: bool) -> Result<u32, Exceptions> {
|
||||||
int numCodewords;
|
let mut parameterData = parameterData;
|
||||||
int numDataCodewords;
|
|
||||||
|
|
||||||
if (compact) {
|
let numCodewords: i32;
|
||||||
|
let numDataCodewords: i32;
|
||||||
|
|
||||||
|
if compact {
|
||||||
numCodewords = 7;
|
numCodewords = 7;
|
||||||
numDataCodewords = 2;
|
numDataCodewords = 2;
|
||||||
} else {
|
} else {
|
||||||
@@ -214,24 +246,28 @@ impl Detector {
|
|||||||
numDataCodewords = 4;
|
numDataCodewords = 4;
|
||||||
}
|
}
|
||||||
|
|
||||||
int numECCodewords = numCodewords - numDataCodewords;
|
let numECCodewords = numCodewords - numDataCodewords;
|
||||||
int[] parameterWords = new int[numCodewords];
|
let mut parameterWords = vec![0i32; numCodewords as usize];
|
||||||
for (int i = numCodewords - 1; i >= 0; --i) {
|
for i in (0..numCodewords - 1).rev() {
|
||||||
parameterWords[i] = (int) parameterData & 0xF;
|
// for (int i = numCodewords - 1; i >= 0; --i) {
|
||||||
|
parameterWords[i as usize] = (parameterData & 0xF) as i32;
|
||||||
parameterData >>= 4;
|
parameterData >>= 4;
|
||||||
}
|
}
|
||||||
try {
|
//try {
|
||||||
ReedSolomonDecoder rsDecoder = new ReedSolomonDecoder(GenericGF.AZTEC_PARAM);
|
let field =
|
||||||
rsDecoder.decode(parameterWords, numECCodewords);
|
reedsolomon::get_predefined_genericgf(reedsolomon::PredefinedGenericGF::AztecParam);
|
||||||
} catch (ReedSolomonException ignored) {
|
let rsDecoder = ReedSolomonDecoder::new(field);
|
||||||
throw NotFoundException.getNotFoundInstance();
|
rsDecoder.decode(&mut parameterWords, numECCodewords)?;
|
||||||
}
|
//} catch (ReedSolomonException ignored) {
|
||||||
|
//throw NotFoundException.getNotFoundInstance();
|
||||||
|
//}
|
||||||
// Toss the error correction. Just return the data as an integer
|
// Toss the error correction. Just return the data as an integer
|
||||||
int result = 0;
|
let mut result = 0u32;
|
||||||
for (int i = 0; i < numDataCodewords; i++) {
|
for i in 0..numDataCodewords {
|
||||||
result = (result << 4) + parameterWords[i];
|
// for (int i = 0; i < numDataCodewords; i++) {
|
||||||
|
result = (result << 4) + parameterWords[i as usize] as u32;
|
||||||
}
|
}
|
||||||
return result;
|
Ok(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -243,28 +279,35 @@ impl Detector {
|
|||||||
* @return The corners of the bull-eye
|
* @return The corners of the bull-eye
|
||||||
* @throws NotFoundException If no valid bull-eye can be found
|
* @throws NotFoundException If no valid bull-eye can be found
|
||||||
*/
|
*/
|
||||||
private RXingResultPoint[] getBullsEyeCorners(Point pCenter) throws NotFoundException {
|
fn getBullsEyeCorners(&mut self, pCenter: Point) -> Result<Vec<RXingResultPoint>, Exceptions> {
|
||||||
|
let mut pina = pCenter;
|
||||||
|
let mut pinb = pCenter;
|
||||||
|
let mut pinc = pCenter;
|
||||||
|
let mut pind = pCenter;
|
||||||
|
|
||||||
Point pina = pCenter;
|
let mut color = true;
|
||||||
Point pinb = pCenter;
|
|
||||||
Point pinc = pCenter;
|
|
||||||
Point pind = pCenter;
|
|
||||||
|
|
||||||
boolean color = true;
|
for nbCenterLayers in 1..9 {
|
||||||
|
// for (nbCenterLayers = 1; nbCenterLayers < 9; nbCenterLayers++) {
|
||||||
for (nbCenterLayers = 1; nbCenterLayers < 9; nbCenterLayers++) {
|
let pouta = self.getFirstDifferent(&pina, color, 1, -1);
|
||||||
Point pouta = getFirstDifferent(pina, color, 1, -1);
|
let poutb = self.getFirstDifferent(&pinb, color, 1, 1);
|
||||||
Point poutb = getFirstDifferent(pinb, color, 1, 1);
|
let poutc = self.getFirstDifferent(&pinc, color, -1, 1);
|
||||||
Point poutc = getFirstDifferent(pinc, color, -1, 1);
|
let poutd = self.getFirstDifferent(&pind, color, -1, -1);
|
||||||
Point poutd = getFirstDifferent(pind, color, -1, -1);
|
|
||||||
|
|
||||||
//d a
|
//d a
|
||||||
//
|
//
|
||||||
//c b
|
//c b
|
||||||
|
|
||||||
if (nbCenterLayers > 2) {
|
if nbCenterLayers > 2 {
|
||||||
float q = distance(poutd, pouta) * nbCenterLayers / (distance(pind, pina) * (nbCenterLayers + 2));
|
let q: f32 =
|
||||||
if (q < 0.75 || q > 1.25 || !isWhiteOrBlackRectangle(pouta, poutb, poutc, poutd)) {
|
Self::distance(&poutd.toRXingResultPoint(), &pouta.toRXingResultPoint())
|
||||||
|
* nbCenterLayers as f32
|
||||||
|
/ (Self::distance(&pind.toRXingResultPoint(), &pina.toRXingResultPoint())
|
||||||
|
* (nbCenterLayers + 2) as f32);
|
||||||
|
if q < 0.75
|
||||||
|
|| q > 1.25
|
||||||
|
|| !self.isWhiteOrBlackRectangle(&pouta, &poutb, &poutc, &poutd)
|
||||||
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -277,24 +320,26 @@ impl Detector {
|
|||||||
color = !color;
|
color = !color;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nbCenterLayers != 5 && nbCenterLayers != 7) {
|
if self.nbCenterLayers != 5 && self.nbCenterLayers != 7 {
|
||||||
throw NotFoundException.getNotFoundInstance();
|
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||||
}
|
}
|
||||||
|
|
||||||
compact = nbCenterLayers == 5;
|
self.compact = self.nbCenterLayers == 5;
|
||||||
|
|
||||||
// Expand the square by .5 pixel in each direction so that we're on the border
|
// Expand the square by .5 pixel in each direction so that we're on the border
|
||||||
// between the white square and the black square
|
// between the white square and the black square
|
||||||
RXingResultPoint pinax = new RXingResultPoint(pina.getX() + 0.5f, pina.getY() - 0.5f);
|
let pinax = RXingResultPoint::new(pina.getX() as f32 + 0.5f32, pina.getY() as f32 - 0.5f32);
|
||||||
RXingResultPoint pinbx = new RXingResultPoint(pinb.getX() + 0.5f, pinb.getY() + 0.5f);
|
let pinbx = RXingResultPoint::new(pinb.getX() as f32 + 0.5f32, pinb.getY() as f32 + 0.5f32);
|
||||||
RXingResultPoint pincx = new RXingResultPoint(pinc.getX() - 0.5f, pinc.getY() + 0.5f);
|
let pincx = RXingResultPoint::new(pinc.getX() as f32 - 0.5f32, pinc.getY() as f32 + 0.5f32);
|
||||||
RXingResultPoint pindx = new RXingResultPoint(pind.getX() - 0.5f, pind.getY() - 0.5f);
|
let pindx = RXingResultPoint::new(pind.getX() as f32 - 0.5f32, pind.getY() as f32 - 0.5f32);
|
||||||
|
|
||||||
// Expand the square so that its corners are the centers of the points
|
// Expand the square so that its corners are the centers of the points
|
||||||
// just outside the bull's eye.
|
// just outside the bull's eye.
|
||||||
return expandSquare(new RXingResultPoint[]{pinax, pinbx, pincx, pindx},
|
Ok(Self::expandSquare(
|
||||||
2 * nbCenterLayers - 3,
|
&[pinax, pinbx, pincx, pindx],
|
||||||
2 * nbCenterLayers);
|
2 * self.nbCenterLayers - 3,
|
||||||
|
2 * self.nbCenterLayers,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -302,62 +347,125 @@ impl Detector {
|
|||||||
*
|
*
|
||||||
* @return the center point
|
* @return the center point
|
||||||
*/
|
*/
|
||||||
private Point getMatrixCenter() {
|
fn getMatrixCenter(&self) -> Point {
|
||||||
|
let mut pointA = RXingResultPoint { x: 0.0, y: 0.0 };
|
||||||
|
let mut pointB = RXingResultPoint { x: 0.0, y: 0.0 };
|
||||||
|
let mut pointC = RXingResultPoint { x: 0.0, y: 0.0 };
|
||||||
|
let mut pointD = RXingResultPoint { x: 0.0, y: 0.0 };
|
||||||
|
|
||||||
RXingResultPoint pointA;
|
let mut fnd = false;
|
||||||
RXingResultPoint pointB;
|
|
||||||
RXingResultPoint pointC;
|
|
||||||
RXingResultPoint pointD;
|
|
||||||
|
|
||||||
//Get a white rectangle that can be the border of the matrix in center bull's eye or
|
//Get a white rectangle that can be the border of the matrix in center bull's eye or
|
||||||
try {
|
if let Ok(wrd) = WhiteRectangleDetector::new_from_image(&self.image) {
|
||||||
|
if let Ok(cornerPoints) = wrd.detect() {
|
||||||
RXingResultPoint[] cornerPoints = new WhiteRectangleDetector(image).detect();
|
|
||||||
pointA = cornerPoints[0];
|
pointA = cornerPoints[0];
|
||||||
pointB = cornerPoints[1];
|
pointB = cornerPoints[1];
|
||||||
pointC = cornerPoints[2];
|
pointC = cornerPoints[2];
|
||||||
pointD = cornerPoints[3];
|
pointD = cornerPoints[3];
|
||||||
|
fnd = true;
|
||||||
} catch (NotFoundException e) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This exception can be in case the initial rectangle is white
|
// This exception can be in case the initial rectangle is white
|
||||||
// In that case, surely in the bull's eye, we try to expand the rectangle.
|
// In that case, surely in the bull's eye, we try to expand the rectangle.
|
||||||
int cx = image.getWidth() / 2;
|
if !fnd {
|
||||||
int cy = image.getHeight() / 2;
|
let cx: i32 = (self.image.getWidth() / 2).try_into().unwrap();
|
||||||
pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toRXingResultPoint();
|
let cy: i32 = (self.image.getHeight() / 2).try_into().unwrap();
|
||||||
pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toRXingResultPoint();
|
pointA = self
|
||||||
pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toRXingResultPoint();
|
.getFirstDifferent(&Point::new(cx + 7, cy - 7), false, 1, -1)
|
||||||
pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toRXingResultPoint();
|
.toRXingResultPoint();
|
||||||
|
pointB = self
|
||||||
|
.getFirstDifferent(&Point::new(cx + 7, cy + 7), false, 1, 1)
|
||||||
|
.toRXingResultPoint();
|
||||||
|
pointC = self
|
||||||
|
.getFirstDifferent(&Point::new(cx - 7, cy + 7), false, -1, 1)
|
||||||
|
.toRXingResultPoint();
|
||||||
|
pointD = self
|
||||||
|
.getFirstDifferent(&Point::new(cx - 7, cy - 7), false, -1, -1)
|
||||||
|
.toRXingResultPoint();
|
||||||
}
|
}
|
||||||
|
// try {
|
||||||
|
|
||||||
|
// let cornerPoints = WhiteRectangleDetector::new(image).detect();
|
||||||
|
// pointA = cornerPoints[0];
|
||||||
|
// pointB = cornerPoints[1];
|
||||||
|
// pointC = cornerPoints[2];
|
||||||
|
// pointD = cornerPoints[3];
|
||||||
|
|
||||||
|
// } catch (NotFoundException e) {
|
||||||
|
|
||||||
|
// // This exception can be in case the initial rectangle is white
|
||||||
|
// // In that case, surely in the bull's eye, we try to expand the rectangle.
|
||||||
|
// int cx = image.getWidth() / 2;
|
||||||
|
// int cy = image.getHeight() / 2;
|
||||||
|
// pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toRXingResultPoint();
|
||||||
|
// pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toRXingResultPoint();
|
||||||
|
// pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toRXingResultPoint();
|
||||||
|
// pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toRXingResultPoint();
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
//Compute the center of the rectangle
|
//Compute the center of the rectangle
|
||||||
int cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f);
|
let mut cx = MathUtils::round(
|
||||||
int cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f);
|
(pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f32,
|
||||||
|
);
|
||||||
|
let mut cy = MathUtils::round(
|
||||||
|
(pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f32,
|
||||||
|
);
|
||||||
|
|
||||||
// Redetermine the white rectangle starting from previously computed center.
|
// Redetermine the white rectangle starting from previously computed center.
|
||||||
// This will ensure that we end up with a white rectangle in center bull's eye
|
// This will ensure that we end up with a white rectangle in center bull's eye
|
||||||
// in order to compute a more accurate center.
|
// in order to compute a more accurate center.
|
||||||
try {
|
let mut fnd = false;
|
||||||
RXingResultPoint[] cornerPoints = new WhiteRectangleDetector(image, 15, cx, cy).detect();
|
if let Ok(wrd) = WhiteRectangleDetector::new(&self.image, 15, cx, cy) {
|
||||||
|
if let Ok(cornerPoints) = wrd.detect() {
|
||||||
pointA = cornerPoints[0];
|
pointA = cornerPoints[0];
|
||||||
pointB = cornerPoints[1];
|
pointB = cornerPoints[1];
|
||||||
pointC = cornerPoints[2];
|
pointC = cornerPoints[2];
|
||||||
pointD = cornerPoints[3];
|
pointD = cornerPoints[3];
|
||||||
} catch (NotFoundException e) {
|
fnd = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
// This exception can be in case the initial rectangle is white
|
// This exception can be in case the initial rectangle is white
|
||||||
// In that case we try to expand the rectangle.
|
// In that case we try to expand the rectangle.
|
||||||
pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toRXingResultPoint();
|
if !fnd {
|
||||||
pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toRXingResultPoint();
|
pointA = self
|
||||||
pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toRXingResultPoint();
|
.getFirstDifferent(&Point::new(cx + 7, cy - 7), false, 1, -1)
|
||||||
pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toRXingResultPoint();
|
.toRXingResultPoint();
|
||||||
|
pointB = self
|
||||||
|
.getFirstDifferent(&Point::new(cx + 7, cy + 7), false, 1, 1)
|
||||||
|
.toRXingResultPoint();
|
||||||
|
pointC = self
|
||||||
|
.getFirstDifferent(&Point::new(cx - 7, cy + 7), false, -1, 1)
|
||||||
|
.toRXingResultPoint();
|
||||||
|
pointD = self
|
||||||
|
.getFirstDifferent(&Point::new(cx - 7, cy - 7), false, -1, -1)
|
||||||
|
.toRXingResultPoint();
|
||||||
}
|
}
|
||||||
|
// try {
|
||||||
|
// RXingResultPoint[] cornerPoints = new WhiteRectangleDetector(image, 15, cx, cy).detect();
|
||||||
|
// pointA = cornerPoints[0];
|
||||||
|
// pointB = cornerPoints[1];
|
||||||
|
// pointC = cornerPoints[2];
|
||||||
|
// pointD = cornerPoints[3];
|
||||||
|
// } catch (NotFoundException e) {
|
||||||
|
// // This exception can be in case the initial rectangle is white
|
||||||
|
// // In that case we try to expand the rectangle.
|
||||||
|
// pointA = getFirstDifferent(new Point(cx + 7, cy - 7), false, 1, -1).toRXingResultPoint();
|
||||||
|
// pointB = getFirstDifferent(new Point(cx + 7, cy + 7), false, 1, 1).toRXingResultPoint();
|
||||||
|
// pointC = getFirstDifferent(new Point(cx - 7, cy + 7), false, -1, 1).toRXingResultPoint();
|
||||||
|
// pointD = getFirstDifferent(new Point(cx - 7, cy - 7), false, -1, -1).toRXingResultPoint();
|
||||||
|
// }
|
||||||
|
|
||||||
// Recompute the center of the rectangle
|
// Recompute the center of the rectangle
|
||||||
cx = MathUtils.round((pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f);
|
cx = MathUtils::round(
|
||||||
cy = MathUtils.round((pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f);
|
(pointA.getX() + pointD.getX() + pointB.getX() + pointC.getX()) / 4.0f32,
|
||||||
|
);
|
||||||
|
cy = MathUtils::round(
|
||||||
|
(pointA.getY() + pointD.getY() + pointB.getY() + pointC.getY()) / 4.0f32,
|
||||||
|
);
|
||||||
|
|
||||||
return new Point(cx, cy);
|
Point::new(cx, cy)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -366,8 +474,12 @@ impl Detector {
|
|||||||
* @param bullsEyeCorners the array of bull's eye corners
|
* @param bullsEyeCorners the array of bull's eye corners
|
||||||
* @return the array of aztec code corners
|
* @return the array of aztec code corners
|
||||||
*/
|
*/
|
||||||
private RXingResultPoint[] getMatrixCornerPoints(RXingResultPoint[] bullsEyeCorners) {
|
fn getMatrixCornerPoints(&self, bullsEyeCorners: &[RXingResultPoint]) -> Vec<RXingResultPoint> {
|
||||||
return expandSquare(bullsEyeCorners, 2 * nbCenterLayers, getDimension());
|
Self::expandSquare(
|
||||||
|
bullsEyeCorners,
|
||||||
|
2 * self.nbCenterLayers,
|
||||||
|
self.getDimension(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -375,29 +487,41 @@ impl Detector {
|
|||||||
* topLeft, topRight, bottomRight, and bottomLeft are the centers of the squares on the
|
* topLeft, topRight, bottomRight, and bottomLeft are the centers of the squares on the
|
||||||
* diagonal just outside the bull's eye.
|
* diagonal just outside the bull's eye.
|
||||||
*/
|
*/
|
||||||
private BitMatrix sampleGrid(BitMatrix image,
|
fn sampleGrid(
|
||||||
RXingResultPoint topLeft,
|
&self,
|
||||||
RXingResultPoint topRight,
|
image: &BitMatrix,
|
||||||
RXingResultPoint bottomRight,
|
topLeft: &RXingResultPoint,
|
||||||
RXingResultPoint bottomLeft) throws NotFoundException {
|
topRight: &RXingResultPoint,
|
||||||
|
bottomRight: &RXingResultPoint,
|
||||||
|
bottomLeft: &RXingResultPoint,
|
||||||
|
) -> Result<BitMatrix, Exceptions> {
|
||||||
|
let sampler = DefaultGridSampler {};
|
||||||
|
let dimension = self.getDimension();
|
||||||
|
|
||||||
GridSampler sampler = GridSampler.getInstance();
|
let low = dimension as f32 / 2.0f32 - self.nbCenterLayers as f32;
|
||||||
int dimension = getDimension();
|
let high = dimension as f32 / 2.0f32 + self.nbCenterLayers as f32;
|
||||||
|
|
||||||
float low = dimension / 2.0f - nbCenterLayers;
|
sampler.sample_grid_detailed(
|
||||||
float high = dimension / 2.0f + nbCenterLayers;
|
image,
|
||||||
|
|
||||||
return sampler.sampleGrid(image,
|
|
||||||
dimension,
|
dimension,
|
||||||
dimension,
|
dimension,
|
||||||
low, low, // topleft
|
low,
|
||||||
high, low, // topright
|
low, // topleft
|
||||||
high, high, // bottomright
|
high,
|
||||||
low, high, // bottomleft
|
low, // topright
|
||||||
topLeft.getX(), topLeft.getY(),
|
high,
|
||||||
topRight.getX(), topRight.getY(),
|
high, // bottomright
|
||||||
bottomRight.getX(), bottomRight.getY(),
|
low,
|
||||||
bottomLeft.getX(), bottomLeft.getY());
|
high, // bottomleft
|
||||||
|
topLeft.getX(),
|
||||||
|
topLeft.getY(),
|
||||||
|
topRight.getX(),
|
||||||
|
topRight.getY(),
|
||||||
|
bottomRight.getX(),
|
||||||
|
bottomRight.getY(),
|
||||||
|
bottomLeft.getX(),
|
||||||
|
bottomLeft.getY(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -408,17 +532,21 @@ impl Detector {
|
|||||||
* @param size number of bits
|
* @param size number of bits
|
||||||
* @return the array of bits as an int (first bit is high-order bit of result)
|
* @return the array of bits as an int (first bit is high-order bit of result)
|
||||||
*/
|
*/
|
||||||
private int sampleLine(RXingResultPoint p1, RXingResultPoint p2, int size) {
|
fn sampleLine(&self, p1: &RXingResultPoint, p2: &RXingResultPoint, size: u32) -> u32 {
|
||||||
int result = 0;
|
let mut result = 0;
|
||||||
|
|
||||||
float d = distance(p1, p2);
|
let d = Self::distance(p1, p2);
|
||||||
float moduleSize = d / size;
|
let moduleSize = d / size as f32;
|
||||||
float px = p1.getX();
|
let px = p1.getX();
|
||||||
float py = p1.getY();
|
let py = p1.getY();
|
||||||
float dx = moduleSize * (p2.getX() - p1.getX()) / d;
|
let dx = moduleSize * (p2.getX() - p1.getX()) / d;
|
||||||
float dy = moduleSize * (p2.getY() - p1.getY()) / d;
|
let dy = moduleSize * (p2.getY() - p1.getY()) / d;
|
||||||
for (int i = 0; i < size; i++) {
|
for i in 0..size {
|
||||||
if (image.get(MathUtils.round(px + i * dx), MathUtils.round(py + i * dy))) {
|
// for (int i = 0; i < size; i++) {
|
||||||
|
if self.image.get(
|
||||||
|
MathUtils::round(px + i as f32 * dx) as u32,
|
||||||
|
MathUtils::round(py + i as f32 * dy) as u32,
|
||||||
|
) {
|
||||||
result |= 1 << (size - i - 1);
|
result |= 1 << (size - i - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -429,42 +557,50 @@ impl Detector {
|
|||||||
* @return true if the border of the rectangle passed in parameter is compound of white points only
|
* @return true if the border of the rectangle passed in parameter is compound of white points only
|
||||||
* or black points only
|
* or black points only
|
||||||
*/
|
*/
|
||||||
private boolean isWhiteOrBlackRectangle(Point p1,
|
fn isWhiteOrBlackRectangle(&self, p1: &Point, p2: &Point, p3: &Point, p4: &Point) -> bool {
|
||||||
Point p2,
|
let corr = 3;
|
||||||
Point p3,
|
|
||||||
Point p4) {
|
|
||||||
|
|
||||||
int corr = 3;
|
let p1 = Point::new(
|
||||||
|
0.max(p1.getX() - corr),
|
||||||
|
(self.image.getHeight() as i32 - 1).min(p1.getY() + corr),
|
||||||
|
);
|
||||||
|
// let p1 = Point::new(Math.max(0, p1.getX() - corr), Math.min(image.getHeight() - 1, p1.getY() + corr));
|
||||||
|
let p2 = Point::new(0.max(p2.getX() - corr), 0.max(p2.getY() - corr));
|
||||||
|
// let p2 = Point::new(Math.max(0, p2.getX() - corr), Math.max(0, p2.getY() - corr));
|
||||||
|
let p3 = Point::new(
|
||||||
|
(self.image.getWidth() as i32 - 1).min(p3.getX() + corr),
|
||||||
|
0.max((self.image.getHeight() as i32 - 1).min(p3.getY() - corr)),
|
||||||
|
);
|
||||||
|
// let p3 = Point::new(Math.min(image.getWidth() - 1, p3.getX() + corr),
|
||||||
|
// Math.max(0, Math.min(image.getHeight() - 1, p3.getY() - corr)));
|
||||||
|
let p4 = Point::new(
|
||||||
|
self.image.getWidth() as i32 - 1.min(p4.getX() + corr),
|
||||||
|
(self.image.getHeight() as i32 - 1).min(p4.getY() + corr),
|
||||||
|
);
|
||||||
|
// let p4 = Point::new(Math.min(image.getWidth() - 1, p4.getX() + corr),
|
||||||
|
// Math.min(image.getHeight() - 1, p4.getY() + corr));
|
||||||
|
|
||||||
p1 = new Point(Math.max(0, p1.getX() - corr), Math.min(image.getHeight() - 1, p1.getY() + corr));
|
let cInit = self.getColor(&p4, &p1);
|
||||||
p2 = new Point(Math.max(0, p2.getX() - corr), Math.max(0, p2.getY() - corr));
|
|
||||||
p3 = new Point(Math.min(image.getWidth() - 1, p3.getX() + corr),
|
|
||||||
Math.max(0, Math.min(image.getHeight() - 1, p3.getY() - corr)));
|
|
||||||
p4 = new Point(Math.min(image.getWidth() - 1, p4.getX() + corr),
|
|
||||||
Math.min(image.getHeight() - 1, p4.getY() + corr));
|
|
||||||
|
|
||||||
int cInit = getColor(p4, p1);
|
if cInit == 0 {
|
||||||
|
|
||||||
if (cInit == 0) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int c = getColor(p1, p2);
|
let c = self.getColor(&p1, &p2);
|
||||||
|
|
||||||
if (c != cInit) {
|
if c != cInit {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = getColor(p2, p3);
|
let c = self.getColor(&p2, &p3);
|
||||||
|
|
||||||
if (c != cInit) {
|
if c != cInit {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
c = getColor(p3, p4);
|
let c = self.getColor(&p3, &p4);
|
||||||
|
|
||||||
return c == cInit;
|
return c == cInit;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -472,46 +608,51 @@ impl Detector {
|
|||||||
*
|
*
|
||||||
* @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
|
* @return 1 if segment more than 90% black, -1 if segment is more than 90% white, 0 else
|
||||||
*/
|
*/
|
||||||
private int getColor(Point p1, Point p2) {
|
fn getColor(&self, p1: &Point, p2: &Point) -> i32 {
|
||||||
float d = distance(p1, p2);
|
let d = Self::distance_points(p1, p2);
|
||||||
if (d == 0.0f) {
|
if d == 0.0f32 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
float dx = (p2.getX() - p1.getX()) / d;
|
let dx = (p2.getX() - p1.getX()) as f32 / d;
|
||||||
float dy = (p2.getY() - p1.getY()) / d;
|
let dy = (p2.getY() - p1.getY()) as f32 / d;
|
||||||
int error = 0;
|
let mut error = 0;
|
||||||
|
|
||||||
float px = p1.getX();
|
let mut px = p1.getX();
|
||||||
float py = p1.getY();
|
let mut py = p1.getY();
|
||||||
|
|
||||||
boolean colorModel = image.get(p1.getX(), p1.getY());
|
let colorModel = self.image.get(p1.getX() as u32, p1.getY() as u32);
|
||||||
|
|
||||||
int iMax = (int) Math.floor(d);
|
let iMax = d.floor() as u32; //(int) Math.floor(d);
|
||||||
for (int i = 0; i < iMax; i++) {
|
for _i in 0..iMax {
|
||||||
if (image.get(MathUtils.round(px), MathUtils.round(py)) != colorModel) {
|
// for (int i = 0; i < iMax; i++) {
|
||||||
error++;
|
if self.image.get(px as u32, py as u32) != colorModel {
|
||||||
|
error += 1;
|
||||||
}
|
}
|
||||||
px += dx;
|
px += dx.floor() as i32;
|
||||||
py += dy;
|
py += dy.floor() as i32;
|
||||||
}
|
}
|
||||||
|
|
||||||
float errRatio = error / d;
|
let errRatio = error as f32 / d;
|
||||||
|
|
||||||
if (errRatio > 0.1f && errRatio < 0.9f) {
|
if errRatio > 0.1f32 && errRatio < 0.9f32 {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (errRatio <= 0.1f) == colorModel ? 1 : -1;
|
if (errRatio <= 0.1f32) == colorModel {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
-1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the coordinate of the first point with a different color in the given direction
|
* Gets the coordinate of the first point with a different color in the given direction
|
||||||
*/
|
*/
|
||||||
private Point getFirstDifferent(Point init, boolean color, int dx, int dy) {
|
fn getFirstDifferent(&self, init: &Point, color: bool, dx: i32, dy: i32) -> Point {
|
||||||
int x = init.getX() + dx;
|
let mut x = init.getX() + dx;
|
||||||
int y = init.getY() + dy;
|
let mut y = init.getY() + dy;
|
||||||
|
|
||||||
while (isValid(x, y) && image.get(x, y) == color) {
|
while self.isValidPoints(x, y) && self.image.get(x as u32, y as u32) == color {
|
||||||
x += dx;
|
x += dx;
|
||||||
y += dy;
|
y += dy;
|
||||||
}
|
}
|
||||||
@@ -519,17 +660,17 @@ impl Detector {
|
|||||||
x -= dx;
|
x -= dx;
|
||||||
y -= dy;
|
y -= dy;
|
||||||
|
|
||||||
while (isValid(x, y) && image.get(x, y) == color) {
|
while self.isValidPoints(x, y) && self.image.get(x as u32, y as u32) == color {
|
||||||
x += dx;
|
x += dx;
|
||||||
}
|
}
|
||||||
x -= dx;
|
x -= dx;
|
||||||
|
|
||||||
while (isValid(x, y) && image.get(x, y) == color) {
|
while self.isValidPoints(x, y) && self.image.get(x as u32, y as u32) == color {
|
||||||
y += dy;
|
y += dy;
|
||||||
}
|
}
|
||||||
y -= dy;
|
y -= dy;
|
||||||
|
|
||||||
return new Point(x, y);
|
Point::new(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -540,75 +681,85 @@ impl Detector {
|
|||||||
* @param newSide the new length of the size of the square in the target bit matrix
|
* @param newSide the new length of the size of the square in the target bit matrix
|
||||||
* @return the corners of the expanded square
|
* @return the corners of the expanded square
|
||||||
*/
|
*/
|
||||||
private static RXingResultPoint[] expandSquare(RXingResultPoint[] cornerPoints, int oldSide, int newSide) {
|
fn expandSquare(
|
||||||
float ratio = newSide / (2.0f * oldSide);
|
cornerPoints: &[RXingResultPoint],
|
||||||
float dx = cornerPoints[0].getX() - cornerPoints[2].getX();
|
oldSide: u32,
|
||||||
float dy = cornerPoints[0].getY() - cornerPoints[2].getY();
|
newSide: u32,
|
||||||
float centerx = (cornerPoints[0].getX() + cornerPoints[2].getX()) / 2.0f;
|
) -> Vec<RXingResultPoint> {
|
||||||
float centery = (cornerPoints[0].getY() + cornerPoints[2].getY()) / 2.0f;
|
let ratio = newSide as f32 / (2.0f32 * oldSide as f32);
|
||||||
|
let mut dx = cornerPoints[0].getX() - cornerPoints[2].getX();
|
||||||
|
let mut dy = cornerPoints[0].getY() - cornerPoints[2].getY();
|
||||||
|
let mut centerx = (cornerPoints[0].getX() + cornerPoints[2].getX()) / 2.0f32;
|
||||||
|
let mut centery = (cornerPoints[0].getY() + cornerPoints[2].getY()) / 2.0f32;
|
||||||
|
|
||||||
RXingResultPoint result0 = new RXingResultPoint(centerx + ratio * dx, centery + ratio * dy);
|
let result0 = RXingResultPoint::new(centerx + ratio * dx, centery + ratio * dy);
|
||||||
RXingResultPoint result2 = new RXingResultPoint(centerx - ratio * dx, centery - ratio * dy);
|
let result2 = RXingResultPoint::new(centerx - ratio * dx, centery - ratio * dy);
|
||||||
|
|
||||||
dx = cornerPoints[1].getX() - cornerPoints[3].getX();
|
dx = cornerPoints[1].getX() - cornerPoints[3].getX();
|
||||||
dy = cornerPoints[1].getY() - cornerPoints[3].getY();
|
dy = cornerPoints[1].getY() - cornerPoints[3].getY();
|
||||||
centerx = (cornerPoints[1].getX() + cornerPoints[3].getX()) / 2.0f;
|
centerx = (cornerPoints[1].getX() + cornerPoints[3].getX()) / 2.0f32;
|
||||||
centery = (cornerPoints[1].getY() + cornerPoints[3].getY()) / 2.0f;
|
centery = (cornerPoints[1].getY() + cornerPoints[3].getY()) / 2.0f32;
|
||||||
RXingResultPoint result1 = new RXingResultPoint(centerx + ratio * dx, centery + ratio * dy);
|
let result1 = RXingResultPoint::new(centerx + ratio * dx, centery + ratio * dy);
|
||||||
RXingResultPoint result3 = new RXingResultPoint(centerx - ratio * dx, centery - ratio * dy);
|
let result3 = RXingResultPoint::new(centerx - ratio * dx, centery - ratio * dy);
|
||||||
|
|
||||||
return new RXingResultPoint[]{result0, result1, result2, result3};
|
vec![result0, result1, result2, result3]
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValid(int x, int y) {
|
fn isValidPoints(&self, x: i32, y: i32) -> bool {
|
||||||
return x >= 0 && x < image.getWidth() && y >= 0 && y < image.getHeight();
|
x >= 0
|
||||||
|
&& x < self.image.getWidth().try_into().unwrap()
|
||||||
|
&& y >= 0
|
||||||
|
&& y < self.image.getHeight().try_into().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isValid(RXingResultPoint point) {
|
fn isValid(&self, point: &RXingResultPoint) -> bool {
|
||||||
int x = MathUtils.round(point.getX());
|
let x = MathUtils::round(point.getX());
|
||||||
int y = MathUtils.round(point.getY());
|
let y = MathUtils::round(point.getY());
|
||||||
return isValid(x, y);
|
self.isValidPoints(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
private static float distance(Point a, Point b) {
|
fn distance_points(a: &Point, b: &Point) -> f32 {
|
||||||
return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY());
|
MathUtils::distance_int(a.getX(), a.getY(), b.getX(), b.getY())
|
||||||
}
|
}
|
||||||
|
|
||||||
private static float distance(RXingResultPoint a, RXingResultPoint b) {
|
fn distance(a: &RXingResultPoint, b: &RXingResultPoint) -> f32 {
|
||||||
return MathUtils.distance(a.getX(), a.getY(), b.getX(), b.getY());
|
MathUtils::distance_float(a.getX(), a.getY(), b.getX(), b.getY())
|
||||||
}
|
}
|
||||||
|
|
||||||
private int getDimension() {
|
fn getDimension(&self) -> u32 {
|
||||||
if (compact) {
|
if self.compact {
|
||||||
return 4 * nbLayers + 11;
|
return 4 * self.nbLayers + 11;
|
||||||
}
|
|
||||||
return 4 * nbLayers + 2 * ((2 * nbLayers + 6) / 15) + 15;
|
|
||||||
}
|
|
||||||
|
|
||||||
static final class Point {
|
|
||||||
private final int x;
|
|
||||||
private final int y;
|
|
||||||
|
|
||||||
RXingResultPoint toRXingResultPoint() {
|
|
||||||
return new RXingResultPoint(x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Point(int x, int y) {
|
|
||||||
this.x = x;
|
|
||||||
this.y = y;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getX() {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getY() {
|
|
||||||
return y;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "<" + x + ' ' + y + '>';
|
|
||||||
}
|
}
|
||||||
|
4 * self.nbLayers + 2 * ((2 * self.nbLayers + 6) / 15) + 15
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Point {
|
||||||
|
pub fn toRXingResultPoint(&self) -> RXingResultPoint {
|
||||||
|
RXingResultPoint::new(self.x as f32, self.y as f32)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new(x: i32, y: i32) -> Self {
|
||||||
|
Self { x, y }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getX(&self) -> i32 {
|
||||||
|
self.x
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getY(&self) -> i32 {
|
||||||
|
self.y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Point {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "<{} {}>", &self.x, &self.y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
mod AztecDetectorResult;
|
mod AztecDetectorResult;
|
||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
// pub mod detector;
|
pub mod detector;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod DecoderTest;
|
mod DecoderTest;
|
||||||
// #[cfg(test)]
|
// #[cfg(test)]
|
||||||
// mod EncoderTest;
|
// mod EncoderTest;
|
||||||
|
#[cfg(test)]
|
||||||
|
mod DetectorTest;
|
||||||
|
|
||||||
mod shared_test_methods;
|
mod shared_test_methods;
|
||||||
@@ -823,7 +823,7 @@ use crate::common::detector::MathUtils;
|
|||||||
*
|
*
|
||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub struct RXingResultPoint {
|
pub struct RXingResultPoint {
|
||||||
x: f32,
|
x: f32,
|
||||||
y: f32,
|
y: f32,
|
||||||
|
|||||||
Reference in New Issue
Block a user