Pietro Pozzoli
Pietro Pozzoli

Reputation: 33

I'm havig trouble generating QR codes dynamicaly

I have this class right now, it creates an Image with a QR-code in it, it works great. However when i use this class to create another QR-code by feeding it with a new String the first QR-code is painted... Please help! Is there a way to initialize Graphics2d, or BufferedImage?

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QRCodeGenerator {

int size = 300;
String fileType = "jpg";

static String filePath = "QR.jpg";
static File myFile = new File(filePath);
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = new BitMatrix(300, 300);
BufferedImage image;
Graphics2D graphics;

QRCodeGenerator(String myText) {

    try {
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
        byteMatrix = qrCodeWriter.encode(myText, BarcodeFormat.QR_CODE,
                size, size, hintMap);
        int width = byteMatrix.getWidth();
        image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();

        graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(ColorsClass.colorBackground);
        graphics.fillRect(0, 0, width, width);
        graphics.setColor(ColorsClass.colorForeground);

        for (int i = 0; i < width; i++) {
            for (int j = 0; j < width; j++) {
                if (byteMatrix.get(i, j)) {
                    graphics.fillRect(i, j, 1, 1);
                }
            }
        }
        ImageIO.write(image, fileType, myFile);
        System.out.println(myText);
    } catch (WriterException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Upvotes: 2

Views: 203

Answers (1)

hemal
hemal

Reputation: 51

It Seems the issue with the declaration of the field myFile or the image file which you need to write.

As mentioned in the link below. You have to explicitly close and open the image file every time

Also you can try creating a new file with different name every time want to create the 2D image like "QR_1.jpg", "QR_2.jpg" thereby reducing the possibility for overriding and loosing previous data.

http://docs.oracle.com/javase/7/docs/api/javax/imageio/ImageIO.html

Writes an image using the an arbitrary ImageWriter that supports the given format to an ImageOutputStream. The image is written to the ImageOutputStream starting at the current stream pointer, overwriting existing stream data from that point forward, if present.

This method does not close the provided ImageOutputStream after the write operation has completed; it is the responsibility of the caller to close the stream, if desired.

Upvotes: 1

Related Questions