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.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
24 import javax.imageio.ImageIO;
25 import java.awt.image.BufferedImage;
27 import java.io.IOException;
31 * Utility application for evaluating the effectiveness of the BlackPointEstimator used by
32 * MonochromeBitmapSource. Given a set of images on the command line, it converts each to a
33 * black-and-white PNG. The result is placed in a file based on the input name, with either
34 * "_converted_row" or "_converted_2d" appended.
36 * @author alasdair@google.com (Alasdair Mackintosh)
37 * @author dswitkin@google.com (Daniel Switkin)
39 public final class ImageConverter {
41 private static final String FORMAT = "png";
42 private static final int WHITE = 0xFFFFFFFF;
43 private static final int BLACK = 0xFF000000;
44 private static final int RED = 0xFFFF0000;
45 private static BlackPointEstimationMethod sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
47 private ImageConverter() {
50 public static void main(String[] args) throws Exception {
51 for (String arg : args) {
52 if (arg.equals("-row")) {
53 sMethod = BlackPointEstimationMethod.ROW_SAMPLING;
54 } else if (arg.equals("-2d")) {
55 sMethod = BlackPointEstimationMethod.TWO_D_SAMPLING;
56 } else if (arg.startsWith("-")) {
57 System.out.println("Ignoring unrecognized option: " + arg);
60 for (String arg : args) {
61 if (arg.startsWith("-")) continue;
62 File inputFile = new File(arg);
63 if (inputFile.exists()) {
64 if (inputFile.isDirectory()) {
65 for (File input : inputFile.listFiles()) {
66 convertImage(input.toURI());
69 convertImage(inputFile.toURI());
72 convertImage(new URI(arg));
78 private static void convertImage(URI uri) throws IOException {
79 BufferedImage image = ImageIO.read(uri.toURL());
80 MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
81 int width = src.getWidth();
82 int height = src.getHeight();
84 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
85 BitArray array = new BitArray(width);
88 // Run the 2D sampling once up front
89 if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
90 src.estimateBlackPoint(sMethod, 0);
92 } catch (ReaderException e) {
93 System.out.println(e.toString());
97 for (int y = 0; y < height; y++) {
98 // Run the 1D sampling once per row
99 if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
101 src.estimateBlackPoint(sMethod, y);
102 } catch (ReaderException e) {
103 // Draw rows with insufficient dynamic range in red
104 for (int x = 0; x < width; x++) {
105 result.setRGB(x, y, RED);
111 // Fetch the entire row at once, then fill out the result image
112 src.getBlackRow(y, array, 0, width);
113 for (int x = 0; x < width; x++) {
114 result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
119 File output = getOutput(uri);
120 System.out.printf("Writing output to %s\n", output);
121 ImageIO.write(result, FORMAT, output);
124 private static File getFileOfUri(URI uri) {
125 String name = uri.getPath();
126 int slashPos = name.lastIndexOf((int) '/');
127 String parent, basename;
128 if (slashPos != -1 && slashPos != name.length() - 1) {
129 parent = name.substring(0, slashPos);
130 basename = name.substring(slashPos + 1);
135 File parentFile = new File(parent);
136 if (!parentFile.exists()) {
140 File baseFile = new File(parent, basename);
141 if (!baseFile.exists()) {
148 private static File getOutput(URI uri) {
149 File result = getFileOfUri(uri);
150 if (result == null) {
151 result = new File("ConvertedImage." + FORMAT);
153 String name = result.getPath();
154 int dotpos = name.lastIndexOf((int) '.');
156 name = name.substring(0, dotpos);
158 String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
159 result = new File(name + "_converted_" + suffix + "." + FORMAT);