Initial revision
[zxing.git] / javase / src / com / google / zxing / client / j2se / ImageConverter.java
1 /*
2  * Copyright 2007 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.j2se;
18
19 import javax.imageio.ImageIO;
20 import java.awt.image.BufferedImage;
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URI;
24
25 /**
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.
30  *
31  * @author alasdair@google.com (Alasdair Mackintosh)
32  */
33 public final class ImageConverter {
34
35   private static final String FORMAT = "gif";
36
37   private ImageConverter() {
38   }
39
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()) {
45           int count = 0;
46           for (File input : inputFile.listFiles()) {
47             convertImage(input.toURI(), count++);
48           }
49         } else {
50           convertImage(inputFile.toURI(), 0);
51         }
52       } else {
53         convertImage(new URI(args[i]), 0);
54       }
55     }
56   }
57
58
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();
64     
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);
71       }
72     }
73
74     File output = getOutput(uri, count);
75     System.out.printf("Writing output to %s\n", output);
76     ImageIO.write(result, FORMAT, output);
77   }
78
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);
86     } else {
87       parent = ".";
88       basename = name;
89     }
90     File parentFile = new File(parent);
91     if (!parentFile.exists())
92       return null;
93
94     File baseFile = new File(parent,basename);
95     if (!baseFile.exists())
96       return null;
97
98     return baseFile;
99   }
100     
101
102   private static File getOutput(URI uri, int count) {
103     File result = getFileOfUri(uri);
104     if (result == null) {
105       result = new File("ConvertedImage." + FORMAT);
106     } else {
107       String name = result.getPath();
108       int dotpos = name.lastIndexOf('.');
109       if (dotpos != -1)
110         name = name.substring(0, dotpos);
111       result = new File(name + "_converted." + FORMAT);
112     }
113     return result;
114   }
115
116 }