/** * Generates the codewords expectation file for the specified symbol. * * @param symbol the symbol to generate codewords for * @throws IOException if there is any I/O error */ private void generateCodewordsExpectationFile(Symbol symbol) throws IOException { if (!codewordsFile.exists()) { PrintWriter writer = new PrintWriter(codewordsFile); try { int[] codewords = symbol.getCodewords(); for (int codeword : codewords) { writer.println(codeword); } } catch (UnsupportedOperationException e) { for (String pattern : symbol.pattern) { writer.println(pattern); } } writer.close(); } }
/** * Verifies that the specified symbol was encoded and rendered in a way that matches expectations. * * @param symbol the symbol to check * @throws IOException if there is any I/O error * @throws ReaderException if ZXing has an issue decoding the barcode image */ private void verifySuccess(Symbol symbol) throws IOException, ReaderException { assertEquals("error message", "", symbol.error_msg); List<String> expectedList = Files.readAllLines(codewordsFile.toPath(), UTF_8); try { // try to verify codewords int[] actualCodewords = symbol.getCodewords(); assertEquals(expectedList.size(), actualCodewords.length); for (int i = 0; i < actualCodewords.length; i++) { int expected = getInt(expectedList.get(i)); int actual = actualCodewords[i]; assertEquals("at codeword index " + i, expected, actual); } } catch (UnsupportedOperationException e) { // codewords aren't supported, try to verify patterns String[] actualPatterns = symbol.pattern; assertEquals(expectedList.size(), actualPatterns.length); for (int i = 0; i < actualPatterns.length; i++) { String expected = expectedList.get(i); String actual = actualPatterns[i]; assertEquals("at pattern index " + i, expected, actual); } } // make sure the barcode images match BufferedImage expected = ImageIO.read(pngFile); BufferedImage actual = draw(symbol); assertEqual(expected, actual); // if possible, ensure an independent third party (ZXing) can read the generated barcode and // agrees on what it represents Reader zxingReader = findReader(symbol); if (zxingReader != null) { LuminanceSource source = new BufferedImageLuminanceSource(expected); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); Map<DecodeHintType, Boolean> hints = Collections.singletonMap(DecodeHintType.PURE_BARCODE, Boolean.TRUE); Result result = zxingReader.decode(bitmap, hints); String zxingData = removeChecksum(result.getText(), symbol); String okapiData = removeStartStopChars(symbol.getContent(), symbol); assertEquals(okapiData, zxingData); } }