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.BlackPointEstimationMethod;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
24 import java.awt.image.BufferedImage;
26 import java.io.IOException;
29 import javax.imageio.ImageIO;
32 * Utility application for evaluating the effectiveness of the BlackPointEstimator used by
33 * MonochromeBitmapSource. Given a set of images on the command line, it converts each to a
34 * black-and-white PNG. The result is placed in a file based on the input name, with either
35 * "_converted_row" or "_converted_2d" appended.
37 * @author alasdair@google.com (Alasdair Mackintosh)
38 * @author dswitkin@google.com (Daniel Switkin)
40 public final class ImageConverter {
42 private static final String FORMAT = "png";
43 private static final int WHITE = 0xFFFFFFFF;
44 private static final int BLACK = 0xFF000000;
45 private static final int RED = 0xFFFF0000;
46 private static BlackPointEstimationMethod sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
48 private ImageConverter() {
51 public static void main(String[] args) throws Exception {
52 for (String arg : args) {
53 if (arg.equals("-row")) {
54 sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
55 } else if (arg.equals("-2d")) {
56 sMethod = BlackPointEstimationMethod.TWO_D_SAMPLING;
57 } else if (arg.startsWith("-")) {
58 System.out.println("Ignoring unrecognized option: " + arg);
61 for (String arg : args) {
62 if (arg.startsWith("-")) {
65 File inputFile = new File(arg);
66 if (inputFile.exists()) {
67 if (inputFile.isDirectory()) {
68 for (File input : inputFile.listFiles()) {
69 String filename = input.getName().toLowerCase();
70 // Skip hidden files and text files (the latter is found in the blackbox tests).
71 if (filename.startsWith(".") || filename.endsWith(".txt")) {
74 // Skip the results of dumping the black point.
75 if (filename.contains(".mono.png") || filename.contains(".row.png") ||
76 filename.contains(".2d.png")) {
79 convertImage(input.toURI());
82 convertImage(inputFile.toURI());
85 convertImage(new URI(arg));
91 private static void convertImage(URI uri) throws IOException {
92 BufferedImage image = ImageIO.read(uri.toURL());
93 MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
94 int width = src.getWidth();
95 int height = src.getHeight();
97 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
98 BitArray array = new BitArray(width);
101 // Run the 2D sampling once up front
102 if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
103 src.estimateBlackPoint(sMethod, 0);
105 } catch (ReaderException e) {
106 System.out.println(e.toString());
110 for (int y = 0; y < height; y++) {
111 // Run the 1D sampling once per row
112 if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
114 src.estimateBlackPoint(sMethod, y);
115 } catch (ReaderException e) {
116 // Draw rows with insufficient dynamic range in red
117 for (int x = 0; x < width; x++) {
118 result.setRGB(x, y, RED);
124 // Fetch the entire row at once, then fill out the result image
125 src.getBlackRow(y, array, 0, width);
126 for (int x = 0; x < width; x++) {
127 result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
132 File output = getOutput(uri);
133 System.out.printf("Writing output to %s\n", output);
134 ImageIO.write(result, FORMAT, output);
137 private static File getFileOfUri(URI uri) {
138 String name = uri.getPath();
139 int slashPos = name.lastIndexOf((int) '/');
140 String parent, basename;
141 if (slashPos != -1 && slashPos != name.length() - 1) {
142 parent = name.substring(0, slashPos);
143 basename = name.substring(slashPos + 1);
148 File parentFile = new File(parent);
149 if (!parentFile.exists()) {
153 File baseFile = new File(parent, basename);
154 if (!baseFile.exists()) {
161 private static File getOutput(URI uri) {
162 File result = getFileOfUri(uri);
163 if (result == null) {
164 result = new File("ConvertedImage." + FORMAT);
166 String name = result.getPath();
167 int dotpos = name.lastIndexOf((int) '.');
169 name = name.substring(0, dotpos);
171 String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
172 result = new File(name + '.' + suffix + '.' + FORMAT);