mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
Code tidy-up
This commit is contained in:
@@ -91,3 +91,6 @@ pub use ean_8_writer::*;
|
|||||||
|
|
||||||
mod upc_e_writer;
|
mod upc_e_writer;
|
||||||
pub use upc_e_writer::*;
|
pub use upc_e_writer::*;
|
||||||
|
|
||||||
|
mod telepen_common;
|
||||||
|
pub use telepen_common::*;
|
||||||
20
src/oned/telepen_common.rs
Normal file
20
src/oned/telepen_common.rs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
pub struct TelepenCommon;
|
||||||
|
|
||||||
|
impl TelepenCommon {
|
||||||
|
pub fn calculate_checksum(contents: &str) -> char {
|
||||||
|
let mut sum = 0;
|
||||||
|
|
||||||
|
for c in contents.chars() {
|
||||||
|
sum += c as u32;
|
||||||
|
}
|
||||||
|
|
||||||
|
let remainder = sum % 127;
|
||||||
|
let diff = 127 - remainder;
|
||||||
|
return if diff != 127 {
|
||||||
|
diff as u8 as char
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
0 as char
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ use crate::common::{BitArray, Result};
|
|||||||
use crate::Exceptions;
|
use crate::Exceptions;
|
||||||
use crate::RXingResult;
|
use crate::RXingResult;
|
||||||
use crate::{point_f, BarcodeFormat};
|
use crate::{point_f, BarcodeFormat};
|
||||||
|
use crate::oned::telepen_common::TelepenCommon;
|
||||||
use bit_reverse::ParallelReverse;
|
use bit_reverse::ParallelReverse;
|
||||||
|
|
||||||
use super::OneDReader;
|
use super::OneDReader;
|
||||||
@@ -48,7 +49,7 @@ impl Default for TelepenReader {
|
|||||||
impl OneDReader for TelepenReader {
|
impl OneDReader for TelepenReader {
|
||||||
fn decode_row(
|
fn decode_row(
|
||||||
&mut self,
|
&mut self,
|
||||||
_rowNumber: u32,
|
rowNumber: u32,
|
||||||
row: &crate::common::BitArray,
|
row: &crate::common::BitArray,
|
||||||
_hints: &crate::DecodingHintDictionary,
|
_hints: &crate::DecodingHintDictionary,
|
||||||
) -> Result<crate::RXingResult> {
|
) -> Result<crate::RXingResult> {
|
||||||
@@ -218,34 +219,32 @@ impl OneDReader for TelepenReader {
|
|||||||
// Penultimate byte is a block check character.
|
// Penultimate byte is a block check character.
|
||||||
let check = bytes[byteLength - 2];
|
let check = bytes[byteLength - 2];
|
||||||
|
|
||||||
// Calculate the checksum character
|
let checksum = TelepenCommon::calculate_checksum(&contentString);
|
||||||
let mut sum = 0;
|
|
||||||
|
|
||||||
for c in contentString.chars() {
|
|
||||||
sum += c as u32;
|
|
||||||
}
|
|
||||||
|
|
||||||
let remainder = sum % 127;
|
|
||||||
let diff = 127 - remainder;
|
|
||||||
let checksum = if diff != 127 {
|
|
||||||
diff as u8 as char
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
0 as char
|
|
||||||
};
|
|
||||||
|
|
||||||
// Validate checksum
|
// Validate checksum
|
||||||
if check != checksum as u8 {
|
if check != checksum as u8 {
|
||||||
return Err(Exceptions::NOT_FOUND);
|
return Err(Exceptions::NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let mut runningCount = 0;
|
||||||
|
runningCount += self.counters.iter().take(startOffset).sum::<u32>();
|
||||||
|
let left: f32 = runningCount as f32;
|
||||||
|
|
||||||
|
runningCount += self
|
||||||
|
.counters
|
||||||
|
.iter()
|
||||||
|
.skip(startOffset)
|
||||||
|
.take(self.counterLength - startOffset - end)
|
||||||
|
.sum::<u32>();
|
||||||
|
|
||||||
|
let right: f32 = runningCount as f32;
|
||||||
|
|
||||||
let mut result = RXingResult::new(
|
let mut result = RXingResult::new(
|
||||||
&contentString,
|
&contentString,
|
||||||
bytes,
|
bytes,
|
||||||
vec![
|
vec![
|
||||||
// TODO: populate with real numbers
|
point_f(left, rowNumber as f32),
|
||||||
point_f(0 as f32, 0 as f32),
|
point_f(right, rowNumber as f32),
|
||||||
point_f(0 as f32, 0 as f32),
|
|
||||||
],
|
],
|
||||||
BarcodeFormat::TELEPEN,
|
BarcodeFormat::TELEPEN,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ use rxing_one_d_proc_derive::OneDWriter;
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use crate::common::Result;
|
use crate::common::Result;
|
||||||
use crate::BarcodeFormat;
|
use crate::BarcodeFormat;
|
||||||
|
use crate::oned::telepen_common::TelepenCommon;
|
||||||
|
|
||||||
use super::OneDimensionalCodeWriter;
|
use super::OneDimensionalCodeWriter;
|
||||||
|
|
||||||
@@ -32,77 +33,30 @@ pub struct TelepenWriter;
|
|||||||
impl OneDimensionalCodeWriter for TelepenWriter {
|
impl OneDimensionalCodeWriter for TelepenWriter {
|
||||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
|
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>> {
|
||||||
// Calculate the checksum character
|
// Calculate the checksum character
|
||||||
let mut sum = 0;
|
let checksum = TelepenCommon::calculate_checksum(contents);
|
||||||
|
|
||||||
for c in contents.chars() {
|
|
||||||
sum += c as u32;
|
|
||||||
}
|
|
||||||
|
|
||||||
let remainder = sum % 127;
|
|
||||||
let diff = 127 - remainder;
|
|
||||||
let checksum = if diff != 127 {
|
|
||||||
diff as u8 as char
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
0 as char
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build binary string
|
// Build binary string
|
||||||
let mut binary = String::new();
|
let mut binary = String::new();
|
||||||
|
|
||||||
// Opening character is always _
|
// Opening character is always _
|
||||||
let mut byte = "_".as_bytes()[0];
|
binary = self.add_to_binary('_', binary);
|
||||||
let mut bits = self.get_bits(byte);
|
|
||||||
|
|
||||||
for i in 0..8 {
|
|
||||||
binary.push(if bits[i] {
|
|
||||||
'1'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
'0'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Content
|
// Content
|
||||||
for index in 0..contents.chars().count() {
|
for c in contents.chars() {
|
||||||
byte = contents.chars().nth(index).unwrap() as u8;
|
if c as u8 > 127 {
|
||||||
bits = self.get_bits(byte);
|
return Err(Exceptions::illegal_argument_with(format!(
|
||||||
|
"Telepen only supports ASCII characters."
|
||||||
for i in 0..8 {
|
)));
|
||||||
binary.push(if bits[i] {
|
|
||||||
'1'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
'0'
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
binary = self.add_to_binary(c, binary)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checksum
|
// Checksum
|
||||||
byte = checksum as u8;
|
binary = self.add_to_binary(checksum, binary);
|
||||||
bits = self.get_bits(byte);
|
|
||||||
|
|
||||||
for i in 0..8 {
|
|
||||||
binary.push(if bits[i] {
|
|
||||||
'1'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
'0'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Closing character is always z.
|
// Closing character is always z.
|
||||||
byte = "z".as_bytes()[0];
|
binary = self.add_to_binary('z', binary);
|
||||||
bits = self.get_bits(byte);
|
|
||||||
|
|
||||||
for i in 0..8 {
|
|
||||||
binary.push(if bits[i] {
|
|
||||||
'1'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
'0'
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let re = Regex::new(r"^01|10$|01*0|00|1").unwrap();
|
let re = Regex::new(r"^01|10$|01*0|00|1").unwrap();
|
||||||
let matches: Vec<&str> = re.find_iter(&binary).map(|m| m.as_str()).collect();
|
let matches: Vec<&str> = re.find_iter(&binary).map(|m| m.as_str()).collect();
|
||||||
@@ -214,6 +168,22 @@ impl TelepenWriter {
|
|||||||
|
|
||||||
return bits;
|
return bits;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_to_binary(&self, c: char, mut binary: String) -> String {
|
||||||
|
let byte = c as u8;
|
||||||
|
let bits = self.get_bits(byte);
|
||||||
|
|
||||||
|
for i in 0..8 {
|
||||||
|
binary.push(if bits[i] {
|
||||||
|
'1'
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
'0'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return binary;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user