2 * Copyright 2007 Google Inc.
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;
21 import javax.imageio.ImageIO;
22 import java.awt.image.BufferedImage;
24 import java.io.IOException;
28 * Utility application for evaluating the effectiveness of the
29 * MonochromeBitmapSource. Given a set of images on the command line,
30 * converts each to a black-and-white GIF. The result is placed in
31 * a file based on the input name, with "_converted" appended.
33 * @author alasdair@google.com (Alasdair Mackintosh)
35 public final class ImageConverter {
37 private static final String FORMAT = "gif";
38 private static final int WHITE = 0xFFFFFFFF;
39 private static final int BLACK = 0x000000FF;
41 private ImageConverter() {
44 public static void main(String[] args) throws Exception {
45 for (String arg : args) {
46 File inputFile = new File(arg);
47 if (inputFile.exists()) {
48 if (inputFile.isDirectory()) {
49 for (File input : inputFile.listFiles()) {
50 convertImage(input.toURI());
53 convertImage(inputFile.toURI());
56 convertImage(new URI(arg));
62 private static void convertImage(URI uri) throws IOException {
63 BufferedImage image = ImageIO.read(uri.toURL());
64 MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
65 int width = src.getWidth();
66 int height = src.getHeight();
68 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
69 for (int i = 0; i < width; i++) {
70 for (int j = 0; j < height; j++) {
71 result.setRGB(i, j, src.isBlack(i, j) ? BLACK : WHITE);
75 File output = getOutput(uri);
76 System.out.printf("Writing output to %s\n", output);
77 ImageIO.write(result, FORMAT, output);
80 private static File getFileOfUri(URI uri) {
81 String name = uri.getPath();
82 int slashPos = name.lastIndexOf((int) '/');
83 String parent, basename;
84 if (slashPos != -1 && slashPos != name.length() - 1) {
85 parent = name.substring(0, slashPos);
86 basename = name.substring(slashPos + 1);
91 File parentFile = new File(parent);
92 if (!parentFile.exists()) {
96 File baseFile = new File(parent, basename);
97 if (!baseFile.exists()) {
104 private static File getOutput(URI uri) {
105 File result = getFileOfUri(uri);
106 if (result == null) {
107 result = new File("ConvertedImage." + FORMAT);
109 String name = result.getPath();
110 int dotpos = name.lastIndexOf((int) '.');
112 name = name.substring(0, dotpos);
114 result = new File(name + "_converted." + FORMAT);