X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=javase%2Fsrc%2Fcom%2Fgoogle%2Fzxing%2Fclient%2Fj2se%2FImageConverter.java;h=538e59a06d0d85090787b2a120365e4ae740f7bc;hb=608fe313d82bb79f0f5a96856697bdfb4a6305d3;hp=180fcb780af5f9568eaec4af9cbb26adab961bb4;hpb=61db77fd4960c475243bfb49cc3b6877aef5d8de;p=zxing.git diff --git a/javase/src/com/google/zxing/client/j2se/ImageConverter.java b/javase/src/com/google/zxing/client/j2se/ImageConverter.java index 180fcb78..538e59a0 100644 --- a/javase/src/com/google/zxing/client/j2se/ImageConverter.java +++ b/javase/src/com/google/zxing/client/j2se/ImageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2007 Google Inc. + * Copyright 2008 ZXing authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,37 +16,71 @@ package com.google.zxing.client.j2se; -import com.google.zxing.MonochromeBitmapSource; +import com.google.zxing.BinaryBitmap; +import com.google.zxing.LuminanceSource; +import com.google.zxing.ReaderException; +import com.google.zxing.common.BitArray; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.common.HybridBinarizer; -import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.net.URI; +import javax.imageio.ImageIO; + /** - * Utility application for evaluating the effectiveness of the - * MonochromeBitmapSource. Given a set of images on the command line, - * converts each to a black-and-white GIF. The result is placed in - * a file based on the input name, with "_converted" appended. + * A utility application for evaluating the effectiveness of various thresholding algorithms. + * Given a set of images on the command line, it converts each to a black-and-white PNG. + * The result is placed in a file based on the input name, with either ".row.png" or ".2d.png" + * appended. + * + * TODO: Needs to be updated to accept different Binarizer implementations. + * TODO: Consider whether to keep this separate app, as CommandLineRunner has similar functionality. * * @author alasdair@google.com (Alasdair Mackintosh) + * @author dswitkin@google.com (Daniel Switkin) */ public final class ImageConverter { - private static final String FORMAT = "gif"; + private static final String FORMAT = "png"; private static final int WHITE = 0xFFFFFFFF; - private static final int BLACK = 0x000000FF; + private static final int BLACK = 0xFF000000; + private static final int RED = 0xFFFF0000; + private static boolean rowSampling = false; private ImageConverter() { } public static void main(String[] args) throws Exception { for (String arg : args) { + if ("-row".equals(arg)) { + rowSampling = true; + } else if ("-2d".equals(arg)) { + rowSampling = false; + } else if (arg.startsWith("-")) { + System.err.println("Ignoring unrecognized option: " + arg); + } + } + for (String arg : args) { + if (arg.startsWith("-")) { + continue; + } File inputFile = new File(arg); if (inputFile.exists()) { if (inputFile.isDirectory()) { for (File input : inputFile.listFiles()) { + String filename = input.getName().toLowerCase(); + // Skip hidden files and text files (the latter is found in the blackbox tests). + if (filename.startsWith(".") || filename.endsWith(".txt")) { + continue; + } + // Skip the results of dumping the black point. + if (filename.contains(".mono.png") || filename.contains(".row.png") || + filename.contains(".2d.png")) { + continue; + } convertImage(input.toURI()); } } else { @@ -61,14 +95,40 @@ public final class ImageConverter { private static void convertImage(URI uri) throws IOException { BufferedImage image = ImageIO.read(uri.toURL()); - MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image); - int width = src.getWidth(); - int height = src.getHeight(); - - BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); - for (int i = 0; i < width; i++) { - for (int j = 0; j < height; j++) { - result.setRGB(i, j, src.isBlack(i, j) ? BLACK : WHITE); + LuminanceSource source = new BufferedImageLuminanceSource(image); + BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); + int width = bitmap.getWidth(); + int height = bitmap.getHeight(); + + BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); + BitArray array = new BitArray(width); + + if (rowSampling) { + for (int y = 0; y < height; y++) { + try { + array = bitmap.getBlackRow(y, array); + } catch (ReaderException e) { + // Draw rows with insufficient dynamic range in red + for (int x = 0; x < width; x++) { + result.setRGB(x, y, RED); + } + continue; + } + + for (int x = 0; x < width; x++) { + result.setRGB(x, y, array.get(x) ? BLACK : WHITE); + } + } + } else { + try { + BitMatrix matrix = bitmap.getBlackMatrix(); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + result.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); + } + } + } catch (ReaderException e) { + } } @@ -80,7 +140,8 @@ public final class ImageConverter { private static File getFileOfUri(URI uri) { String name = uri.getPath(); int slashPos = name.lastIndexOf((int) '/'); - String parent, basename; + String parent; + String basename; if (slashPos != -1 && slashPos != name.length() - 1) { parent = name.substring(0, slashPos); basename = name.substring(slashPos + 1); @@ -111,7 +172,8 @@ public final class ImageConverter { if (dotpos != -1) { name = name.substring(0, dotpos); } - result = new File(name + "_converted." + FORMAT); + String suffix = rowSampling ? "row" : "2d"; + result = new File(name + '.' + suffix + '.' + FORMAT); } return result; }