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 javax.imageio.ImageIO;
20 import java.awt.image.BufferedImage;
22 import java.io.IOException;
26 * Utility application for evaluating the effectiveness of the
27 * MonochromeBitmapSource. Given a set of images on the command line,
28 * converts each to a black-and-white GIF. The result is placed in
29 * a file based on the input name, with "_converted" appended.
31 * @author alasdair@google.com (Alasdair Mackintosh)
33 public final class ImageConverter {
35 private static final String FORMAT = "gif";
37 private ImageConverter() {
40 public static void main(String[] args) throws Exception {
41 for (int i = 0; i < args.length; i++) {
42 File inputFile = new File(args[i]);
43 if (inputFile.exists()) {
44 if (inputFile.isDirectory()) {
46 for (File input : inputFile.listFiles()) {
47 convertImage(input.toURI(), count++);
50 convertImage(inputFile.toURI(), 0);
53 convertImage(new URI(args[i]), 0);
59 private static void convertImage(URI uri, int count) throws IOException {
60 BufferedImage image = ImageIO.read(uri.toURL());
61 BufferedImageMonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
62 int width = src.getWidth();
63 int height = src.getHeight();
65 BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
66 int white = 0xFFFFFFFF;
67 int black = 0x000000FF;
68 for (int i = 0; i < width; i++) {
69 for (int j = 0; j < height; j++) {
70 result.setRGB(i, j, src.isBlack(i,j) ? black : white);
74 File output = getOutput(uri, count);
75 System.out.printf("Writing output to %s\n", output);
76 ImageIO.write(result, FORMAT, output);
79 private static File getFileOfUri(URI uri) {
80 String name = uri.getPath();
81 int slashPos = name.lastIndexOf('/');
82 String parent, basename;
83 if (slashPos != -1 && slashPos != name.length()-1) {
84 parent = name.substring(0, slashPos);
85 basename = name.substring(slashPos+1);
90 File parentFile = new File(parent);
91 if (!parentFile.exists())
94 File baseFile = new File(parent,basename);
95 if (!baseFile.exists())
102 private static File getOutput(URI uri, int count) {
103 File result = getFileOfUri(uri);
104 if (result == null) {
105 result = new File("ConvertedImage." + FORMAT);
107 String name = result.getPath();
108 int dotpos = name.lastIndexOf('.');
110 name = name.substring(0, dotpos);
111 result = new File(name + "_converted." + FORMAT);