2 * Copyright 2008 ZXing authors
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.google.zxing.client.j2se;
19 import com.google.zxing.BinaryBitmap;
20 import com.google.zxing.LuminanceSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.BitMatrix;
24 import com.google.zxing.common.HybridBinarizer;
26 import java.awt.image.BufferedImage;
28 import java.io.IOException;
31 import javax.imageio.ImageIO;
34 * A utility application for evaluating the effectiveness of various thresholding algorithms.
35 * Given a set of images on the command line, it converts each to a black-and-white PNG.
36 * The result is placed in a file based on the input name, with either ".row.png" or ".2d.png"
39 * TODO: Needs to be updated to accept different Binarizer implementations.
40 * TODO: Consider whether to keep this separate app, as CommandLineRunner has similar functionality.
42 * @author alasdair@google.com (Alasdair Mackintosh)
43 * @author dswitkin@google.com (Daniel Switkin)
45 public final class ImageConverter {
47 private static final String FORMAT = "png";
48 private static final int WHITE = 0xFFFFFFFF;
49 private static final int BLACK = 0xFF000000;
50 private static final int RED = 0xFFFF0000;
51 private static boolean rowSampling = false;
53 private ImageConverter() {
56 public static void main(String[] args) throws Exception {
57 for (String arg : args) {
58 if ("-row".equals(arg)) {
60 } else if ("-2d".equals(arg)) {
62 } else if (arg.startsWith("-")) {
63 System.err.println("Ignoring unrecognized option: " + arg);
66 for (String arg : args) {
67 if (arg.startsWith("-")) {
70 File inputFile = new File(arg);
71 if (inputFile.exists()) {
72 if (inputFile.isDirectory()) {
73 for (File input : inputFile.listFiles()) {
74 String filename = input.getName().toLowerCase();
75 // Skip hidden files and text files (the latter is found in the blackbox tests).
76 if (filename.startsWith(".") || filename.endsWith(".txt")) {
79 // Skip the results of dumping the black point.
80 if (filename.contains(".mono.png") || filename.contains(".row.png") ||
81 filename.contains(".2d.png")) {
84 convertImage(input.toURI());
87 convertImage(inputFile.toURI());
90 convertImage(new URI(arg));
96 private static void convertImage(URI uri) throws IOException {
97 BufferedImage image = ImageIO.read(uri.toURL());
98 LuminanceSource source = new BufferedImageLuminanceSource(image);
99 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
100 int width = bitmap.getWidth();
101 int height = bitmap.getHeight();
103 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
104 BitArray array = new BitArray(width);
107 for (int y = 0; y < height; y++) {
109 array = bitmap.getBlackRow(y, array);
110 } catch (ReaderException e) {
111 // Draw rows with insufficient dynamic range in red
112 for (int x = 0; x < width; x++) {
113 result.setRGB(x, y, RED);
118 for (int x = 0; x < width; x++) {
119 result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
124 BitMatrix matrix = bitmap.getBlackMatrix();
125 for (int y = 0; y < height; y++) {
126 for (int x = 0; x < width; x++) {
127 result.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
130 } catch (ReaderException e) {
135 File output = getOutput(uri);
136 System.out.printf("Writing output to %s\n", output);
137 ImageIO.write(result, FORMAT, output);
140 private static File getFileOfUri(URI uri) {
141 String name = uri.getPath();
142 int slashPos = name.lastIndexOf((int) '/');
145 if (slashPos != -1 && slashPos != name.length() - 1) {
146 parent = name.substring(0, slashPos);
147 basename = name.substring(slashPos + 1);
152 File parentFile = new File(parent);
153 if (!parentFile.exists()) {
157 File baseFile = new File(parent, basename);
158 if (!baseFile.exists()) {
165 private static File getOutput(URI uri) {
166 File result = getFileOfUri(uri);
167 if (result == null) {
168 result = new File("ConvertedImage." + FORMAT);
170 String name = result.getPath();
171 int dotpos = name.lastIndexOf((int) '.');
173 name = name.substring(0, dotpos);
175 String suffix = rowSampling ? "row" : "2d";
176 result = new File(name + '.' + suffix + '.' + FORMAT);