diff --git a/javase/pom.xml b/javase/pom.xml index ea86889230..2df73a3372 100644 --- a/javase/pom.xml +++ b/javase/pom.xml @@ -36,6 +36,11 @@ jai-imageio-core 1.3.1 + + junit + junit + test + diff --git a/javase/src/main/java/com/google/zxing/client/j2se/MatrixToImageWriter.java b/javase/src/main/java/com/google/zxing/client/j2se/MatrixToImageWriter.java index 07bdb81492..9c0c37f332 100644 --- a/javase/src/main/java/com/google/zxing/client/j2se/MatrixToImageWriter.java +++ b/javase/src/main/java/com/google/zxing/client/j2se/MatrixToImageWriter.java @@ -16,6 +16,7 @@ package com.google.zxing.client.j2se; +import com.google.zxing.common.BitArray; import com.google.zxing.common.BitMatrix; import javax.imageio.ImageIO; @@ -62,14 +63,15 @@ public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfi BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel()); int onColor = config.getPixelOnColor(); int offColor = config.getPixelOffColor(); - int[] pixels = new int[width * height]; - int index = 0; + int[] rowPixels = new int[width]; + BitArray row = new BitArray(width); for (int y = 0; y < height; y++) { + row = matrix.getRow(y, row); for (int x = 0; x < width; x++) { - pixels[index++] = matrix.get(x, y) ? onColor : offColor; + rowPixels[x] = row.get(x) ? onColor : offColor; } + image.setRGB(0, y, width, 1, rowPixels, 0, width); } - image.setRGB(0, 0, width, height, pixels, 0, width); return image; } diff --git a/javase/src/test/java/com/google/zxing/client/j2se/MatrixToImageWriterTestCase.java b/javase/src/test/java/com/google/zxing/client/j2se/MatrixToImageWriterTestCase.java new file mode 100644 index 0000000000..5fb2408861 --- /dev/null +++ b/javase/src/test/java/com/google/zxing/client/j2se/MatrixToImageWriterTestCase.java @@ -0,0 +1,83 @@ +/* + * Copyright 2017 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 com.google.zxing.common.BitMatrix; +import org.junit.Assert; +import org.junit.Test; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +public final class MatrixToImageWriterTestCase extends Assert { + + @Test + public void testBlackAndWhite() throws Exception { + doTest(new MatrixToImageConfig()); + } + + @Test + public void testColor() throws Exception { + doTest(new MatrixToImageConfig(0xFF102030, 0xFF405060)); + } + + @Test + public void testAlpha() throws Exception { + doTest(new MatrixToImageConfig(0x7F102030, 0x7F405060)); + } + + private static void doTest(MatrixToImageConfig config) throws IOException { + doTestFormat("tiff", config); + doTestFormat("png", config); + } + + private static void doTestFormat(String format, MatrixToImageConfig config) throws IOException { + int width = 2; + int height = 3; + BitMatrix matrix = new BitMatrix(width, height); + matrix.set(0, 0); + matrix.set(0, 1); + matrix.set(1, 2); + + BufferedImage newImage = null; + Path tempFile = Files.createTempFile(null, "." + format); + try { + MatrixToImageWriter.writeToPath(matrix, format, tempFile, config); + assertTrue(Files.size(tempFile) > 0); + newImage = ImageIO.read(tempFile.toFile()); + } finally { + Files.delete(tempFile); + } + + assertEquals(width, newImage.getWidth()); + assertEquals(height, newImage.getHeight()); + for (int y = 0; y < height; y++) { + for (int x = 0; x < width; x++) { + int expected = matrix.get(x, y) ? config.getPixelOnColor() : config.getPixelOffColor(); + int actual = newImage.getRGB(x, y); + assertEquals( + "At " + x + "," + y + " expected " + Integer.toHexString(expected) + + " but got " + Integer.toHexString(actual), + expected, actual); + } + } + } + +} diff --git a/zxingorg/pom.xml b/zxingorg/pom.xml index ee0aa3b7e4..05c91d0978 100644 --- a/zxingorg/pom.xml +++ b/zxingorg/pom.xml @@ -54,11 +54,6 @@ org.apache.maven.plugins maven-war-plugin - - org.eclipse.jetty - jetty-maven-plugin - 9.4.3.v20170317 -