Search in sources :

Example 46 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxing by zxing.

the class PDF417BlackBox4TestCase method testPDF417BlackBoxCountingResults.

private void testPDF417BlackBoxCountingResults(boolean assertOnFailure) throws IOException {
    assertFalse(testResults.isEmpty());
    Map<String, List<Path>> imageFiles = getImageFileLists();
    int testCount = testResults.size();
    int[] passedCounts = new int[testCount];
    int[] tryHarderCounts = new int[testCount];
    Path testBase = getTestBase();
    for (Entry<String, List<Path>> testImageGroup : imageFiles.entrySet()) {
        log.fine(String.format("Starting Image Group %s", testImageGroup.getKey()));
        String fileBaseName = testImageGroup.getKey();
        String expectedText;
        Path expectedTextFile = testBase.resolve(fileBaseName + ".txt");
        if (Files.exists(expectedTextFile)) {
            expectedText = readFileAsString(expectedTextFile, StandardCharsets.UTF_8);
        } else {
            expectedTextFile = testBase.resolve(fileBaseName + ".bin");
            assertTrue(Files.exists(expectedTextFile));
            expectedText = readFileAsString(expectedTextFile, StandardCharsets.ISO_8859_1);
        }
        for (int x = 0; x < testCount; x++) {
            List<Result> results = new ArrayList<>();
            for (Path imageFile : testImageGroup.getValue()) {
                BufferedImage image = ImageIO.read(imageFile.toFile());
                float rotation = testResults.get(x).getRotation();
                BufferedImage rotatedImage = rotateImage(image, rotation);
                LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                try {
                    results.addAll(Arrays.asList(decode(bitmap, false)));
                } catch (ReaderException ignored) {
                // ignore
                }
            }
            Collections.sort(results, new Comparator<Result>() {

                @Override
                public int compare(Result arg0, Result arg1) {
                    PDF417ResultMetadata resultMetadata = getMeta(arg0);
                    PDF417ResultMetadata otherResultMetadata = getMeta(arg1);
                    return resultMetadata.getSegmentIndex() - otherResultMetadata.getSegmentIndex();
                }
            });
            StringBuilder resultText = new StringBuilder();
            String fileId = null;
            for (Result result : results) {
                PDF417ResultMetadata resultMetadata = getMeta(result);
                assertNotNull("resultMetadata", resultMetadata);
                if (fileId == null) {
                    fileId = resultMetadata.getFileId();
                }
                assertEquals("FileId", fileId, resultMetadata.getFileId());
                resultText.append(result.getText());
            }
            assertEquals("ExpectedText", expectedText, resultText.toString());
            passedCounts[x]++;
            tryHarderCounts[x]++;
        }
    }
    // Print the results of all tests first
    int totalFound = 0;
    int totalMustPass = 0;
    int numberOfTests = imageFiles.keySet().size();
    for (int x = 0; x < testResults.size(); x++) {
        TestResult testResult = testResults.get(x);
        log.info(String.format("Rotation %d degrees:", (int) testResult.getRotation()));
        log.info(String.format(" %d of %d images passed (%d required)", passedCounts[x], numberOfTests, testResult.getMustPassCount()));
        log.info(String.format(" %d of %d images passed with try harder (%d required)", tryHarderCounts[x], numberOfTests, testResult.getTryHarderCount()));
        totalFound += passedCounts[x] + tryHarderCounts[x];
        totalMustPass += testResult.getMustPassCount() + testResult.getTryHarderCount();
    }
    int totalTests = numberOfTests * testCount * 2;
    log.info(String.format("Decoded %d images out of %d (%d%%, %d required)", totalFound, totalTests, totalFound * 100 / totalTests, totalMustPass));
    if (totalFound > totalMustPass) {
        log.warning(String.format("+++ Test too lax by %d images", totalFound - totalMustPass));
    } else if (totalFound < totalMustPass) {
        log.warning(String.format("--- Test failed by %d images", totalMustPass - totalFound));
    }
    // Then run through again and assert if any failed
    if (assertOnFailure) {
        for (int x = 0; x < testCount; x++) {
            TestResult testResult = testResults.get(x);
            String label = "Rotation " + testResult.getRotation() + " degrees: Too many images failed";
            assertTrue(label, passedCounts[x] >= testResult.getMustPassCount());
            assertTrue("Try harder, " + label, tryHarderCounts[x] >= testResult.getTryHarderCount());
        }
    }
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) TestResult(com.google.zxing.common.TestResult) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) TestResult(com.google.zxing.common.TestResult) ReaderException(com.google.zxing.ReaderException) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ArrayList(java.util.ArrayList) List(java.util.List) BinaryBitmap(com.google.zxing.BinaryBitmap)

Example 47 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxing by zxing.

the class MultiQRCodeTestCase method testMultiQRCodes.

