mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
format datamatrix
This commit is contained in:
@@ -2,13 +2,19 @@ pub mod decoder;
|
|||||||
pub mod detector;
|
pub mod detector;
|
||||||
pub mod encoder;
|
pub mod encoder;
|
||||||
|
|
||||||
use crate::{BarcodeFormat,BinaryBitmap,DecodeHintType,ChecksumException,FormatException,NotFoundException,Reader,RXingResult,ResultMetadataType,ResultPoint,EncodeHintType,Writer,Dimension};
|
use crate::common::{BitMatrix, DecoderResult, DetectorResult};
|
||||||
use crate::common::{DecoderResult,BitMatrix,DetectorResult,};
|
use crate::datamatrix::decoder::Decoder;
|
||||||
use crate::datamatrix::decoder::{Decoder};
|
use crate::datamatrix::detector::Detector;
|
||||||
use crate::datamatrix::detector::{Detector};
|
use crate::datamatrix::encoder::{
|
||||||
use crate::datamatrix::encoder::{DefaultPlacement,ErrorCorrection,HighLevelEncoder,MinimalEncoder,SymbolInfo,SymbolShapeHint};
|
DefaultPlacement, ErrorCorrection, HighLevelEncoder, MinimalEncoder, SymbolInfo,
|
||||||
|
SymbolShapeHint,
|
||||||
|
};
|
||||||
use crate::qrcode::encoder::ByteMatrix;
|
use crate::qrcode::encoder::ByteMatrix;
|
||||||
|
use crate::{
|
||||||
|
BarcodeFormat, BinaryBitmap, ChecksumException, DecodeHintType, Dimension, EncodeHintType,
|
||||||
|
FormatException, NotFoundException, RXingResult, Reader, ResultMetadataType, ResultPoint,
|
||||||
|
Writer,
|
||||||
|
};
|
||||||
|
|
||||||
// DataMatrixReader.java
|
// DataMatrixReader.java
|
||||||
/**
|
/**
|
||||||
@@ -19,11 +25,10 @@ use crate::qrcode::encoder::ByteMatrix;
|
|||||||
|
|
||||||
const NO_POINTS: [Option<ResultPoint>; 0] = [None; 0];
|
const NO_POINTS: [Option<ResultPoint>; 0] = [None; 0];
|
||||||
pub struct DataMatrixReader {
|
pub struct DataMatrixReader {
|
||||||
|
decoder: Decoder,
|
||||||
decoder: Decoder
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Reader for DataMatrixReader{
|
impl Reader for DataMatrixReader {
|
||||||
/**
|
/**
|
||||||
* Locates and decodes a Data Matrix code in an image.
|
* Locates and decodes a Data Matrix code in an image.
|
||||||
*
|
*
|
||||||
@@ -32,7 +37,11 @@ impl Reader for DataMatrixReader{
|
|||||||
* @throws FormatException if a Data Matrix code cannot be decoded
|
* @throws FormatException if a Data Matrix code cannot be decoded
|
||||||
* @throws ChecksumException if error correction fails
|
* @throws ChecksumException if error correction fails
|
||||||
*/
|
*/
|
||||||
fn decode(&self, image: &BinaryBitmap, hints: &Map<DecodeHintType, _>) -> Result<Result, NotFoundException, ChecksumException, FormatException> {
|
fn decode(
|
||||||
|
&self,
|
||||||
|
image: &BinaryBitmap,
|
||||||
|
hints: &Map<DecodeHintType, _>,
|
||||||
|
) -> Result<Result, NotFoundException, ChecksumException, FormatException> {
|
||||||
let decoder_result: DecoderResult;
|
let decoder_result: DecoderResult;
|
||||||
let mut points: Vec<ResultPoint>;
|
let mut points: Vec<ResultPoint>;
|
||||||
if hints != null && hints.contains_key(DecodeHintType::PURE_BARCODE) {
|
if hints != null && hints.contains_key(DecodeHintType::PURE_BARCODE) {
|
||||||
@@ -44,7 +53,12 @@ impl Reader for DataMatrixReader{
|
|||||||
decoder_result = self.decoder.decode(&detector_result.get_bits());
|
decoder_result = self.decoder.decode(&detector_result.get_bits());
|
||||||
points = detector_result.get_points();
|
points = detector_result.get_points();
|
||||||
}
|
}
|
||||||
let result: Result = Result::new(&decoder_result.get_text(), &decoder_result.get_raw_bytes(), points, BarcodeFormat::DATA_MATRIX);
|
let result: Result = Result::new(
|
||||||
|
&decoder_result.get_text(),
|
||||||
|
&decoder_result.get_raw_bytes(),
|
||||||
|
points,
|
||||||
|
BarcodeFormat::DATA_MATRIX,
|
||||||
|
);
|
||||||
let byte_segments: List<Vec<i8>> = decoder_result.get_byte_segments();
|
let byte_segments: List<Vec<i8>> = decoder_result.get_byte_segments();
|
||||||
if byte_segments != null {
|
if byte_segments != null {
|
||||||
result.put_metadata(ResultMetadataType::BYTE_SEGMENTS, &byte_segments);
|
result.put_metadata(ResultMetadataType::BYTE_SEGMENTS, &byte_segments);
|
||||||
@@ -53,19 +67,21 @@ impl Reader for DataMatrixReader{
|
|||||||
if ec_level != null {
|
if ec_level != null {
|
||||||
result.put_metadata(ResultMetadataType::ERROR_CORRECTION_LEVEL, &ec_level);
|
result.put_metadata(ResultMetadataType::ERROR_CORRECTION_LEVEL, &ec_level);
|
||||||
}
|
}
|
||||||
result.put_metadata(ResultMetadataType::SYMBOLOGY_IDENTIFIER, format!("]d{}", decoder_result.get_symbology_modifier()));
|
result.put_metadata(
|
||||||
|
ResultMetadataType::SYMBOLOGY_IDENTIFIER,
|
||||||
|
format!("]d{}", decoder_result.get_symbology_modifier()),
|
||||||
|
);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset(&self) {
|
fn reset(&self) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DataMatrixReader {
|
impl DataMatrixReader {
|
||||||
|
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self{
|
Self {
|
||||||
decoder: Decoder::new(),
|
decoder: Decoder::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,11 +92,11 @@ impl DataMatrixReader {
|
|||||||
* around it. This is a specialized method that works exceptionally fast in this special
|
* around it. This is a specialized method that works exceptionally fast in this special
|
||||||
* case.
|
* case.
|
||||||
*/
|
*/
|
||||||
fn extract_pure_bits( image: &BitMatrix) -> Result<BitMatrix, NotFoundException> {
|
fn extract_pure_bits(image: &BitMatrix) -> Result<BitMatrix, NotFoundException> {
|
||||||
let left_top_black: Vec<i32> = image.get_top_left_on_bit();
|
let left_top_black: Vec<i32> = image.get_top_left_on_bit();
|
||||||
let right_bottom_black: Vec<i32> = image.get_bottom_right_on_bit();
|
let right_bottom_black: Vec<i32> = image.get_bottom_right_on_bit();
|
||||||
if left_top_black == null || right_bottom_black == null {
|
if left_top_black == null || right_bottom_black == null {
|
||||||
return Err( NotFoundException::get_not_found_instance());
|
return Err(NotFoundException::get_not_found_instance());
|
||||||
}
|
}
|
||||||
let module_size: i32 = self.module_size(&left_top_black, image);
|
let module_size: i32 = self.module_size(&left_top_black, image);
|
||||||
let mut top: i32 = left_top_black[1];
|
let mut top: i32 = left_top_black[1];
|
||||||
@@ -90,7 +106,7 @@ impl DataMatrixReader {
|
|||||||
let matrix_width: i32 = (right - left + 1) / module_size;
|
let matrix_width: i32 = (right - left + 1) / module_size;
|
||||||
let matrix_height: i32 = (bottom - top + 1) / module_size;
|
let matrix_height: i32 = (bottom - top + 1) / module_size;
|
||||||
if matrix_width <= 0 || matrix_height <= 0 {
|
if matrix_width <= 0 || matrix_height <= 0 {
|
||||||
return Err( NotFoundException::get_not_found_instance());
|
return Err(NotFoundException::get_not_found_instance());
|
||||||
}
|
}
|
||||||
// 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
|
||||||
@@ -116,7 +132,6 @@ impl DataMatrixReader {
|
|||||||
x += 1;
|
x += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
y += 1;
|
y += 1;
|
||||||
}
|
}
|
||||||
@@ -125,7 +140,7 @@ impl DataMatrixReader {
|
|||||||
return Ok(bits);
|
return Ok(bits);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn module_size( left_top_black: &Vec<i32>, image: &BitMatrix) -> Result<i32, NotFoundException> {
|
fn module_size(left_top_black: &Vec<i32>, image: &BitMatrix) -> Result<i32, NotFoundException> {
|
||||||
let width: i32 = image.get_width();
|
let width: i32 = image.get_width();
|
||||||
let mut x: i32 = left_top_black[0];
|
let mut x: i32 = left_top_black[0];
|
||||||
let y: i32 = left_top_black[1];
|
let y: i32 = left_top_black[1];
|
||||||
@@ -133,19 +148,16 @@ impl DataMatrixReader {
|
|||||||
x += 1;
|
x += 1;
|
||||||
}
|
}
|
||||||
if x == width {
|
if x == width {
|
||||||
return Err( NotFoundException::get_not_found_instance());
|
return Err(NotFoundException::get_not_found_instance());
|
||||||
}
|
}
|
||||||
let module_size: i32 = x - left_top_black[0];
|
let module_size: i32 = x - left_top_black[0];
|
||||||
if module_size == 0 {
|
if module_size == 0 {
|
||||||
return Err( NotFoundException::get_not_found_instance());
|
return Err(NotFoundException::get_not_found_instance());
|
||||||
}
|
}
|
||||||
return Ok(module_size);
|
return Ok(module_size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// DataMatrixWriter.java
|
// DataMatrixWriter.java
|
||||||
/**
|
/**
|
||||||
* This object renders a Data Matrix code as a BitMatrix 2D array of greyscale values.
|
* This object renders a Data Matrix code as a BitMatrix 2D array of greyscale values.
|
||||||
@@ -153,27 +165,39 @@ impl DataMatrixReader {
|
|||||||
* @author dswitkin@google.com (Daniel Switkin)
|
* @author dswitkin@google.com (Daniel Switkin)
|
||||||
* @author Guillaume Le Biller Added to zxing lib.
|
* @author Guillaume Le Biller Added to zxing lib.
|
||||||
*/
|
*/
|
||||||
pub struct DataMatrixWriter {
|
pub struct DataMatrixWriter {}
|
||||||
}
|
|
||||||
|
|
||||||
impl Writer for DataMatrixWriter{
|
impl Writer for DataMatrixWriter {
|
||||||
|
fn encode(
|
||||||
fn encode(&self, contents: &String, format: &BarcodeFormat, width: i32, height: i32, hints: &Map<EncodeHintType, _>) -> BitMatrix {
|
&self,
|
||||||
|
contents: &String,
|
||||||
|
format: &BarcodeFormat,
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
hints: &Map<EncodeHintType, _>,
|
||||||
|
) -> BitMatrix {
|
||||||
if contents.is_empty() {
|
if contents.is_empty() {
|
||||||
return Err( IllegalArgumentException::new("Found empty contents"));
|
return Err(IllegalArgumentException::new("Found empty contents"));
|
||||||
}
|
}
|
||||||
if format != BarcodeFormat::DATA_MATRIX {
|
if format != BarcodeFormat::DATA_MATRIX {
|
||||||
return Err( IllegalArgumentException::new(format!("Can only encode DATA_MATRIX, but got {}", format)));
|
return Err(IllegalArgumentException::new(format!(
|
||||||
|
"Can only encode DATA_MATRIX, but got {}",
|
||||||
|
format
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
if width < 0 || height < 0 {
|
if width < 0 || height < 0 {
|
||||||
return Err( IllegalArgumentException::new(format!("Requested dimensions can't be negative: {}x{}", width, height)));
|
return Err(IllegalArgumentException::new(format!(
|
||||||
|
"Requested dimensions can't be negative: {}x{}",
|
||||||
|
width, height
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
// Try to get force shape & min / max size
|
// Try to get force shape & min / max size
|
||||||
let mut shape: SymbolShapeHint = SymbolShapeHint::FORCE_NONE;
|
let mut shape: SymbolShapeHint = SymbolShapeHint::FORCE_NONE;
|
||||||
let min_size: Dimension = null;
|
let min_size: Dimension = null;
|
||||||
let max_size: Dimension = null;
|
let max_size: Dimension = null;
|
||||||
if hints != null {
|
if hints != null {
|
||||||
let requested_shape: SymbolShapeHint = hints.get(EncodeHintType::DATA_MATRIX_SHAPE) as SymbolShapeHint;
|
let requested_shape: SymbolShapeHint =
|
||||||
|
hints.get(EncodeHintType::DATA_MATRIX_SHAPE) as SymbolShapeHint;
|
||||||
if requested_shape != null {
|
if requested_shape != null {
|
||||||
shape = requested_shape;
|
shape = requested_shape;
|
||||||
}
|
}
|
||||||
@@ -188,24 +212,45 @@ impl Writer for DataMatrixWriter{
|
|||||||
}
|
}
|
||||||
//1. step: Data encodation
|
//1. step: Data encodation
|
||||||
let mut encoded: String;
|
let mut encoded: String;
|
||||||
let has_compaction_hint: bool = hints != null && hints.contains_key(EncodeHintType::DATA_MATRIX_COMPACT) && Boolean::parse_boolean(&hints.get(EncodeHintType::DATA_MATRIX_COMPACT).to_string());
|
let has_compaction_hint: bool = hints != null
|
||||||
|
&& hints.contains_key(EncodeHintType::DATA_MATRIX_COMPACT)
|
||||||
|
&& Boolean::parse_boolean(&hints.get(EncodeHintType::DATA_MATRIX_COMPACT).to_string());
|
||||||
if has_compaction_hint {
|
if has_compaction_hint {
|
||||||
let has_g_s1_format_hint: bool = hints.contains_key(EncodeHintType::GS1_FORMAT) && Boolean::parse_boolean(&hints.get(EncodeHintType::GS1_FORMAT).to_string());
|
let has_g_s1_format_hint: bool = hints.contains_key(EncodeHintType::GS1_FORMAT)
|
||||||
|
&& Boolean::parse_boolean(&hints.get(EncodeHintType::GS1_FORMAT).to_string());
|
||||||
let mut charset: Charset = null;
|
let mut charset: Charset = null;
|
||||||
let has_encoding_hint: bool = hints.contains_key(EncodeHintType::CHARACTER_SET);
|
let has_encoding_hint: bool = hints.contains_key(EncodeHintType::CHARACTER_SET);
|
||||||
if has_encoding_hint {
|
if has_encoding_hint {
|
||||||
charset = Charset::for_name(&hints.get(EncodeHintType::CHARACTER_SET).to_string());
|
charset = Charset::for_name(&hints.get(EncodeHintType::CHARACTER_SET).to_string());
|
||||||
}
|
}
|
||||||
encoded = MinimalEncoder::encode_high_level(&contents, &charset, if has_g_s1_format_hint { 0x1D } else { -1 }, &shape);
|
encoded = MinimalEncoder::encode_high_level(
|
||||||
|
&contents,
|
||||||
|
&charset,
|
||||||
|
if has_g_s1_format_hint { 0x1D } else { -1 },
|
||||||
|
&shape,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
let has_force_c40_hint: bool = hints != null && hints.contains_key(EncodeHintType::FORCE_C40) && Boolean::parse_boolean(&hints.get(EncodeHintType::FORCE_C40).to_string());
|
let has_force_c40_hint: bool = hints != null
|
||||||
encoded = HighLevelEncoder::encode_high_level(&contents, shape, min_size, max_size, has_force_c40_hint);
|
&& hints.contains_key(EncodeHintType::FORCE_C40)
|
||||||
|
&& Boolean::parse_boolean(&hints.get(EncodeHintType::FORCE_C40).to_string());
|
||||||
|
encoded = HighLevelEncoder::encode_high_level(
|
||||||
|
&contents,
|
||||||
|
shape,
|
||||||
|
min_size,
|
||||||
|
max_size,
|
||||||
|
has_force_c40_hint,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let symbol_info: SymbolInfo = SymbolInfo::lookup(&encoded.length(), shape, min_size, max_size, true);
|
let symbol_info: SymbolInfo =
|
||||||
|
SymbolInfo::lookup(&encoded.length(), shape, min_size, max_size, true);
|
||||||
//2. step: ECC generation
|
//2. step: ECC generation
|
||||||
let codewords: String = ErrorCorrection::encode_e_c_c200(&encoded, &symbol_info);
|
let codewords: String = ErrorCorrection::encode_e_c_c200(&encoded, &symbol_info);
|
||||||
//3. step: Module placement in Matrix
|
//3. step: Module placement in Matrix
|
||||||
let placement: DefaultPlacement = DefaultPlacement::new(&codewords, &symbol_info.get_symbol_data_width(), &symbol_info.get_symbol_data_height());
|
let placement: DefaultPlacement = DefaultPlacement::new(
|
||||||
|
&codewords,
|
||||||
|
&symbol_info.get_symbol_data_width(),
|
||||||
|
&symbol_info.get_symbol_data_height(),
|
||||||
|
);
|
||||||
placement.place();
|
placement.place();
|
||||||
//4. step: low-level encoding
|
//4. step: low-level encoding
|
||||||
return ::encode_low_level(placement, symbol_info, width, height);
|
return ::encode_low_level(placement, symbol_info, width, height);
|
||||||
@@ -213,8 +258,6 @@ impl Writer for DataMatrixWriter{
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl DataMatrixWriter {
|
impl DataMatrixWriter {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode the given symbol info to a bit matrix.
|
* Encode the given symbol info to a bit matrix.
|
||||||
*
|
*
|
||||||
@@ -222,10 +265,18 @@ impl DataMatrixWriter {
|
|||||||
* @param symbolInfo The symbol info to encode.
|
* @param symbolInfo The symbol info to encode.
|
||||||
* @return The bit matrix generated.
|
* @return The bit matrix generated.
|
||||||
*/
|
*/
|
||||||
fn encode_low_level( placement: &DefaultPlacement, symbol_info: &SymbolInfo, width: i32, height: i32) -> BitMatrix {
|
fn encode_low_level(
|
||||||
|
placement: &DefaultPlacement,
|
||||||
|
symbol_info: &SymbolInfo,
|
||||||
|
width: i32,
|
||||||
|
height: i32,
|
||||||
|
) -> BitMatrix {
|
||||||
let symbol_width: i32 = symbol_info.get_symbol_data_width();
|
let symbol_width: i32 = symbol_info.get_symbol_data_width();
|
||||||
let symbol_height: i32 = symbol_info.get_symbol_data_height();
|
let symbol_height: i32 = symbol_info.get_symbol_data_height();
|
||||||
let matrix: ByteMatrix = ByteMatrix::new(&symbol_info.get_symbol_width(), &symbol_info.get_symbol_height());
|
let matrix: ByteMatrix = ByteMatrix::new(
|
||||||
|
&symbol_info.get_symbol_width(),
|
||||||
|
&symbol_info.get_symbol_height(),
|
||||||
|
);
|
||||||
let matrix_y: i32 = 0;
|
let matrix_y: i32 = 0;
|
||||||
{
|
{
|
||||||
let mut y: i32 = 0;
|
let mut y: i32 = 0;
|
||||||
@@ -303,7 +354,11 @@ impl DataMatrixWriter {
|
|||||||
* @param matrix The input matrix.
|
* @param matrix The input matrix.
|
||||||
* @return The output matrix.
|
* @return The output matrix.
|
||||||
*/
|
*/
|
||||||
fn convert_byte_matrix_to_bit_matrix( matrix: &ByteMatrix, req_width: i32, req_height: i32) -> BitMatrix {
|
fn convert_byte_matrix_to_bit_matrix(
|
||||||
|
matrix: &ByteMatrix,
|
||||||
|
req_width: i32,
|
||||||
|
req_height: i32,
|
||||||
|
) -> BitMatrix {
|
||||||
let matrix_width: i32 = matrix.get_width();
|
let matrix_width: i32 = matrix.get_width();
|
||||||
let matrix_height: i32 = matrix.get_height();
|
let matrix_height: i32 = matrix.get_height();
|
||||||
let output_width: i32 = Math::max(req_width, matrix_width);
|
let output_width: i32 = Math::max(req_width, matrix_width);
|
||||||
@@ -340,7 +395,6 @@ impl DataMatrixWriter {
|
|||||||
output_x += multiple;
|
output_x += multiple;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
input_y += 1;
|
input_y += 1;
|
||||||
output_y += multiple;
|
output_y += multiple;
|
||||||
@@ -350,4 +404,3 @@ impl DataMatrixWriter {
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
use crate::{NotFoundException,ResultPoint};
|
|
||||||
use crate::common::{BitMatrix,DetectorResult,GridSampler};
|
|
||||||
use crate::common::detector::WhiteRectangleDetector;
|
use crate::common::detector::WhiteRectangleDetector;
|
||||||
|
use crate::common::{BitMatrix, DetectorResult, GridSampler};
|
||||||
|
use crate::{NotFoundException, ResultPoint};
|
||||||
|
|
||||||
// Detector.java
|
// Detector.java
|
||||||
/**
|
/**
|
||||||
@@ -10,17 +10,15 @@ use crate::common::detector::WhiteRectangleDetector;
|
|||||||
* @author Sean Owen
|
* @author Sean Owen
|
||||||
*/
|
*/
|
||||||
pub struct Detector {
|
pub struct Detector {
|
||||||
|
|
||||||
image: BitMatrix,
|
image: BitMatrix,
|
||||||
|
|
||||||
rectangle_detector: WhiteRectangleDetector
|
rectangle_detector: WhiteRectangleDetector,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Detector {
|
impl Detector {
|
||||||
|
pub fn new(image: &BitMatrix) -> Result<Self, NotFoundException> {
|
||||||
pub fn new( image: &BitMatrix) -> Result<Self, NotFoundException> {
|
let d: Self;
|
||||||
let d : Self;
|
d.image = image;
|
||||||
d .image = image;
|
|
||||||
d.rectangle_detector = WhiteRectangleDetector::new(image, None, None, None);
|
d.rectangle_detector = WhiteRectangleDetector::new(image, None, None, None);
|
||||||
|
|
||||||
Ok(d)
|
Ok(d)
|
||||||
@@ -38,7 +36,7 @@ impl Detector {
|
|||||||
points = self.detect_solid2(points?);
|
points = self.detect_solid2(points?);
|
||||||
points[3] = self.correct_top_right(points?);
|
points[3] = self.correct_top_right(points?);
|
||||||
if points[3] == null {
|
if points[3] == null {
|
||||||
return Err( NotFoundException::get_not_found_instance());
|
return Err(NotFoundException::get_not_found_instance());
|
||||||
}
|
}
|
||||||
points = self.shift_to_module_center(points?);
|
points = self.shift_to_module_center(points?);
|
||||||
let top_left: ResultPoint = points[0];
|
let top_left: ResultPoint = points[0];
|
||||||
@@ -57,18 +55,28 @@ impl Detector {
|
|||||||
// The matrix is square
|
// The matrix is square
|
||||||
dimension_top = dimension_right = Math::max(dimension_top, dimension_right);
|
dimension_top = dimension_right = Math::max(dimension_top, dimension_right);
|
||||||
}
|
}
|
||||||
let bits: BitMatrix = ::sample_grid(self.image, top_left, bottom_left, bottom_right, top_right, dimension_top, dimension_right);
|
let bits: BitMatrix = ::sample_grid(
|
||||||
return Ok(DetectorResult::new(bits, vec![top_left, bottom_left, bottom_right, top_right, ]
|
self.image,
|
||||||
|
top_left,
|
||||||
|
bottom_left,
|
||||||
|
bottom_right,
|
||||||
|
top_right,
|
||||||
|
dimension_top,
|
||||||
|
dimension_right,
|
||||||
|
);
|
||||||
|
return Ok(DetectorResult::new(
|
||||||
|
bits,
|
||||||
|
vec![top_left, bottom_left, bottom_right, top_right],
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn shift_point( point: &ResultPoint, to: &ResultPoint, div: i32) -> ResultPoint {
|
fn shift_point(point: &ResultPoint, to: &ResultPoint, div: i32) -> ResultPoint {
|
||||||
let x: f32 = (to.get_x() - point.get_x()) / (div + 1);
|
let x: f32 = (to.get_x() - point.get_x()) / (div + 1);
|
||||||
let y: f32 = (to.get_y() - point.get_y()) / (div + 1);
|
let y: f32 = (to.get_y() - point.get_y()) / (div + 1);
|
||||||
return ResultPoint::new(point.get_x() + x, point.get_y() + y);
|
return ResultPoint::new(point.get_x() + x, point.get_y() + y);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_away( point: &ResultPoint, from_x: f32, from_y: f32) -> ResultPoint {
|
fn move_away(point: &ResultPoint, from_x: f32, from_y: f32) -> ResultPoint {
|
||||||
let mut x: f32 = point.get_x();
|
let mut x: f32 = point.get_x();
|
||||||
let mut y: f32 = point.get_y();
|
let mut y: f32 = point.get_y();
|
||||||
if x < from_x {
|
if x < from_x {
|
||||||
@@ -102,8 +110,7 @@ impl Detector {
|
|||||||
// : :
|
// : :
|
||||||
// 1--2
|
// 1--2
|
||||||
let mut min: i32 = tr_a_b;
|
let mut min: i32 = tr_a_b;
|
||||||
let mut points: vec![Vec<ResultPoint>; 4] = vec![point_d, point_a, point_b, point_c, ]
|
let mut points: vec![Vec<ResultPoint>; 4] = vec![point_d, point_a, point_b, point_c];
|
||||||
;
|
|
||||||
if min > tr_b_c {
|
if min > tr_b_c {
|
||||||
min = tr_b_c;
|
min = tr_b_c;
|
||||||
points[0] = point_a;
|
points[0] = point_a;
|
||||||
@@ -180,8 +187,14 @@ impl Detector {
|
|||||||
let point_cs: ResultPoint = ::shift_point(point_c, point_b, (tr_top + 1) * 4);
|
let point_cs: ResultPoint = ::shift_point(point_c, point_b, (tr_top + 1) * 4);
|
||||||
tr_top = self.transitions_between(&point_as, &point_d);
|
tr_top = self.transitions_between(&point_as, &point_d);
|
||||||
tr_right = self.transitions_between(&point_cs, &point_d);
|
tr_right = self.transitions_between(&point_cs, &point_d);
|
||||||
let candidate1: ResultPoint = ResultPoint::new(point_d.get_x() + (point_c.get_x() - point_b.get_x()) / (tr_top + 1), point_d.get_y() + (point_c.get_y() - point_b.get_y()) / (tr_top + 1));
|
let candidate1: ResultPoint = ResultPoint::new(
|
||||||
let candidate2: ResultPoint = ResultPoint::new(point_d.get_x() + (point_a.get_x() - point_b.get_x()) / (tr_right + 1), point_d.get_y() + (point_a.get_y() - point_b.get_y()) / (tr_right + 1));
|
point_d.get_x() + (point_c.get_x() - point_b.get_x()) / (tr_top + 1),
|
||||||
|
point_d.get_y() + (point_c.get_y() - point_b.get_y()) / (tr_top + 1),
|
||||||
|
);
|
||||||
|
let candidate2: ResultPoint = ResultPoint::new(
|
||||||
|
point_d.get_x() + (point_a.get_x() - point_b.get_x()) / (tr_right + 1),
|
||||||
|
point_d.get_y() + (point_a.get_y() - point_b.get_y()) / (tr_right + 1),
|
||||||
|
);
|
||||||
if !self.is_valid(&candidate1) {
|
if !self.is_valid(&candidate1) {
|
||||||
if self.is_valid(&candidate2) {
|
if self.is_valid(&candidate2) {
|
||||||
return candidate2;
|
return candidate2;
|
||||||
@@ -191,8 +204,10 @@ impl Detector {
|
|||||||
if !self.is_valid(&candidate2) {
|
if !self.is_valid(&candidate2) {
|
||||||
return candidate1;
|
return candidate1;
|
||||||
}
|
}
|
||||||
let sumc1: i32 = self.transitions_between(&point_as, &candidate1) + self.transitions_between(&point_cs, &candidate1);
|
let sumc1: i32 = self.transitions_between(&point_as, &candidate1)
|
||||||
let sumc2: i32 = self.transitions_between(&point_as, &candidate2) + self.transitions_between(&point_cs, &candidate2);
|
+ self.transitions_between(&point_cs, &candidate1);
|
||||||
|
let sumc2: i32 = self.transitions_between(&point_as, &candidate2)
|
||||||
|
+ self.transitions_between(&point_cs, &candidate2);
|
||||||
if sumc1 > sumc2 {
|
if sumc1 > sumc2 {
|
||||||
return candidate1;
|
return candidate1;
|
||||||
} else {
|
} else {
|
||||||
@@ -228,8 +243,10 @@ impl Detector {
|
|||||||
}
|
}
|
||||||
// WhiteRectangleDetector returns points inside of the rectangle.
|
// WhiteRectangleDetector returns points inside of the rectangle.
|
||||||
// I want points on the edges.
|
// I want points on the edges.
|
||||||
let center_x: f32 = (point_a.get_x() + point_b.get_x() + point_c.get_x() + point_d.get_x()) / 4;
|
let center_x: f32 =
|
||||||
let center_y: f32 = (point_a.get_y() + point_b.get_y() + point_c.get_y() + point_d.get_y()) / 4;
|
(point_a.get_x() + point_b.get_x() + point_c.get_x() + point_d.get_x()) / 4;
|
||||||
|
let center_y: f32 =
|
||||||
|
(point_a.get_y() + point_b.get_y() + point_c.get_y() + point_d.get_y()) / 4;
|
||||||
point_a = ::move_away(point_a, center_x, center_y);
|
point_a = ::move_away(point_a, center_x, center_y);
|
||||||
point_b = ::move_away(point_b, center_x, center_y);
|
point_b = ::move_away(point_b, center_x, center_y);
|
||||||
point_c = ::move_away(point_c, center_x, center_y);
|
point_c = ::move_away(point_c, center_x, center_y);
|
||||||
@@ -245,17 +262,47 @@ impl Detector {
|
|||||||
point_cs = ::shift_point(point_cs, point_b, dim_h * 4);
|
point_cs = ::shift_point(point_cs, point_b, dim_h * 4);
|
||||||
point_ds = ::shift_point(point_d, point_c, dim_v * 4);
|
point_ds = ::shift_point(point_d, point_c, dim_v * 4);
|
||||||
point_ds = ::shift_point(point_ds, point_a, dim_h * 4);
|
point_ds = ::shift_point(point_ds, point_a, dim_h * 4);
|
||||||
return vec![point_as, point_bs, point_cs, point_ds, ]
|
return vec![point_as, point_bs, point_cs, point_ds];
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_valid(&self, p: &ResultPoint) -> bool {
|
fn is_valid(&self, p: &ResultPoint) -> bool {
|
||||||
return p.get_x() >= 0 && p.get_x() <= self.image.get_width() - 1 && p.get_y() > 0 && p.get_y() <= self.image.get_height() - 1;
|
return p.get_x() >= 0
|
||||||
|
&& p.get_x() <= self.image.get_width() - 1
|
||||||
|
&& p.get_y() > 0
|
||||||
|
&& p.get_y() <= self.image.get_height() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sample_grid( image: &BitMatrix, top_left: &ResultPoint, bottom_left: &ResultPoint, bottom_right: &ResultPoint, top_right: &ResultPoint, dimension_x: i32, dimension_y: i32) -> /* throws NotFoundException */Result<BitMatrix, Rc<Exception>> {
|
fn sample_grid(
|
||||||
|
image: &BitMatrix,
|
||||||
|
top_left: &ResultPoint,
|
||||||
|
bottom_left: &ResultPoint,
|
||||||
|
bottom_right: &ResultPoint,
|
||||||
|
top_right: &ResultPoint,
|
||||||
|
dimension_x: i32,
|
||||||
|
dimension_y: i32,
|
||||||
|
) -> Result<BitMatrix, Rc<Exception>> {
|
||||||
let sampler: GridSampler = GridSampler::get_instance();
|
let sampler: GridSampler = GridSampler::get_instance();
|
||||||
return Ok(sampler.sample_grid(image, dimension_x, dimension_y, 0.5f32, 0.5f32, dimension_x - 0.5f32, 0.5f32, dimension_x - 0.5f32, dimension_y - 0.5f32, 0.5f32, dimension_y - 0.5f32, &top_left.get_x(), &top_left.get_y(), &top_right.get_x(), &top_right.get_y(), &bottom_right.get_x(), &bottom_right.get_y(), &bottom_left.get_x(), &bottom_left.get_y()));
|
return Ok(sampler.sample_grid(
|
||||||
|
image,
|
||||||
|
dimension_x,
|
||||||
|
dimension_y,
|
||||||
|
0.5f32,
|
||||||
|
0.5f32,
|
||||||
|
dimension_x - 0.5f32,
|
||||||
|
0.5f32,
|
||||||
|
dimension_x - 0.5f32,
|
||||||
|
dimension_y - 0.5f32,
|
||||||
|
0.5f32,
|
||||||
|
dimension_y - 0.5f32,
|
||||||
|
&top_left.get_x(),
|
||||||
|
&top_left.get_y(),
|
||||||
|
&top_right.get_x(),
|
||||||
|
&top_right.get_y(),
|
||||||
|
&bottom_right.get_x(),
|
||||||
|
&bottom_right.get_y(),
|
||||||
|
&bottom_left.get_x(),
|
||||||
|
&bottom_left.get_y(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -282,13 +329,18 @@ impl Detector {
|
|||||||
let ystep: i32 = if from_y < to_y { 1 } else { -1 };
|
let ystep: i32 = if from_y < to_y { 1 } else { -1 };
|
||||||
let xstep: i32 = if from_x < to_x { 1 } else { -1 };
|
let xstep: i32 = if from_x < to_x { 1 } else { -1 };
|
||||||
let mut transitions: i32 = 0;
|
let mut transitions: i32 = 0;
|
||||||
let in_black: bool = self.image.get( if steep { from_y } else { from_x }, if steep { from_x } else { from_y });
|
let in_black: bool = self.image.get(
|
||||||
|
if steep { from_y } else { from_x },
|
||||||
|
if steep { from_x } else { from_y },
|
||||||
|
);
|
||||||
{
|
{
|
||||||
let mut x: i32 = from_x;
|
let mut x: i32 = from_x;
|
||||||
let mut y: i32 = from_y;
|
let mut y: i32 = from_y;
|
||||||
while x != to_x {
|
while x != to_x {
|
||||||
{
|
{
|
||||||
let is_black: bool = self.image.get( if steep { y } else { x }, if steep { x } else { y });
|
let is_black: bool = self
|
||||||
|
.image
|
||||||
|
.get(if steep { y } else { x }, if steep { x } else { y });
|
||||||
if is_black != in_black {
|
if is_black != in_black {
|
||||||
transitions += 1;
|
transitions += 1;
|
||||||
in_black = is_black;
|
in_black = is_black;
|
||||||
@@ -309,4 +361,3 @@ impl Detector {
|
|||||||
return transitions;
|
return transitions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user