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=6e380df0c0e171994a3bb7fea2764ec407c0818f;hpb=35b22c6706f613e7612db3b0f229af191faff0cc;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 6e380df0..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,99 +16,164 @@ package com.google.zxing.client.j2se; -import javax.imageio.ImageIO; +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 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 = 0xFF000000; + private static final int RED = 0xFFFF0000; + private static boolean rowSampling = false; private ImageConverter() { } public static void main(String[] args) throws Exception { - for (int i = 0; i < args.length; i++) { - File inputFile = new File(args[i]); + 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()) { - int count = 0; for (File input : inputFile.listFiles()) { - convertImage(input.toURI(), count++); + 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 { - convertImage(inputFile.toURI(), 0); + convertImage(inputFile.toURI()); } } else { - convertImage(new URI(args[i]), 0); + convertImage(new URI(arg)); } } } - private static void convertImage(URI uri, int count) throws IOException { + private static void convertImage(URI uri) throws IOException { BufferedImage image = ImageIO.read(uri.toURL()); - BufferedImageMonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image); - int width = src.getWidth(); - int height = src.getHeight(); - - BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY); - int white = 0xFFFFFFFF; - int black = 0x000000FF; - 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) { + } } - File output = getOutput(uri, count); + File output = getOutput(uri); System.out.printf("Writing output to %s\n", output); ImageIO.write(result, FORMAT, output); } private static File getFileOfUri(URI uri) { String name = uri.getPath(); - int slashPos = name.lastIndexOf('/'); - String parent, basename; - if (slashPos != -1 && slashPos != name.length()-1) { + int slashPos = name.lastIndexOf((int) '/'); + String parent; + String basename; + if (slashPos != -1 && slashPos != name.length() - 1) { parent = name.substring(0, slashPos); - basename = name.substring(slashPos+1); + basename = name.substring(slashPos + 1); } else { parent = "."; basename = name; } File parentFile = new File(parent); - if (!parentFile.exists()) + if (!parentFile.exists()) { return null; + } - File baseFile = new File(parent,basename); - if (!baseFile.exists()) + File baseFile = new File(parent, basename); + if (!baseFile.exists()) { return null; + } return baseFile; } - - private static File getOutput(URI uri, int count) { + private static File getOutput(URI uri) { File result = getFileOfUri(uri); if (result == null) { result = new File("ConvertedImage." + FORMAT); } else { String name = result.getPath(); - int dotpos = name.lastIndexOf('.'); - if (dotpos != -1) + int dotpos = name.lastIndexOf((int) '.'); + 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; }