Boneheaded mistake: was writing transparent pixels
[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}, file or stream.
30  * Provided here instead of core since it depends on Java SE libraries.
31  *
32  * @author Sean Owen
33  */
34 public final class MatrixToImageWriter {
35
36   private static final int BLACK = 0xFF000000;
37   private static final int WHITE = 0xFFFFFFFF;
38
39   private MatrixToImageWriter() {}
40
41   public static BufferedImage toBufferedImage(BitMatrix matrix) {
42     int width = matrix.getWidth();
43     int height = matrix.getHeight();
44     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
45     for (int x = 0; x < width; x++) {
46       for (int y = 0; y < height; y++) {
47         image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
48       }
49     }
50     return image;
51   }
52
53   public static BufferedImage toBufferedImage(ByteMatrix matrix) {
54     int width = matrix.getWidth();
55     int height = matrix.getHeight();
56     BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
57     for (int x = 0; x < width; x++) {
58       for (int y = 0; y < height; y++) {
59         image.setRGB(x, y, matrix.get(x, y) == 0 ? WHITE : BLACK);
60       }
61     }
62     return image;
63   }
64
65   public static void writeToFile(BitMatrix matrix, String format, File file)
66           throws IOException {
67     BufferedImage image = toBufferedImage(matrix);
68     ImageIO.write(image, format, file);
69   }
70
71   public static void writeToFile(ByteMatrix matrix, String format, File file)
72           throws IOException {
73     BufferedImage image = toBufferedImage(matrix);
74     ImageIO.write(image, format, file);
75   }
76
77   public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
78           throws IOException {
79     BufferedImage image = toBufferedImage(matrix);
80     ImageIO.write(image, format, stream);
81   }
82
83   public static void writeToStream(ByteMatrix matrix, String format, OutputStream stream)
84           throws IOException {
85     BufferedImage image = toBufferedImage(matrix);
86     ImageIO.write(image, format, stream);
87   }
88
89 }