Related to Issue 205, but not the direct issue: read both copies of the format info...
[zxing.git] / javase / src / com / google / zxing / client / j2se / MatrixToImageWriter.java
1 /*
2  * Copyright 2009 ZXing authors
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 com.google.zxing.common.BitMatrix;
20 import com.google.zxing.common.ByteMatrix;
21
22 import javax.imageio.ImageIO;
23 import java.io.File;
24 import java.io.OutputStream;
25 import java.io.IOException;
26 import java.awt.image.BufferedImage;
27
28 /**
29  * Writes a {@link BitMatrix} or {@link ByteMatrix} to {@link BufferedImage},
30  * file or stream. Provided here instead of core since it depends on
31  * Java SE libraries.
32  *
33  * @author Sean Owen
34  */
35 public final class MatrixToImageWriter {
36
37   private static final int BLACK = 0xFF000000;
38   private static final int WHITE = 0xFFFFFFFF;
39
40   private MatrixToImageWriter() {}
41
42   /**
43    * Renders a {@link BitMatrix} as an image, where "false" bits are rendered
44    * as white, and "true" bits are rendered as black.
45    */
46   public static BufferedImage toBufferedImage(BitMatrix matrix) {
47     int width = matrix.getWidth();
48     int height = matrix.getHeight();
49     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
50     for (int x = 0; x < width; x++) {
51       for (int y = 0; y < height; y++) {
52         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
53       }
54     }
55     return image;
56   }
57
58   /**
59    * Renders a {@link ByteMatrix} as an image, as a
60    * {@link BufferedImage}. The byte values are construed as (unsigned)
61    * luminance values, in theory.
62    * However, anything but 0 will be rendered as white, and 0 will be
63    * rendered as black.
64    */
65   public static BufferedImage toBufferedImage(ByteMatrix matrix) {
66     int width = matrix.getWidth();
67     int height = matrix.getHeight();
68     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
69     for (int x = 0; x < width; x++) {
70       for (int y = 0; y < height; y++) {
71         image.setRGB(x, y, matrix.get(x, y) == 0 ? WHITE : BLACK);
72       }
73     }
74     return image;
75   }
76
77   /**
78    * Writes a {@link BitMatrix} to a file.
79    *
80    * @see #toBufferedImage(BitMatrix)
81    */
82   public static void writeToFile(BitMatrix matrix, String format, File file)
83           throws IOException {
84     BufferedImage image = toBufferedImage(matrix);
85     ImageIO.write(image, format, file);
86   }
87
88   /**
89    * Writes a {@link ByteMatrix} to a file.
90    *
91    * @see #toBufferedImage(ByteMatrix)
92    */
93   public static void writeToFile(ByteMatrix matrix, String format, File file)
94           throws IOException {
95     BufferedImage image = toBufferedImage(matrix);
96     ImageIO.write(image, format, file);
97   }
98
99   /**
100    * Writes a {@link BitMatrix} to a stream.
101    *
102    * @see #toBufferedImage(BitMatrix)
103    */
104   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
105           throws IOException {
106     BufferedImage image = toBufferedImage(matrix);
107     ImageIO.write(image, format, stream);
108   }
109
110   /**
111    * Writes a {@link ByteMatrix} to a stream.
112    *
113    * @see #toBufferedImage(ByteMatrix)
114    */
115   public static void writeToStream(ByteMatrix matrix, String format, OutputStream stream)
116           throws IOException {
117     BufferedImage image = toBufferedImage(matrix);
118     ImageIO.write(image, format, stream);
119   }
120
121 }