Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Issue 1146 add custom colors

git-svn-id: http://zxing.googlecode.com/svn/trunk@2169 59b500cc-1b3d-0410-9834-0bbf25fbcc57
  • Loading branch information...
commit 4b7494400250fe1f3cd3d6c57df4dc98153210e5 1 parent a8cd1d0
srowen authored
View
55 javase/src/com/google/zxing/client/j2se/MatrixToImageConfig.java
@@ -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;
+ }
+
+}
View
40 javase/src/com/google/zxing/client/j2se/MatrixToImageWriter.java
@@ -33,8 +33,7 @@
*/
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 @@ private 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 static BufferedImage toBufferedImage(BitMatrix matrix) {
*
* @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 static void writeToFile(BitMatrix matrix, String format, File file)
*
* @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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.