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()) {
50 for (File input : inputFile.listFiles()) {
51 convertImage(input.toURI());
54 convertImage(inputFile.toURI());
57 convertImage(new URI(arg));
63 private static void convertImage(URI uri) throws IOException {
64 BufferedImage image = ImageIO.read(uri.toURL());
65 MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
66 int width = src.getWidth();
67 int height = src.getHeight();
69 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
70 for (int i = 0; i < width; i++) {
71 for (int j = 0; j < height; j++) {
72 result.setRGB(i, j, src.isBlack(i, j) ? BLACK : WHITE);
76 File output = getOutput(uri);
77 System.out.printf("Writing output to %s\n", output);
78 ImageIO.write(result, FORMAT, output);
81 private static File getFileOfUri(URI uri) {
82 String name = uri.getPath();
83 int slashPos = name.lastIndexOf((int) '/');
84 String parent, basename;
85 if (slashPos != -1 && slashPos != name.length() - 1) {
86 parent = name.substring(0, slashPos);
87 basename = name.substring(slashPos + 1);
92 File parentFile = new File(parent);
93 if (!parentFile.exists()) {
97 File baseFile = new File(parent, basename);
98 if (!baseFile.exists()) {
105 private static File getOutput(URI uri) {
106 File result = getFileOfUri(uri);
107 if (result == null) {
108 result = new File("ConvertedImage." + FORMAT);
110 String name = result.getPath();
111 int dotpos = name.lastIndexOf((int) '.');
113 name = name.substring(0, dotpos);
115 result = new File(name + "_converted." + FORMAT);