package peergos.server.tests;

import org.junit.*;
import peergos.shared.zxing.*;
import peergos.shared.zxing.common.*;
import peergos.shared.zxing.qrcode.*;

import javax.imageio.*;
import java.awt.image.*;
import java.io.*;

public class QrCodeTests {

    @Test
    public void invertable() throws Exception {
        String original = "Peergos is amazing! So many cool features!";

        QRCodeWriter writer = new QRCodeWriter();
        BitMatrix result = writer.encode(original, BarcodeFormat.QR_CODE, 512, 512);
        BufferedImage bitmap = new BufferedImage(result.getWidth(), result.getHeight(), BufferedImage.TYPE_INT_ARGB);

        for (int y = 0; y < result.getHeight(); y++) {
            for (int x = 0; x < result.getWidth(); x++) {
                if (result.get(x, y))
                    bitmap.setRGB(x, y, 0xff000000);
                else
                    bitmap.setRGB(x, y, 0xffffffff);
            }
        }
        File file = new File("qr-code.png");
        ImageIO.write(bitmap, "png", file);


        // now read back in
        BufferedImage read = ImageIO.read(file);

        int width = read.getWidth();
        int height = read.getHeight();
        int[] pixels = read.getRGB(0, 0, width, height, null, 0, width);
        // This source doesn't handle rotations or dilations
        RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels);

        BinaryBitmap readBitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        Result decoded = reader.decode(readBitmap);
        String text = decoded.getText();
        Assert.assertTrue("Round trip", text.equals(original));
    }
}

@@ -172,24 +172,48 @@
 .imageio.*;%0A
+import java.awt.geom.*;%0A
 import java.
@@ -353,16 +353,20 @@
 original
+Text
  = %22Peer
@@ -506,16 +506,20 @@
 original
+Text
 , Barcod
@@ -568,22 +568,24 @@
 edImage 
-bitmap
+original
  = new B
@@ -830,38 +830,40 @@
                 
-bitmap
+original
 .setRGB(x, y, 0x
@@ -914,22 +914,24 @@
         
-bitmap
+original
 .setRGB(
@@ -1044,14 +1044,16 @@
 ite(
-bitmap
+original
 , %22p
@@ -1065,17 +1065,16 @@
 file);%0A%0A
-%0A
         
@@ -1219,24 +1219,25 @@
 ight();%0A
+%0A
         
 int%5B%5D pi
@@ -1232,27 +1232,1318 @@
     
-int%5B%5D pixels = read
+Result decoded = decodeRGB(read);%0A        String text = decoded.getText();%0A        Assert.assertTrue(%22Round trip perfect scan%22, text.equals(originalText));%0A%0A        // Now try a rotated and dilated image%0A        int scaledHeight = (int) (height * 0.9);%0A        int scaledWidth = (int) (width * 0.9);%0A        AffineTransform transform = AffineTransform.getTranslateInstance((scaledHeight-scaledWidth)/2, (scaledWidth-scaledHeight)/2);%0A        int degrees = 10;%0A        transform.rotate(Math.toRadians(degrees), scaledWidth/2, scaledHeight/2);%0A        transform.scale(.9, .9);%0A        AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);%0A        BufferedImage transformedImage = operation.createCompatibleDestImage(original, original.getColorModel());%0A        BufferedImage transformed = operation.filter(original, transformedImage);%0A        ImageIO.write(transformed, %22png%22, new File(%22qr-code-transformed.png%22));%0A%0A        // now decode%0A        Result fromTransform = decodeRGB(transformed);%0A        Assert.assertTrue(%22Decode transformed scan%22, fromTransform.getText().equals(originalText));%0A    %7D%0A%0A    private static Result decodeRGB(BufferedImage in) throws Exception %7B%0A        int width = in.getWidth();%0A        int height = in.getHeight();%0A        int%5B%5D pixels = in
 .get
@@ -2868,32 +2868,22 @@
         
-Result decoded =
+return
  reader.
@@ -2906,113 +2906,8 @@
 p);%0A
-        String text = decoded.getText();%0A        Assert.assertTrue(%22Round trip%22, text.equals(original));%0A
     

Add test for transformed qr code decoding