use of com.google.zxing.BinaryBitmap in project zxing by zxing.
the class DecodeWorker method decode.
private Result[] decode(URI uri, Map<DecodeHintType, ?> hints) throws IOException {
BufferedImage image = ImageReader.readImage(uri);
LuminanceSource source;
if (config.crop == null) {
source = new BufferedImageLuminanceSource(image);
} else {
List<Integer> crop = config.crop;
source = new BufferedImageLuminanceSource(image, crop.get(0), crop.get(1), crop.get(2), crop.get(3));
}
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
if (config.dumpBlackPoint) {
dumpBlackPoint(uri, image, bitmap);
}
MultiFormatReader multiFormatReader = new MultiFormatReader();
Result[] results;
try {
if (config.multi) {
MultipleBarcodeReader reader = new GenericMultipleBarcodeReader(multiFormatReader);
results = reader.decodeMultiple(bitmap, hints);
} else {
results = new Result[] { multiFormatReader.decode(bitmap, hints) };
}
} catch (NotFoundException ignored) {
System.out.println(uri + ": No barcode found");
return null;
}
if (config.brief) {
System.out.println(uri + ": Success");
} else {
StringWriter output = new StringWriter();
for (Result result : results) {
ParsedResult parsedResult = ResultParser.parseResult(result);
output.write(uri + " (format: " + result.getBarcodeFormat() + ", type: " + parsedResult.getType() + "):\n" + "Raw result:\n" + result.getText() + "\n" + "Parsed result:\n" + parsedResult.getDisplayResult() + "\n");
output.write("Found " + result.getResultPoints().length + " result points.\n");
for (int pointIndex = 0; pointIndex < result.getResultPoints().length; pointIndex++) {
ResultPoint rp = result.getResultPoints()[pointIndex];
output.write(" Point " + pointIndex + ": (" + rp.getX() + ',' + rp.getY() + ')');
if (pointIndex != result.getResultPoints().length - 1) {
output.write('\n');
}
}
output.write('\n');
}
System.out.println(output);
}
return results;
}
use of com.google.zxing.BinaryBitmap in project zxing by zxing.
the class GUIRunner method getDecodeText.
private static String getDecodeText(Path file) {
BufferedImage image;
try {
image = ImageReader.readImage(file.toUri());
} catch (IOException ioe) {
return ioe.toString();
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
try {
result = new MultiFormatReader().decode(bitmap);
} catch (ReaderException re) {
return re.toString();
}
return String.valueOf(result.getText());
}
use of com.google.zxing.BinaryBitmap 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.BinaryBitmap in project zxing by zxing.
the class RSSExpandedInternalTestCase method testDecodeDataCharacter.
@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);
//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, false);
assertEquals(19, dataCharacter.getValue());
assertEquals(1007, dataCharacter.getChecksumPortion());
}
use of com.google.zxing.BinaryBitmap in project zxing by zxing.
the class TestCaseUtil method getBinaryBitmap.
static BinaryBitmap getBinaryBitmap(String path) throws IOException {
BufferedImage bufferedImage = getBufferedImage(path);
BufferedImageLuminanceSource luminanceSource = new BufferedImageLuminanceSource(bufferedImage);
return new BinaryBitmap(new GlobalHistogramBinarizer(luminanceSource));
}
Aggregations