mirror of
https://github.com/octoleo/plantuml.git
synced 2024-06-07 10:50:53 +00:00
81 lines
2.3 KiB
Java
81 lines
2.3 KiB
Java
/*
|
|
* Copyright 2009 ZXing authors
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.google.zxing.client.j2se;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.io.OutputStream;
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
import com.google.zxing.common.BitMatrix;
|
|
|
|
/**
|
|
* Writes a {@link BitMatrix} to {@link BufferedImage},
|
|
* file or stream. Provided here instead of core since it depends on
|
|
* Java SE libraries.
|
|
*
|
|
* @author Sean Owen
|
|
*/
|
|
public final class MatrixToImageWriter {
|
|
|
|
private static final int BLACK = 0xFF000000;
|
|
private static final int WHITE = 0xFFFFFFFF;
|
|
|
|
private MatrixToImageWriter() {}
|
|
|
|
/**
|
|
* Renders a {@link BitMatrix} as an image, where "false" bits are rendered
|
|
* as white, and "true" bits are rendered as black.
|
|
*/
|
|
public static BufferedImage toBufferedImage(BitMatrix matrix) {
|
|
int width = matrix.getWidth();
|
|
int height = matrix.getHeight();
|
|
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
for (int x = 0; x < width; x++) {
|
|
for (int y = 0; y < height; y++) {
|
|
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
|
|
}
|
|
}
|
|
return image;
|
|
}
|
|
|
|
/**
|
|
* Writes a {@link BitMatrix} to a file.
|
|
*
|
|
* @see #toBufferedImage(BitMatrix)
|
|
*/
|
|
public static void writeToFile(BitMatrix matrix, String format, File file)
|
|
throws IOException {
|
|
BufferedImage image = toBufferedImage(matrix);
|
|
ImageIO.write(image, format, file);
|
|
}
|
|
|
|
/**
|
|
* Writes a {@link BitMatrix} to a stream.
|
|
*
|
|
* @see #toBufferedImage(BitMatrix)
|
|
*/
|
|
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
|
|
throws IOException {
|
|
BufferedImage image = toBufferedImage(matrix);
|
|
ImageIO.write(image, format, stream);
|
|
}
|
|
|
|
}
|