For better handling of some formats, perform rotation with help of Graphics2D
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageLuminanceSource.java
index b251eb7..2d613cc 100644 (file)
@@ -18,9 +18,8 @@ package com.google.zxing.client.j2se;
 
 import com.google.zxing.LuminanceSource;
 
+import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
-import java.awt.image.BufferedImageOp;
-import java.awt.image.AffineTransformOp;
 import java.awt.geom.AffineTransform;
 
 /**
@@ -57,6 +56,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
 
   // These methods use an integer calculation for luminance derived from:
   // <code>Y = 0.299R + 0.587G + 0.114B</code>
+  @Override
   public byte[] getRow(int y, byte[] row) {
     if (y < 0 || y >= getHeight()) {
       throw new IllegalArgumentException("Requested row is outside the image: " + y);
@@ -69,7 +69,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
     if (rgbData == null || rgbData.length < width) {
       rgbData = new int[width];
     }
-    image.getRGB(left, top + y, width, 1, rgbData, 0, image.getWidth());
+    image.getRGB(left, top + y, width, 1, rgbData, 0, width);
     for (int x = 0; x < width; x++) {
       int pixel = rgbData[x];
       int luminance = (306 * ((pixel >> 16) & 0xFF) +
@@ -80,6 +80,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
     return row;
   }
 
+  @Override
   public byte[] getMatrix() {
     int width = getWidth();
     int height = getHeight();
@@ -87,7 +88,7 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
     byte[] matrix = new byte[area];
 
     int[] rgb = new int[area];
-    image.getRGB(left, top, width, height, rgb, 0, image.getWidth());
+    image.getRGB(left, top, width, height, rgb, 0, width);
     for (int y = 0; y < height; y++) {
       int offset = y * width;
       for (int x = 0; x < width; x++) {
@@ -101,19 +102,23 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
     return matrix;
   }
 
+  @Override
   public boolean isCropSupported() {
     return true;
   }
 
+  @Override
   public LuminanceSource crop(int left, int top, int width, int height) {
-    return new BufferedImageLuminanceSource(image, left, top, width, height);
+    return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
   }
 
   // Can't run AffineTransforms on images of unknown format.
+  @Override
   public boolean isRotateSupported() {
     return image.getType() != BufferedImage.TYPE_CUSTOM;
   }
 
+  @Override
   public LuminanceSource rotateCounterClockwise() {
     if (!isRotateSupported()) {
       throw new IllegalStateException("Rotate not supported");
@@ -123,11 +128,14 @@ public final class BufferedImageLuminanceSource extends LuminanceSource {
 
     // Rotate 90 degrees counterclockwise.
     AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
-    BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
 
     // Note width/height are flipped since we are rotating 90 degrees.
     BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
-    op.filter(image, rotatedImage);
+
+    // Draw the original image into rotated, via transformation
+    Graphics2D g = rotatedImage.createGraphics();
+    g.drawImage(image, transform, null);
+    g.dispose();
 
     // Maintain the cropped region, but rotate it too.
     int width = getWidth();