mirror of
https://github.com/zxing/zxing.git
synced 2025-01-12 11:47:26 -08:00
Issue 1146 add custom colors
git-svn-id: https://zxing.googlecode.com/svn/trunk@2169 59b500cc-1b3d-0410-9834-0bbf25fbcc57
This commit is contained in:
parent
cae8f40d5c
commit
0b0a7cf3b7
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Copyright 2012 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;
|
||||
|
||||
/**
|
||||
* Encapsulates custom configuration used in methods of {@link MatrixToImageWriter}.
|
||||
*/
|
||||
public final class MatrixToImageConfig {
|
||||
|
||||
public static final int BLACK = 0xFF000000;
|
||||
public static final int WHITE = 0xFFFFFFFF;
|
||||
|
||||
private final int onColor;
|
||||
private final int offColor;
|
||||
|
||||
/**
|
||||
* Creates a default config with on color {@link #BLACK} and off color {@link #WHITE}, generating normal
|
||||
* black-on-white barcodes.
|
||||
*/
|
||||
public MatrixToImageConfig() {
|
||||
this(BLACK, WHITE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param onColor pixel on color, specified as an ARGB value as an int
|
||||
* @param offColor pixel off color, specified as an ARGB value as an int
|
||||
*/
|
||||
public MatrixToImageConfig(int onColor, int offColor) {
|
||||
this.onColor = onColor;
|
||||
this.offColor = offColor;
|
||||
}
|
||||
|
||||
public int getPixelOnColor() {
|
||||
return onColor;
|
||||
}
|
||||
|
||||
public int getPixelOffColor() {
|
||||
return offColor;
|
||||
}
|
||||
|
||||
}
|
|
@ -33,8 +33,7 @@ import java.awt.image.BufferedImage;
|
|||
*/
|
||||
public final class MatrixToImageWriter {
|
||||
|
||||
private static final int BLACK = 0xFF000000;
|
||||
private static final int WHITE = 0xFFFFFFFF;
|
||||
private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();
|
||||
|
||||
private MatrixToImageWriter() {}
|
||||
|
||||
|
@ -43,12 +42,21 @@ public final class MatrixToImageWriter {
|
|||
* as white, and "true" bits are rendered as black.
|
||||
*/
|
||||
public static BufferedImage toBufferedImage(BitMatrix matrix) {
|
||||
return toBufferedImage(matrix, DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #toBufferedImage(BitMatrix)}, but allows customization of the output.
|
||||
*/
|
||||
public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config) {
|
||||
int width = matrix.getWidth();
|
||||
int height = matrix.getHeight();
|
||||
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||
int onColor = config.getPixelOnColor();
|
||||
int offColor = config.getPixelOffColor();
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
|
||||
image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
|
||||
}
|
||||
}
|
||||
return image;
|
||||
|
@ -59,9 +67,16 @@ public final class MatrixToImageWriter {
|
|||
*
|
||||
* @see #toBufferedImage(BitMatrix)
|
||||
*/
|
||||
public static void writeToFile(BitMatrix matrix, String format, File file)
|
||||
throws IOException {
|
||||
BufferedImage image = toBufferedImage(matrix);
|
||||
public static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
|
||||
writeToFile(matrix, format, file, DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #writeToFile(BitMatrix, String, File)}, but allows customization of the output.
|
||||
*/
|
||||
public static void writeToFile(BitMatrix matrix, String format, File file, MatrixToImageConfig config)
|
||||
throws IOException {
|
||||
BufferedImage image = toBufferedImage(matrix, config);
|
||||
if (!ImageIO.write(image, format, file)) {
|
||||
throw new IOException("Could not write an image of format " + format + " to " + file);
|
||||
}
|
||||
|
@ -72,9 +87,16 @@ public final class MatrixToImageWriter {
|
|||
*
|
||||
* @see #toBufferedImage(BitMatrix)
|
||||
*/
|
||||
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
|
||||
throws IOException {
|
||||
BufferedImage image = toBufferedImage(matrix);
|
||||
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
|
||||
writeToStream(matrix, format, stream, DEFAULT_CONFIG);
|
||||
}
|
||||
|
||||
/**
|
||||
* As {@link #writeToStream(BitMatrix, String, OutputStream)}, but allows customization of the output.
|
||||
*/
|
||||
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream, MatrixToImageConfig config)
|
||||
throws IOException {
|
||||
BufferedImage image = toBufferedImage(matrix, config);
|
||||
if (!ImageIO.write(image, format, stream)) {
|
||||
throw new IOException("Could not write an image of format " + format);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue