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("-")) {
64 File inputFile = new File(arg);
65 if (inputFile.exists()) {
66 if (inputFile.isDirectory()) {
67 for (File input : inputFile.listFiles()) {
68 convertImage(input.toURI());
71 convertImage(inputFile.toURI());
74 convertImage(new URI(arg));
80 private static void convertImage(URI uri) throws IOException {
81 BufferedImage image = ImageIO.read(uri.toURL());
82 MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
83 int width = src.getWidth();
84 int height = src.getHeight();
86 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
87 BitArray array = new BitArray(width);
90 // Run the 2D sampling once up front
91 if (sMethod == BlackPointEstimationMethod.TWO_D_SAMPLING) {
92 src.estimateBlackPoint(sMethod, 0);
94 } catch (ReaderException e) {
95 System.out.println(e.toString());
99 for (int y = 0; y < height; y++) {
100 // Run the 1D sampling once per row
101 if (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
103 src.estimateBlackPoint(sMethod, y);
104 } catch (ReaderException e) {
105 // Draw rows with insufficient dynamic range in red
106 for (int x = 0; x < width; x++) {
107 result.setRGB(x, y, RED);
113 // Fetch the entire row at once, then fill out the result image
114 src.getBlackRow(y, array, 0, width);
115 for (int x = 0; x < width; x++) {
116 result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
121 File output = getOutput(uri);
122 System.out.printf("Writing output to %s\n", output);
123 ImageIO.write(result, FORMAT, output);
126 private static File getFileOfUri(URI uri) {
127 String name = uri.getPath();
128 int slashPos = name.lastIndexOf((int) '/');
129 String parent, basename;
130 if (slashPos != -1 && slashPos != name.length() - 1) {
131 parent = name.substring(0, slashPos);
132 basename = name.substring(slashPos + 1);
137 File parentFile = new File(parent);
138 if (!parentFile.exists()) {
142 File baseFile = new File(parent, basename);
143 if (!baseFile.exists()) {
150 private static File getOutput(URI uri) {
151 File result = getFileOfUri(uri);
152 if (result == null) {
153 result = new File("ConvertedImage." + FORMAT);
155 String name = result.getPath();
156 int dotpos = name.lastIndexOf((int) '.');
158 name = name.substring(0, dotpos);
160 String suffix = (sMethod == BlackPointEstimationMethod.ROW_SAMPLING) ? "row" : "2d";
161 result = new File(name + "_converted_" + suffix + '.' + FORMAT);