format datamatrix

This commit is contained in:
Henry Schimke
2022-08-19 17:31:02 -05:00
parent b8c45efe7f
commit 7d97055747
4 changed files with 4718 additions and 3621 deletions

View File

@@ -2,13 +2,19 @@ pub mod decoder;
pub mod detector;
pub mod encoder;
use crate::{BarcodeFormat,BinaryBitmap,DecodeHintType,ChecksumException,FormatException,NotFoundException,Reader,RXingResult,ResultMetadataType,ResultPoint,EncodeHintType,Writer,Dimension};
use crate::common::{DecoderResult,BitMatrix,DetectorResult,};
use crate::datamatrix::decoder::{Decoder};
use crate::datamatrix::detector::{Detector};
use crate::datamatrix::encoder::{DefaultPlacement,ErrorCorrection,HighLevelEncoder,MinimalEncoder,SymbolInfo,SymbolShapeHint};
use crate::common::{BitMatrix, DecoderResult, DetectorResult};
use crate::datamatrix::decoder::Decoder;
use crate::datamatrix::detector::Detector;
use crate::datamatrix::encoder::{
DefaultPlacement, ErrorCorrection, HighLevelEncoder, MinimalEncoder, SymbolInfo,
SymbolShapeHint,
};
use crate::qrcode::encoder::ByteMatrix;
use crate::{
BarcodeFormat, BinaryBitmap, ChecksumException, DecodeHintType, Dimension, EncodeHintType,
FormatException, NotFoundException, RXingResult, Reader, ResultMetadataType, ResultPoint,
Writer,
};
// DataMatrixReader.java
/**
@@ -19,8 +25,7 @@ use crate::qrcode::encoder::ByteMatrix;
const NO_POINTS: [Option<ResultPoint>; 0] = [None; 0];
pub struct DataMatrixReader {
decoder: Decoder
decoder: Decoder,
}
impl Reader for DataMatrixReader {
@@ -32,7 +37,11 @@ impl Reader for DataMatrixReader{
* @throws FormatException if a Data Matrix code cannot be decoded
* @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 mut points: Vec<ResultPoint>;
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());
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();
if byte_segments != null {
result.put_metadata(ResultMetadataType::BYTE_SEGMENTS, &byte_segments);
@@ -53,7 +67,10 @@ impl Reader for DataMatrixReader{
if ec_level != null {
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);
}
@@ -63,7 +80,6 @@ impl Reader for DataMatrixReader{
}
impl DataMatrixReader {
pub fn new() -> Self {
Self {
decoder: Decoder::new(),
@@ -116,7 +132,6 @@ impl DataMatrixReader {
x += 1;
}
}
}
y += 1;
}
@@ -143,9 +158,6 @@ impl DataMatrixReader {
}
}
// DataMatrixWriter.java
/**
* 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 Guillaume Le Biller Added to zxing lib.
*/
pub struct DataMatrixWriter {
}
pub struct DataMatrixWriter {}
impl Writer for DataMatrixWriter {
fn encode(&self, contents: &String, format: &BarcodeFormat, width: i32, height: i32, hints: &Map<EncodeHintType, _>) -> BitMatrix {
fn encode(
&self,
contents: &String,
format: &BarcodeFormat,
width: i32,
height: i32,
hints: &Map<EncodeHintType, _>,
) -> BitMatrix {
if contents.is_empty() {
return Err(IllegalArgumentException::new("Found empty contents"));
}
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 {
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
let mut shape: SymbolShapeHint = SymbolShapeHint::FORCE_NONE;
let min_size: Dimension = null;
let max_size: Dimension = 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 {
shape = requested_shape;
}
@@ -188,24 +212,45 @@ impl Writer for DataMatrixWriter{
}
//1. step: Data encodation
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 {
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 has_encoding_hint: bool = hints.contains_key(EncodeHintType::CHARACTER_SET);
if has_encoding_hint {
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 {
let has_force_c40_hint: bool = hints != null && 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 has_force_c40_hint: bool = hints != null
&& 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
let codewords: String = ErrorCorrection::encode_e_c_c200(&encoded, &symbol_info);
//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();
//4. step: low-level encoding
return ::encode_low_level(placement, symbol_info, width, height);
@@ -213,8 +258,6 @@ impl Writer for DataMatrixWriter{
}
impl DataMatrixWriter {
/**
* Encode the given symbol info to a bit matrix.
*
@@ -222,10 +265,18 @@ impl DataMatrixWriter {
* @param symbolInfo The symbol info to encode.
* @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_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 mut y: i32 = 0;
@@ -303,7 +354,11 @@ impl DataMatrixWriter {
* @param matrix The input 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_height: i32 = matrix.get_height();
let output_width: i32 = Math::max(req_width, matrix_width);
@@ -340,7 +395,6 @@ impl DataMatrixWriter {
output_x += multiple;
}
}
}
input_y += 1;
output_y += multiple;
@@ -350,4 +404,3 @@ impl DataMatrixWriter {
return output;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
use crate::{NotFoundException,ResultPoint};
use crate::common::{BitMatrix,DetectorResult,GridSampler};
use crate::common::detector::WhiteRectangleDetector;
use crate::common::{BitMatrix, DetectorResult, GridSampler};
use crate::{NotFoundException, ResultPoint};
// Detector.java
/**
@@ -10,14 +10,12 @@ use crate::common::detector::WhiteRectangleDetector;
* @author Sean Owen
*/
pub struct Detector {
image: BitMatrix,
rectangle_detector: WhiteRectangleDetector
rectangle_detector: WhiteRectangleDetector,
}
impl Detector {
pub fn new(image: &BitMatrix) -> Result<Self, NotFoundException> {
let d: Self;
d.image = image;
@@ -57,8 +55,18 @@ impl Detector {
// The matrix is square
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);
return Ok(DetectorResult::new(bits, vec![top_left, bottom_left, bottom_right, top_right, ]
let bits: BitMatrix = ::sample_grid(
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],
));
}
@@ -102,8 +110,7 @@ impl Detector {
// : :
// 1--2
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 {
min = tr_b_c;
points[0] = point_a;
@@ -180,8 +187,14 @@ impl Detector {
let point_cs: ResultPoint = ::shift_point(point_c, point_b, (tr_top + 1) * 4);
tr_top = self.transitions_between(&point_as, &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 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));
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 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(&candidate2) {
return candidate2;
@@ -191,8 +204,10 @@ impl Detector {
if !self.is_valid(&candidate2) {
return candidate1;
}
let sumc1: i32 = self.transitions_between(&point_as, &candidate1) + self.transitions_between(&point_cs, &candidate1);
let sumc2: i32 = self.transitions_between(&point_as, &candidate2) + self.transitions_between(&point_cs, &candidate2);
let sumc1: i32 = self.transitions_between(&point_as, &candidate1)
+ self.transitions_between(&point_cs, &candidate1);
let sumc2: i32 = self.transitions_between(&point_as, &candidate2)
+ self.transitions_between(&point_cs, &candidate2);
if sumc1 > sumc2 {
return candidate1;
} else {
@@ -228,8 +243,10 @@ impl Detector {
}
// WhiteRectangleDetector returns points inside of the rectangle.
// 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_y: f32 = (point_a.get_y() + point_b.get_y() + point_c.get_y() + point_d.get_y()) / 4;
let center_x: f32 =
(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_b = ::move_away(point_b, 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_ds = ::shift_point(point_d, point_c, dim_v * 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 {
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();
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 xstep: i32 = if from_x < to_x { 1 } else { -1 };
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 y: i32 = from_y;
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 {
transitions += 1;
in_black = is_black;
@@ -309,4 +361,3 @@ impl Detector {
return transitions;
}
}

File diff suppressed because it is too large Load Diff