use of com.google.zxing.LuminanceSource in project zxing by zxing.
the class AbstractBlackBoxTestCase method testBlackBoxCountingResults.
private void testBlackBoxCountingResults(boolean assertOnFailure) throws IOException {
assertFalse(testResults.isEmpty());
List<Path> imageFiles = getImageFiles();
int testCount = testResults.size();
int[] passedCounts = new int[testCount];
int[] misreadCounts = new int[testCount];
int[] tryHarderCounts = new int[testCount];
int[] tryHarderMisreadCounts = new int[testCount];
for (Path testImage : imageFiles) {
log.info(String.format("Starting %s", testImage));
BufferedImage image = ImageIO.read(testImage.toFile());
String testImageFileName = testImage.getFileName().toString();
String fileBaseName = testImageFileName.substring(0, testImageFileName.indexOf('.'));
Path expectedTextFile = testBase.resolve(fileBaseName + ".txt");
String expectedText;
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);
}
Path expectedMetadataFile = testBase.resolve(fileBaseName + ".metadata.txt");
Properties expectedMetadata = new Properties();
if (Files.exists(expectedMetadataFile)) {
try (BufferedReader reader = Files.newBufferedReader(expectedMetadataFile, StandardCharsets.UTF_8)) {
expectedMetadata.load(reader);
}
}
for (int x = 0; x < testCount; x++) {
float rotation = testResults.get(x).getRotation();
BufferedImage rotatedImage = rotateImage(image, rotation);
LuminanceSource source = new BufferedImageLuminanceSource(rotatedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
if (decode(bitmap, rotation, expectedText, expectedMetadata, false)) {
passedCounts[x]++;
} else {
misreadCounts[x]++;
}
} catch (ReaderException ignored) {
log.fine(String.format("could not read at rotation %f", rotation));
}
try {
if (decode(bitmap, rotation, expectedText, expectedMetadata, true)) {
tryHarderCounts[x]++;
} else {
tryHarderMisreadCounts[x]++;
}
} catch (ReaderException ignored) {
log.fine(String.format("could not read at rotation %f w/TH", rotation));
}
}
}
// Print the results of all tests first
int totalFound = 0;
int totalMustPass = 0;
int totalMisread = 0;
int totalMaxMisread = 0;
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], imageFiles.size(), testResult.getMustPassCount()));
int failed = imageFiles.size() - passedCounts[x];
log.info(String.format(" %d failed due to misreads, %d not detected", misreadCounts[x], failed - misreadCounts[x]));
log.info(String.format(" %d of %d images passed with try harder (%d required)", tryHarderCounts[x], imageFiles.size(), testResult.getTryHarderCount()));
failed = imageFiles.size() - tryHarderCounts[x];
log.info(String.format(" %d failed due to misreads, %d not detected", tryHarderMisreadCounts[x], failed - tryHarderMisreadCounts[x]));
totalFound += passedCounts[x] + tryHarderCounts[x];
totalMustPass += testResult.getMustPassCount() + testResult.getTryHarderCount();
totalMisread += misreadCounts[x] + tryHarderMisreadCounts[x];
totalMaxMisread += testResult.getMaxMisreads() + testResult.getMaxTryHarderMisreads();
}
int totalTests = imageFiles.size() * 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));
}
if (totalMisread < totalMaxMisread) {
log.warning(String.format("+++ Test expects too many misreads by %d images", totalMaxMisread - totalMisread));
} else if (totalMisread > totalMaxMisread) {
log.warning(String.format("--- Test had too many misreads by %d images", totalMisread - totalMaxMisread));
}
// 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());
label = "Rotation " + testResult.getRotation() + " degrees: Too many images misread";
assertTrue(label, misreadCounts[x] <= testResult.getMaxMisreads());
assertTrue("Try harder, " + label, tryHarderMisreadCounts[x] <= testResult.getMaxTryHarderMisreads());
}
}
}
use of com.google.zxing.LuminanceSource 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());
}
}
}
use of com.google.zxing.LuminanceSource 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);
}
use of com.google.zxing.LuminanceSource in project zxing by zxing.
the class GlobalHistogramBinarizer method getBlackRow.
// Applies simple sharpening to the row data to improve performance of the 1D Readers.
@Override
public BitArray getBlackRow(int y, BitArray row) throws NotFoundException {
LuminanceSource source = getLuminanceSource();
int width = source.getWidth();
if (row == null || row.getSize() < width) {
row = new BitArray(width);
} else {
row.clear();
}
initArrays(width);
byte[] localLuminances = source.getRow(y, luminances);
int[] localBuckets = buckets;
for (int x = 0; x < width; x++) {
localBuckets[(localLuminances[x] & 0xff) >> LUMINANCE_SHIFT]++;
}
int blackPoint = estimateBlackPoint(localBuckets);
if (width < 3) {
// Special case for very small images
for (int x = 0; x < width; x++) {
if ((localLuminances[x] & 0xff) < blackPoint) {
row.set(x);
}
}
} else {
int left = localLuminances[0] & 0xff;
int center = localLuminances[1] & 0xff;
for (int x = 1; x < width - 1; x++) {
int right = localLuminances[x + 1] & 0xff;
// A simple -1 4 -1 box filter with a weight of 2.
if (((center * 4) - left - right) / 2 < blackPoint) {
row.set(x);
}
left = center;
center = right;
}
}
return row;
}
use of com.google.zxing.LuminanceSource in project weex-example by KalicyZhou.
the class HybridBinarizer method getBlackMatrix.
/**
* Calculates the final BitMatrix once for all requests. This could be called once from the
* constructor instead, but there are some advantages to doing it lazily, such as making
* profiling easier, and not doing heavy lifting when callers don't expect it.
*/
@Override
public BitMatrix getBlackMatrix() throws NotFoundException {
if (matrix != null) {
return matrix;
}
LuminanceSource source = getLuminanceSource();
int width = source.getWidth();
int height = source.getHeight();
if (width >= MINIMUM_DIMENSION && height >= MINIMUM_DIMENSION) {
byte[] luminances = source.getMatrix();
int subWidth = width >> BLOCK_SIZE_POWER;
if ((width & BLOCK_SIZE_MASK) != 0) {
subWidth++;
}
int subHeight = height >> BLOCK_SIZE_POWER;
if ((height & BLOCK_SIZE_MASK) != 0) {
subHeight++;
}
int[][] blackPoints = calculateBlackPoints(luminances, subWidth, subHeight, width, height);
BitMatrix newMatrix = new BitMatrix(width, height);
calculateThresholdForBlock(luminances, subWidth, subHeight, width, height, blackPoints, newMatrix);
matrix = newMatrix;
} else {
// If the image is too small, fall back to the global histogram approach.
matrix = super.getBlackMatrix();
}
return matrix;
}
Aggregations