2 * Copyright 2009 ZXing authors
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.common.BitMatrix;
20 import com.google.zxing.common.ByteMatrix;
22 import javax.imageio.ImageIO;
24 import java.io.OutputStream;
25 import java.io.IOException;
26 import java.awt.image.BufferedImage;
29 * Writes a {@link BitMatrix} or {@link ByteMatrix} to {@link BufferedImage},
30 * file or stream. Provided here instead of core since it depends on
35 public final class MatrixToImageWriter {
37 private static final int BLACK = 0xFF000000;
38 private static final int WHITE = 0xFFFFFFFF;
40 private MatrixToImageWriter() {}
43 * Renders a {@link BitMatrix} as an image, where "false" bits are rendered
44 * as white, and "true" bits are rendered as black.
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);
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
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);
78 * Writes a {@link BitMatrix} to a file.
80 * @see #toBufferedImage(BitMatrix)
82 public static void writeToFile(BitMatrix matrix, String format, File file)
84 BufferedImage image = toBufferedImage(matrix);
85 ImageIO.write(image, format, file);
89 * Writes a {@link ByteMatrix} to a file.
91 * @see #toBufferedImage(ByteMatrix)
93 public static void writeToFile(ByteMatrix matrix, String format, File file)
95 BufferedImage image = toBufferedImage(matrix);
96 ImageIO.write(image, format, file);
100 * Writes a {@link BitMatrix} to a stream.
102 * @see #toBufferedImage(BitMatrix)
104 public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
106 BufferedImage image = toBufferedImage(matrix);
107 ImageIO.write(image, format, stream);
111 * Writes a {@link ByteMatrix} to a stream.
113 * @see #toBufferedImage(ByteMatrix)
115 public static void writeToStream(ByteMatrix matrix, String format, OutputStream stream)
117 BufferedImage image = toBufferedImage(matrix);
118 ImageIO.write(image, format, stream);