diff options
| author | AlexGeller1 <87009702+AlexGeller1@users.noreply.github.com> | 2022-03-05 17:07:37 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-05 17:07:37 +0300 |
| commit | 92854d4a5553465ff42e68927f3a161bb8977e98 (patch) | |
| tree | 71a36ad3199acb603d3a284c1031756d1ff7bdff /core/src/test/java/com/google | |
| parent | 4bd257e8c5577036b38352082bde646045bf95ee (diff) | |
| download | zxing-92854d4a5553465ff42e68927f3a161bb8977e98.tar.xz | |
Add support for multi-eci encoding for PDF417 (#1506)
* - Added multi-eci encoding for PDF417
- Fixed issue that the PDF417 decoder incorrectly decoded input with a leading ECI and no explicit latch to TEXT encoding
Diffstat (limited to 'core/src/test/java/com/google')
| -rw-r--r-- | core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java | 131 | ||||
| -rw-r--r-- | core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java | 12 |
2 files changed, 128 insertions, 15 deletions
diff --git a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java index 5f6ac8c08..d9c4b6bc0 100644 --- a/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/decoder/PDF417DecoderTestCase.java @@ -25,6 +25,7 @@ import com.google.zxing.pdf417.encoder.PDF417HighLevelEncoderTestAdapter; import org.junit.Assert; import org.junit.Test; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Random; @@ -283,21 +284,76 @@ public class PDF417DecoderTestCase extends Assert { assertEquals(4190044, total); } + @Test + public void testECIEnglishHiragana() throws Exception { + //multi ECI UTF-8, UTF-16 and ISO-8859-1 + performECITest(new char[] {'a', '1', '\u3040'}, new float[] {20f, 1f, 10f}, 102583, 110914); + } + + @Test + public void testECIEnglishKatakana() throws Exception { + //multi ECI UTF-8, UTF-16 and ISO-8859-1 + performECITest(new char[] {'a', '1', '\u30a0'}, new float[] {20f, 1f, 10f}, 104691, 110914); + } + + @Test + public void testECIEnglishHalfWidthKatakana() throws Exception { + //single ECI + performECITest(new char[] {'a', '1', '\uff80'}, new float[] {20f, 1f, 10f}, 80463, 110914); + } + + @Test + public void testECIEnglishChinese() throws Exception { + //single ECI + performECITest(new char[] {'a', '1', '\u4e00'}, new float[] {20f, 1f, 10f}, 95643, 110914); + } + + @Test + public void testECIGermanCyrillic() throws Exception { + //single ECI since the German Umlaut is in ISO-8859-1 + performECITest(new char[] {'a', '1', '\u00c4', '\u042f'}, new float[] {20f, 1f, 1f, 10f}, 80529, 96007); + } + + @Test + public void testECIEnglishCzechCyrillic1() throws Exception { + //multi ECI between ISO-8859-2 and ISO-8859-5 + performECITest(new char[] {'a', '1', '\u010c', '\u042f'}, new float[] {10f, 1f, 10f, 10f}, 91482, 124525); + } + + @Test + public void testECIEnglishCzechCyrillic2() throws Exception { + //multi ECI between ISO-8859-2 and ISO-8859-5 + performECITest(new char[] {'a', '1', '\u010c', '\u042f'}, new float[] {40f, 1f, 10f, 10f}, 79331, 88236); + } + + @Test + public void testECIEnglishArabicCyrillic() throws Exception { + //multi ECI between UTF-8 (ISO-8859-6 is excluded in CharacterSetECI) and ISO-8859-5 + performECITest(new char[] {'a', '1', '\u0620', '\u042f'}, new float[] {10f, 1f, 10f, 10f}, 111508, 124525); + } + private static void encodeDecode(String input, int expectedLength) throws WriterException, FormatException { assertEquals(expectedLength, encodeDecode(input)); } private static int encodeDecode(String input) throws WriterException, FormatException { - String s = PDF417HighLevelEncoderTestAdapter.encodeHighLevel(input, Compaction.AUTO, null); - int[] codewords = new int[s.length() + 1]; - codewords[0] = codewords.length; - for (int i = 1; i < codewords.length; i++) { - codewords[i] = s.charAt(i - 1); - } - DecoderResult result = DecodedBitStreamParser.decode(codewords, "0"); + return encodeDecode(input, null, false, true); + } - assertEquals(input, result.getText()); - return codewords.length; + private static int encodeDecode(String input, Charset charset, boolean autoECI, boolean decode) + throws WriterException, FormatException { + String s = PDF417HighLevelEncoderTestAdapter.encodeHighLevel(input, Compaction.AUTO, charset, autoECI); + if (decode) { + int[] codewords = new int[s.length() + 1]; + codewords[0] = codewords.length; + for (int i = 1; i < codewords.length; i++) { + codewords[i] = s.charAt(i - 1); + } + DecoderResult result = DecodedBitStreamParser.decode(codewords, "0"); + + assertEquals(input, result.getText()); + } + return s.length() + 1; } private static int getEndIndex(int length, char[] chars) { @@ -338,4 +394,61 @@ public class PDF417DecoderTestCase extends Assert { } } + private static void performECITest(char[] chars, + float[] weights, + int expectedMinLength, + int expectedUTFLength) throws WriterException, FormatException { + Random random = new Random(0); + int minLength = 0; + int utfLength = 0; + for (int i = 0; i < 1000; i++) { + String s = generateText(random, 100, chars, weights); + minLength += encodeDecode(s, null, true, false); + // TODO: Use this instead when the decoder supports multi ECI input + //minLength += encodeDecode(s, null, true, true); + utfLength += encodeDecode(s, StandardCharsets.UTF_8, false, true); + } + assertEquals(expectedMinLength, minLength); + assertEquals(expectedUTFLength, utfLength); + } + + private static String generateText(Random random, int maxWidth, char[] chars, float[] weights) { + StringBuilder result = new StringBuilder(); + final int maxWordWidth = 7; + float total = 0; + for (int i = 0; i < weights.length; i++) { + total += weights[i]; + } + for (int i = 0; i < weights.length; i++) { + weights[i] /= total; + } + int cnt = 0; + do { + float maxValue = 0; + int maxIndex = 0; + for (int j = 0; j < weights.length; j++) { + float value = random.nextFloat() * weights[j]; + if (value > maxValue) { + maxValue = value; + maxIndex = j; + } + } + final float wordLength = maxWordWidth * random.nextFloat(); + if (wordLength > 0 && result.length() > 0) { + result.append(' '); + } + for (int j = 0; j < wordLength; j++) { + char c = chars[maxIndex]; + if (j == 0 && c >= 'a' && c <= 'z' && random.nextBoolean()) { + c = (char) (c - 'a' + 'A'); + } + result.append(c); + } + if (cnt % 2 != 0 && random.nextBoolean()) { + result.append('.'); + } + cnt++; + } while (result.length() < maxWidth - maxWordWidth); + return result.toString(); + } } diff --git a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java index 4d4b35b2a..8166333ed 100644 --- a/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java +++ b/core/src/test/java/com/google/zxing/pdf417/encoder/PDF417EncoderTestCase.java @@ -29,7 +29,7 @@ public final class PDF417EncoderTestCase extends Assert { @Test public void testEncodeAuto() throws Exception { String encoded = PDF417HighLevelEncoder.encodeHighLevel( - "ABCD", Compaction.AUTO, StandardCharsets.UTF_8); + "ABCD", Compaction.AUTO, StandardCharsets.UTF_8, false); assertEquals("\u039f\u001A\u0385ABCD", encoded); } @@ -37,33 +37,33 @@ public final class PDF417EncoderTestCase extends Assert { public void testEncodeAutoWithSpecialChars() throws Exception { // Just check if this does not throw an exception PDF417HighLevelEncoder.encodeHighLevel( - "1%§s ?aG$", Compaction.AUTO, StandardCharsets.UTF_8); + "1%§s ?aG$", Compaction.AUTO, StandardCharsets.UTF_8, false); } @Test public void testEncodeIso88591WithSpecialChars() throws Exception { // Just check if this does not throw an exception - PDF417HighLevelEncoder.encodeHighLevel("asdfg§asd", Compaction.AUTO, StandardCharsets.ISO_8859_1); + PDF417HighLevelEncoder.encodeHighLevel("asdfg§asd", Compaction.AUTO, StandardCharsets.ISO_8859_1, false); } @Test public void testEncodeText() throws Exception { String encoded = PDF417HighLevelEncoder.encodeHighLevel( - "ABCD", Compaction.TEXT, StandardCharsets.UTF_8); + "ABCD", Compaction.TEXT, StandardCharsets.UTF_8, false); assertEquals("Ο\u001A\u0001?", encoded); } @Test public void testEncodeNumeric() throws Exception { String encoded = PDF417HighLevelEncoder.encodeHighLevel( - "1234", Compaction.NUMERIC, StandardCharsets.UTF_8); + "1234", Compaction.NUMERIC, StandardCharsets.UTF_8, false); assertEquals("\u039f\u001A\u0386\f\u01b2", encoded); } @Test public void testEncodeByte() throws Exception { String encoded = PDF417HighLevelEncoder.encodeHighLevel( - "abcd", Compaction.BYTE, StandardCharsets.UTF_8); + "abcd", Compaction.BYTE, StandardCharsets.UTF_8, false); assertEquals("\u039f\u001A\u0385abcd", encoded); } |
