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.NotFoundException;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.common.BitArray;
24 import com.google.zxing.common.BitMatrix;
25 import com.google.zxing.common.HybridBinarizer;
27 import java.awt.image.BufferedImage;
29 import java.io.IOException;
32 import javax.imageio.ImageIO;
35 * A utility application for evaluating the effectiveness of various thresholding algorithms.
36 * Given a set of images on the command line, it converts each to a black-and-white PNG.
37 * The result is placed in a file based on the input name, with either ".row.png" or ".2d.png"
40 * TODO: Needs to be updated to accept different Binarizer implementations.
41 * TODO: Consider whether to keep this separate app, as CommandLineRunner has similar functionality.
43 * @author alasdair@google.com (Alasdair Mackintosh)
44 * @author dswitkin@google.com (Daniel Switkin)
46 public final class ImageConverter {
48 private static final String FORMAT = "png";
49 private static final int WHITE = 0xFFFFFFFF;
50 private static final int BLACK = 0xFF000000;
51 private static final int RED = 0xFFFF0000;
52 private static boolean rowSampling = false;
54 private ImageConverter() {
57 public static void main(String[] args) throws Exception {
58 for (String arg : args) {
59 if ("-row".equals(arg)) {
61 } else if ("-2d".equals(arg)) {
63 } else if (arg.startsWith("-")) {
64 System.err.println("Ignoring unrecognized option: " + arg);
67 for (String arg : args) {
68 if (arg.startsWith("-")) {
71 File inputFile = new File(arg);
72 if (inputFile.exists()) {
73 if (inputFile.isDirectory()) {
74 for (File input : inputFile.listFiles()) {
75 String filename = input.getName().toLowerCase();
76 // Skip hidden files and text files (the latter is found in the blackbox tests).
77 if (filename.startsWith(".") || filename.endsWith(".txt")) {
80 // Skip the results of dumping the black point.
81 if (filename.contains(".mono.png") || filename.contains(".row.png") ||
82 filename.contains(".2d.png")) {
85 convertImage(input.toURI());
88 convertImage(inputFile.toURI());
91 convertImage(new URI(arg));
97 private static void convertImage(URI uri) throws IOException {
98 BufferedImage image = ImageIO.read(uri.toURL());
99 LuminanceSource source = new BufferedImageLuminanceSource(image);
100 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
101 int width = bitmap.getWidth();
102 int height = bitmap.getHeight();
104 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
105 BitArray array = new BitArray(width);
108 for (int y = 0; y < height; y++) {
110 array = bitmap.getBlackRow(y, array);
111 } catch (NotFoundException nfe) {
112 // Draw rows with insufficient dynamic range in red
113 for (int x = 0; x < width; x++) {
114 result.setRGB(x, y, RED);
119 for (int x = 0; x < width; x++) {
120 result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
125 BitMatrix matrix = bitmap.getBlackMatrix();
126 for (int y = 0; y < height; y++) {
127 for (int x = 0; x < width; x++) {
128 result.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
131 } catch (NotFoundException nfe) {
136 File output = getOutput(uri);
137 System.out.printf("Writing output to %s\n", output);
138 ImageIO.write(result, FORMAT, output);
141 private static File getFileOfUri(URI uri) {
142 String name = uri.getPath();
143 int slashPos = name.lastIndexOf((int) '/');
146 if (slashPos != -1 && slashPos != name.length() - 1) {
147 parent = name.substring(0, slashPos);
148 basename = name.substring(slashPos + 1);
153 File parentFile = new File(parent);
154 if (!parentFile.exists()) {
158 File baseFile = new File(parent, basename);
159 if (!baseFile.exists()) {
166 private static File getOutput(URI uri) {
167 File result = getFileOfUri(uri);
168 if (result == null) {
169 result = new File("ConvertedImage." + FORMAT);
171 String name = result.getPath();
172 int dotpos = name.lastIndexOf((int) '.');
174 name = name.substring(0, dotpos);
176 String suffix = rowSampling ? "row" : "2d";
177 result = new File(name + '.' + suffix + '.' + FORMAT);