Skip to content

Commit

Permalink
Optimize generating BufferedImage by allocating memory per row. Add t…
Browse files Browse the repository at this point in the history
…ests. Remove old jetty plugin.
  • Loading branch information
srowen committed Apr 23, 2017
1 parent c64568c commit 45f9b07
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
5 changes: 5 additions & 0 deletions javase/pom.xml
Expand Up @@ -36,6 +36,11 @@
<artifactId>jai-imageio-core</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<parent>
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
@@ -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);
}
}
}

}
5 changes: 0 additions & 5 deletions zxingorg/pom.xml
Expand Up @@ -54,11 +54,6 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.3.v20170317</version>
</plugin>
</plugins>
</build>

Expand Down

0 comments on commit 45f9b07

Please sign in to comment.