mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 12:22:34 +00:00
itf writer + tests
This commit is contained in:
@@ -1,88 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.oned;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* This object renders a ITF code as a {@link BitMatrix}.
|
||||
*
|
||||
* @author erik.barbara@gmail.com (Erik Barbara)
|
||||
*/
|
||||
public final class ITFWriter extends OneDimensionalCodeWriter {
|
||||
|
||||
private static final int[] START_PATTERN = {1, 1, 1, 1};
|
||||
private static final int[] END_PATTERN = {3, 1, 1};
|
||||
|
||||
private static final int W = 3; // Pixel width of a 3x wide line
|
||||
private static final int N = 1; // Pixed width of a narrow line
|
||||
|
||||
// See ITFReader.PATTERNS
|
||||
|
||||
private static final int[][] PATTERNS = {
|
||||
{N, N, W, W, N}, // 0
|
||||
{W, N, N, N, W}, // 1
|
||||
{N, W, N, N, W}, // 2
|
||||
{W, W, N, N, N}, // 3
|
||||
{N, N, W, N, W}, // 4
|
||||
{W, N, W, N, N}, // 5
|
||||
{N, W, W, N, N}, // 6
|
||||
{N, N, N, W, W}, // 7
|
||||
{W, N, N, W, N}, // 8
|
||||
{N, W, N, W, N} // 9
|
||||
};
|
||||
|
||||
@Override
|
||||
protected Collection<BarcodeFormat> getSupportedWriteFormats() {
|
||||
return Collections.singleton(BarcodeFormat.ITF);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean[] encode(String contents) {
|
||||
int length = contents.length();
|
||||
if (length % 2 != 0) {
|
||||
throw new IllegalArgumentException("The length of the input should be even");
|
||||
}
|
||||
if (length > 80) {
|
||||
throw new IllegalArgumentException(
|
||||
"Requested contents should be less than 80 digits long, but got " + length);
|
||||
}
|
||||
|
||||
checkNumeric(contents);
|
||||
|
||||
boolean[] result = new boolean[9 + 9 * length];
|
||||
int pos = appendPattern(result, 0, START_PATTERN, true);
|
||||
for (int i = 0; i < length; i += 2) {
|
||||
int one = Character.digit(contents.charAt(i), 10);
|
||||
int two = Character.digit(contents.charAt(i + 1), 10);
|
||||
int[] encoding = new int[10];
|
||||
for (int j = 0; j < 5; j++) {
|
||||
encoding[2 * j] = PATTERNS[one][j];
|
||||
encoding[2 * j + 1] = PATTERNS[two][j];
|
||||
}
|
||||
pos += appendPattern(result, pos, encoding, true);
|
||||
}
|
||||
appendPattern(result, pos, END_PATTERN, true);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
/*
|
||||
* Copyright 2017 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.google.zxing.oned;
|
||||
|
||||
import com.google.zxing.BarcodeFormat;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
import com.google.zxing.common.BitMatrixTestCase;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link ITFWriter}.
|
||||
*/
|
||||
public final class ITFWriterTestCase extends Assert {
|
||||
|
||||
@Test
|
||||
public void testEncode() {
|
||||
doTest("00123456789012",
|
||||
"0000010101010111000111000101110100010101110001110111010001010001110100011" +
|
||||
"100010101000101011100011101011101000111000101110100010101110001110100000");
|
||||
}
|
||||
|
||||
private static void doTest(String input, CharSequence expected) {
|
||||
BitMatrix result = new ITFWriter().encode(input, BarcodeFormat.ITF, 0, 0);
|
||||
assertEquals(expected, BitMatrixTestCase.matrixToString(result));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testEncodeIllegalCharacters() {
|
||||
new ITFWriter().encode("00123456789abc", BarcodeFormat.ITF, 0, 0);
|
||||
}
|
||||
|
||||
}
|
||||
128
src/oned/itf_writer.rs
Normal file
128
src/oned/itf_writer.rs
Normal file
@@ -0,0 +1,128 @@
|
||||
/*
|
||||
* Copyright 2010 ZXing authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use one_d_reader_derive::OneDWriter;
|
||||
|
||||
use crate::BarcodeFormat;
|
||||
|
||||
use super::OneDimensionalCodeWriter;
|
||||
|
||||
|
||||
/**
|
||||
* This object renders a ITF code as a {@link BitMatrix}.
|
||||
*
|
||||
* @author erik.barbara@gmail.com (Erik Barbara)
|
||||
*/
|
||||
#[derive(OneDWriter)]
|
||||
pub struct ITFWriter;
|
||||
|
||||
impl Default for ITFWriter {
|
||||
fn default() -> Self {
|
||||
Self { }
|
||||
}
|
||||
}
|
||||
|
||||
impl OneDimensionalCodeWriter for ITFWriter {
|
||||
fn encode_oned(&self, contents: &str) -> Result<Vec<bool>, Exceptions> {
|
||||
let length = contents.chars().count();
|
||||
if length % 2 != 0 {
|
||||
return Err(Exceptions::IllegalArgumentException("The length of the input should be even".to_owned()));
|
||||
}
|
||||
if length > 80 {
|
||||
return Err(Exceptions::IllegalArgumentException(format!("Requested contents should be less than 80 digits long, but got {}" , length)));
|
||||
}
|
||||
|
||||
Self::checkNumeric(contents)?;
|
||||
|
||||
let mut result = vec![false;9 + 9 * length];
|
||||
let mut pos = Self::appendPattern(&mut result, 0, &START_PATTERN, true) as usize;
|
||||
let mut i = 0;
|
||||
while i < length {
|
||||
// for (int i = 0; i < length; i += 2) {
|
||||
let one = contents.chars().nth(i).unwrap().to_digit(10).unwrap() as usize;
|
||||
let two = contents.chars().nth(i + 1).unwrap().to_digit(10).unwrap() as usize;
|
||||
let mut encoding = [0;10];//new int[10];
|
||||
for j in 0..5 {
|
||||
// for (int j = 0; j < 5; j++) {
|
||||
encoding[2 * j] = PATTERNS[one][j];
|
||||
encoding[2 * j + 1] = PATTERNS[two][j];
|
||||
}
|
||||
pos += Self::appendPattern(&mut result, pos, &encoding, true) as usize;
|
||||
|
||||
i+=2;
|
||||
}
|
||||
Self::appendPattern(&mut result, pos, &END_PATTERN, true);
|
||||
|
||||
Ok( result)
|
||||
}
|
||||
fn getSupportedWriteFormats(&self) -> Option<Vec<crate::BarcodeFormat>> {
|
||||
Some(vec![BarcodeFormat::ITF])
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
const START_PATTERN : [usize;4]= [1, 1, 1, 1];
|
||||
const END_PATTERN :[usize;3]= [3, 1, 1];
|
||||
|
||||
const W : usize= 3; // Pixel width of a 3x wide line
|
||||
const N :usize= 1; // Pixed width of a narrow line
|
||||
|
||||
// See ITFReader.PATTERNS
|
||||
|
||||
const PATTERNS : [[usize;5];10]= [
|
||||
[N, N, W, W, N], // 0
|
||||
[W, N, N, N, W], // 1
|
||||
[N, W, N, N, W], // 2
|
||||
[W, W, N, N, N], // 3
|
||||
[N, N, W, N, W], // 4
|
||||
[W, N, W, N, N], // 5
|
||||
[N, W, W, N, N], // 6
|
||||
[N, N, N, W, W], // 7
|
||||
[W, N, N, W, N], // 8
|
||||
[N, W, N, W, N] // 9
|
||||
];
|
||||
|
||||
/**
|
||||
* Tests {@link ITFWriter}.
|
||||
*/
|
||||
#[cfg(test)]
|
||||
mod ITFWriterTestCase {
|
||||
use crate::{Writer, BarcodeFormat, common::BitMatrixTestCase};
|
||||
|
||||
use super::ITFWriter;
|
||||
|
||||
|
||||
#[test]
|
||||
fn testEncode() {
|
||||
doTest("00123456789012",
|
||||
"0000010101010111000111000101110100010101110001110111010001010001110100011\
|
||||
100010101000101011100011101011101000111000101110100010101110001110100000");
|
||||
}
|
||||
|
||||
fn doTest( input:&str, expected:&str) {
|
||||
let result = ITFWriter::default().encode(input, &BarcodeFormat::ITF, 0, 0).expect("encode");
|
||||
assert_eq!(expected, BitMatrixTestCase::matrix_to_string(&result));
|
||||
}
|
||||
|
||||
//@Test(expected = IllegalArgumentException.class)
|
||||
#[test]
|
||||
#[should_panic]
|
||||
fn testEncodeIllegalCharacters() {
|
||||
ITFWriter::default().encode("00123456789abc", &BarcodeFormat::ITF, 0, 0).expect("should fail");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -61,3 +61,6 @@ pub use code_39_writer::*;
|
||||
|
||||
mod code_93_writer;
|
||||
pub use code_93_writer::*;
|
||||
|
||||
mod itf_writer;
|
||||
pub use itf_writer::*;
|
||||
Reference in New Issue
Block a user