private static void assertCorrectImage2binary(String fileName, String expected) throws IOException, NotFoundException { Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName); BufferedImage image = ImageIO.read(path.toFile()); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> pairs; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); pairs = rssExpandedReader.decodeRow2pairs(rowNumber, row); } catch (ReaderException re) { fail(re.toString()); return; } BitArray binary = BitArrayBuilder.buildBitArray(pairs); assertEquals(expected, binary.toString()); }
@Test public void testRetrieveNextPairPatterns() throws Exception { BufferedImage image = readImage("3.png"); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<>(); RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); }
@Test public void testDecodeDataCharacter() throws Exception { BufferedImage image = readImage("3.png"); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0; // A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false); assertEquals(19, dataCharacter.getValue()); assertEquals(1007, dataCharacter.getChecksumPortion()); }
private static void assertCorrectImage2string(String fileName, String expected) throws IOException, NotFoundException { Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName); BufferedImage image = ImageIO.read(path.toFile()); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result result; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); result = rssExpandedReader.decodeRow(rowNumber, row, null); } catch (ReaderException re) { fail(re.toString()); return; } assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat()); assertEquals(expected, result.getText()); }
private static void assertCorrectImage2result(String fileName, ExpandedProductParsedResult expected) throws IOException, NotFoundException { Path path = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/rssexpanded-1/").resolve(fileName); BufferedImage image = ImageIO.read(path.toFile()); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result theResult; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); theResult = rssExpandedReader.decodeRow(rowNumber, row, null); } catch (ReaderException re) { fail(re.toString()); return; } assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat()); ParsedResult result = ResultParser.parseResult(theResult); assertEquals(expected, result); }
private static void assertCorrectImage2binary(String path, String expected) throws IOException, NotFoundException { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> pairs = rssExpandedReader.decodeRow2pairs(rowNumber, row); BitArray binary = BitArrayBuilder.buildBitArray(pairs); assertEquals(expected, binary.toString()); }
@Test public void testDecodeCheckCharacter() throws Exception { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); String path = "test/data/blackbox/rssexpanded-1/3.jpg"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0;// A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, true); assertEquals(98, dataCharacter.getValue()); }
@Test public void testDecodeDataCharacter() throws Exception { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); String path = "test/data/blackbox/rssexpanded-1/3.jpg"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0; // A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false); assertEquals(19, dataCharacter.getValue()); assertEquals(1007, dataCharacter.getChecksumPortion()); }
private static void assertCorrectImage2string(String path, String expected) throws IOException, NotFoundException { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result result = rssExpandedReader.decodeRow(rowNumber, row, null); assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat()); assertEquals(expected, result.getText()); }
private static void assertCorrectImage2result(String path, ExpandedProductParsedResult expected) throws IOException, NotFoundException { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result theResult = rssExpandedReader.decodeRow(rowNumber, row, null); assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat()); ParsedResult result = ResultParser.parseResult(theResult); assertEquals(expected, result); }
private static void assertCorrectImage2binary(String path, String expected) throws IOException, NotFoundException { File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> pairs; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); pairs = rssExpandedReader.decodeRow2pairs(rowNumber, row); } catch (ReaderException re) { fail(re.toString()); return; } BitArray binary = BitArrayBuilder.buildBitArray(pairs); assertEquals(expected, binary.toString()); }
@Test public void testDecodeCheckCharacter() throws Exception { String path = "test/data/blackbox/rssexpanded-1/3.png"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0;// A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, true); assertEquals(98, dataCharacter.getValue()); }
@Test public void testDecodeDataCharacter() throws Exception { String path = "test/data/blackbox/rssexpanded-1/3.png"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0; // A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, false); assertEquals(19, dataCharacter.getValue()); assertEquals(1007, dataCharacter.getChecksumPortion()); }
private static void assertCorrectImage2string(String path, String expected) throws IOException, NotFoundException { File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result result; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); result = rssExpandedReader.decodeRow(rowNumber, row, null); } catch (ReaderException re) { fail(re.toString()); return; } assertSame(BarcodeFormat.RSS_EXPANDED, result.getBarcodeFormat()); assertEquals(expected, result.getText()); }
/** * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency, reuse the same reader * objects from one decode to the next. */ public static Result decodeImage(byte[] data, int width, int height) { // 处理 Result result = null; try { Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>(); hints.put(DecodeHintType.CHARACTER_SET, "utf-8"); hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE); hints.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, 0, 0, width, height, false); /** * HybridBinarizer算法使用了更高级的算法,但使用GlobalHistogramBinarizer识别效率确实比HybridBinarizer要高一些。 * * GlobalHistogram算法:(http://kuangjianwei.blog.163.com/blog/static/190088953201361015055110/) * * 二值化的关键就是定义出黑白的界限,我们的图像已经转化为了灰度图像,每个点都是由一个灰度值来表示,就需要定义出一个灰度值,大于这个值就为白(0),低于这个值就为黑(1)。 * 在GlobalHistogramBinarizer中,是从图像中均匀取5行(覆盖整个图像高度),每行取中间五分之四作为样本;以灰度值为X轴,每个灰度值的像素个数为Y轴建立一个直方图, * 从直方图中取点数最多的一个灰度值,然后再去给其他的灰度值进行分数计算,按照点数乘以与最多点数灰度值的距离的平方来进行打分,选分数最高的一个灰度值。接下来在这两个灰度值中间选取一个区分界限, * 取的原则是尽量靠近中间并且要点数越少越好。界限有了以后就容易了,与整幅图像的每个点进行比较,如果灰度值比界限小的就是黑,在新的矩阵中将该点置1,其余的就是白,为0。 */ BinaryBitmap bitmap1 = new BinaryBitmap(new GlobalHistogramBinarizer(source)); // BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader2 = new QRCodeReader(); result = reader2.decode(bitmap1, hints); } catch (ReaderException e) { } return result; }
@Test public void testFindFinderPatterns() throws Exception { BufferedImage image = readImage("2.png"); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<>(); RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair3); finderPattern = pair3.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); try{ rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); // the previous was the last pair fail(NotFoundException.class.getName() + " expected"); } catch (NotFoundException nfe) { // ok } }
@Test public void testDecodeCheckCharacter() throws Exception { BufferedImage image = readImage("3.png"); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); BitArray row = binaryMap.getBlackRow(binaryMap.getHeight() / 2, null); int[] startEnd = {145, 243};//image pixels where the A1 pattern starts (at 124) and ends (at 214) int value = 0;// A FinderPattern finderPatternA1 = new FinderPattern(value, startEnd, startEnd[0], startEnd[1], image.getHeight() / 2); //{1, 8, 4, 1, 1}; RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); DataCharacter dataCharacter = rssExpandedReader.decodeDataCharacter(row, finderPatternA1, true, true); assertEquals(98, dataCharacter.getValue()); }
@Test public void testRetrieveNextPairPatterns() throws Exception { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); String path = "test/data/blackbox/rssexpanded-1/3.jpg"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<ExpandedPair>(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); assertFalse(pair1.mayBeLast()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); assertTrue(pair2.mayBeLast()); }
@Test public void testRetrieveNextPairPatterns() throws Exception { String path = "test/data/blackbox/rssexpanded-1/3.png"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<ExpandedPair>(); RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); }
private static void assertCorrectImage2result(String path, ExpandedProductParsedResult expected) throws IOException, NotFoundException { File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); Result theResult; try { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); theResult = rssExpandedReader.decodeRow(rowNumber, row, null); } catch (ReaderException re) { fail(re.toString()); return; } assertSame(BarcodeFormat.RSS_EXPANDED, theResult.getBarcodeFormat()); ParsedResult result = ResultParser.parseResult(theResult); assertEquals(expected, result); }
static BinaryBitmap getBinaryBitmap(String path) throws IOException { BufferedImage bufferedImage = getBufferedImage(path); BufferedImageLuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage); return new BinaryBitmap(new GlobalHistogramBinarizer(luminanceSource)); }
@Test public void testFindFinderPatterns() throws Exception { RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); String path = "test/data/blackbox/rssexpanded-1/2.jpg"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<ExpandedPair>(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); assertFalse(pair1.mayBeLast()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); assertFalse(pair2.mayBeLast()); ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair3); finderPattern = pair3.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); assertTrue(pair3.mayBeLast()); try{ rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); // the previous was the last pair fail(NotFoundException.class.getName() + " expected"); }catch(NotFoundException nfe){ // ok } }
@Test public void testFindFinderPatterns() throws Exception { String path = "test/data/blackbox/rssexpanded-1/2.png"; File file = new File(path); if (!file.exists()) { // Support running from project root too file = new File("core", path); } BufferedImage image = ImageIO.read(file); BinaryBitmap binaryMap = new BinaryBitmap(new GlobalHistogramBinarizer(new BufferedImageLuminanceSource(image))); int rowNumber = binaryMap.getHeight() / 2; BitArray row = binaryMap.getBlackRow(rowNumber, null); List<ExpandedPair> previousPairs = new ArrayList<ExpandedPair>(); RSSExpandedReader rssExpandedReader = new RSSExpandedReader(); ExpandedPair pair1 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair1); FinderPattern finderPattern = pair1.getFinderPattern(); assertNotNull(finderPattern); assertEquals(0, finderPattern.getValue()); ExpandedPair pair2 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair2); finderPattern = pair2.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); ExpandedPair pair3 = rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); previousPairs.add(pair3); finderPattern = pair3.getFinderPattern(); assertNotNull(finderPattern); assertEquals(1, finderPattern.getValue()); try{ rssExpandedReader.retrieveNextPair(row, previousPairs, rowNumber); // the previous was the last pair fail(NotFoundException.class.getName() + " expected"); }catch(NotFoundException nfe){ // ok } }