@Test
public void testMultiQRCodes() throws Exception {
    // Very basic test for now
    Path testBase = AbstractBlackBoxTestCase.buildTestBase("src/test/resources/blackbox/multi-qrcode-1");
    Path testImage = testBase.resolve("1.png");
    BufferedImage image = ImageIO.read(testImage.toFile());
    LuminanceSource source = new BufferedImageLuminanceSource(image);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
    MultipleBarcodeReader reader = new QRCodeMultiReader();
    Result[] results = reader.decodeMultiple(bitmap);
    assertNotNull(results);
    assertEquals(4, results.length);
    Set<String> barcodeContents = new HashSet<>();
    for (Result result : results) {
        barcodeContents.add(result.getText());
        assertEquals(BarcodeFormat.QR_CODE, result.getBarcodeFormat());
        Map<ResultMetadataType, Object> metadata = result.getResultMetadata();
        assertNotNull(metadata);
    }
    Set<String> expectedContents = new HashSet<>();
    expectedContents.add("You earned the class a 5 MINUTE DANCE PARTY!!  Awesome!  Way to go!  Let's boogie!");
    expectedContents.add("You earned the class 5 EXTRA MINUTES OF RECESS!!  Fabulous!!  Way to go!!");
    expectedContents.add("You get to SIT AT MRS. SIGMON'S DESK FOR A DAY!!  Awesome!!  Way to go!! Guess I better clean up! :)");
    expectedContents.add("You get to CREATE OUR JOURNAL PROMPT FOR THE DAY!  Yay!  Way to go!  ");
    assertEquals(expectedContents, barcodeContents);
}
Also used : Path(java.nio.file.Path) MultipleBarcodeReader(com.google.zxing.multi.MultipleBarcodeReader) HybridBinarizer(com.google.zxing.common.HybridBinarizer) BufferedImage(java.awt.image.BufferedImage) Result(com.google.zxing.Result) LuminanceSource(com.google.zxing.LuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ResultMetadataType(com.google.zxing.ResultMetadataType) BinaryBitmap(com.google.zxing.BinaryBitmap) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxing by zxing.

the class RSSExpandedInternalTestCase method testDecodeCheckCharacter.

@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);
    //image pixels where the A1 pattern starts (at 124) and ends (at 214)
    int[] startEnd = { 145, 243 };
    // A
    int value = 0;
    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());
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) FinderPattern(com.google.zxing.oned.rss.FinderPattern) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) DataCharacter(com.google.zxing.oned.rss.DataCharacter) Test(org.junit.Test)

Example 49 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project zxing by zxing.

the class RSSExpandedInternalTestCase method testRetrieveNextPairPatterns.

@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());
}
Also used : GlobalHistogramBinarizer(com.google.zxing.common.GlobalHistogramBinarizer) FinderPattern(com.google.zxing.oned.rss.FinderPattern) BufferedImageLuminanceSource(com.google.zxing.BufferedImageLuminanceSource) ArrayList(java.util.ArrayList) BitArray(com.google.zxing.common.BitArray) BinaryBitmap(com.google.zxing.BinaryBitmap) BufferedImage(java.awt.image.BufferedImage) Test(org.junit.Test)

Example 50 with BinaryBitmap

use of com.google.zxing.BinaryBitmap in project QRCode by 5peak2me.

the class MipcaActivityCapture method scanningImage.

/**
	 * 扫描二维码图片的方法
	 * @param path
	 * @return
	 */
public Result scanningImage(String path) {
    if (TextUtils.isEmpty(path)) {
        return null;
    }
    Hashtable<DecodeHintType, String> hints = new Hashtable<DecodeHintType, String>();
    //设置二维码内容的编码
    hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
    BitmapFactory.Options options = new BitmapFactory.Options();
    // 先获取原大小
    options.inJustDecodeBounds = true;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    // 获取新的大小
    options.inJustDecodeBounds = false;
    int sampleSize = (int) (options.outHeight / (float) 200);
    if (sampleSize <= 0)
        sampleSize = 1;
    options.inSampleSize = sampleSize;
    scanBitmap = BitmapFactory.decodeFile(path, options);
    RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap);
    BinaryBitmap bitmap1 = new BinaryBitmap(new HybridBinarizer(source));
    QRCodeReader reader = new QRCodeReader();
    try {
        return reader.decode(bitmap1, hints);
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (ChecksumException e) {
        e.printStackTrace();
    } catch (FormatException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : QRCodeReader(com.google.zxing.qrcode.QRCodeReader) DecodeHintType(com.google.zxing.DecodeHintType) Hashtable(java.util.Hashtable) ChecksumException(com.google.zxing.ChecksumException) NotFoundException(com.google.zxing.NotFoundException) HybridBinarizer(com.google.zxing.common.HybridBinarizer) FormatException(com.google.zxing.FormatException) BitmapFactory(android.graphics.BitmapFactory) BinaryBitmap(com.google.zxing.BinaryBitmap) RGBLuminanceSource(com.google.zxing.zxing.image.RGBLuminanceSource)

Aggregations

BinaryBitmap (com.google.zxing.BinaryBitmap)81 Result (com.google.zxing.Result)64 HybridBinarizer (com.google.zxing.common.HybridBinarizer)63 ReaderException (com.google.zxing.ReaderException)40 Message (android.os.Message)25 Bundle (android.os.Bundle)24 MultiFormatReader (com.google.zxing.MultiFormatReader)21 NotFoundException (com.google.zxing.NotFoundException)21 BufferedImage (java.awt.image.BufferedImage)21 LuminanceSource (com.google.zxing.LuminanceSource)19 PlanarYUVLuminanceSource (com.google.zxing.PlanarYUVLuminanceSource)19 DecodeHintType (com.google.zxing.DecodeHintType)16 Handler (android.os.Handler)14 BufferedImageLuminanceSource (com.google.zxing.BufferedImageLuminanceSource)13 Hashtable (java.util.Hashtable)12 BufferedImageLuminanceSource (com.google.zxing.client.j2se.BufferedImageLuminanceSource)11 GlobalHistogramBinarizer (com.google.zxing.common.GlobalHistogramBinarizer)10 Test (org.junit.Test)10 Bitmap (android.graphics.Bitmap)8 BitArray (com.google.zxing.common.BitArray)8