Shaun McCran
Shaun McCran

Reputation: 208

Creating colored QR codes using zxing

I am using the google open source java zxing creator: http://code.google.com/p/zxing/

to create QR codes. I have everything in place and working (I'm loading the java files using coldfusion and writing the image to the browser.)

What I want now is to change the black QR colour to something else. Is there an easy way of doing this?

Would I need to edit a decompiled version of the encoder java file? Or is there a way I could add a color argument to the encoding routine?

Thanks Shaun

Upvotes: 3

Views: 14354

Answers (4)

Hemant Nagpal
Hemant Nagpal

Reputation: 624

I hope you are using this code to print your qr code image :

    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

You can specify your own config file (MatrixToImageConfig) and pass it along to MatrixToImageWriter like this :

    QRCodeWriter qrCodeWriter = new QRCodeWriter();
    BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
    int onColor = 0xFF000000;
    int offColor = 0xFFFFFFFF;
    MatrixToImageConfig matrixToImageConfig = new MatrixToImageConfig(onColor, offColor);
    Path path = FileSystems.getDefault().getPath(filePath);
    MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path, matrixToImageConfig);

You can pass your colors in onColor and offColor, which are integers. Append your color code to "0xFF" in onColor and offColor variable and you are good to go. Cheers.

Upvotes: 0

savepopulation
savepopulation

Reputation: 11921

I assume that you're generating qr code like below:

QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(code,null,
                                                Contents.Type.TEXT,
                                                BarcodeFormat.QR_CODE.toString(),            
                                                yourDimension);

Your output's dimension will be based on your code. Set your dimension as possible as low. So you can find exact positions of qr code place you want to change color.

Than get your pixels from bitmap with:

int[] allpixels = new int[bitmap.getHeight() * bitmap.getWidth()];
bitmap.getPixels(allpixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

And color pixels you want:

for (int i = 6; i < 9; i++) {
     allpixels[i] = Color.Red; // your rgb color
      }

Convert dp to px for every devices:

qrCodeDimension = dpToPx((int) getResources().getDimension(R.dimen.qr_dimen));

public static int dpToPx(int dp) {
      return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
   }

Finaly set your colored pixels to bitmap:

bitmap.setPixels(allpixels, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

return Bitmap.createScaledBitmap(bitmap, qrCodeDimension, qrCodeDimension, false);

This's how i solved the problem. I hope this'll help you.

Upvotes: 1

Lovnish Chhabra
Lovnish Chhabra

Reputation: 41

Try this ::

BitMatrix matrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, this.width, this.height, getEncodeHints());
/*
Here the config object represents the QR Code colors.
i.e. Brown and White respectively
*/ 
MatrixToImageConfig conf = new MatrixToImageConfig(-10223615,-1);
BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(matrix, conf);

Upvotes: 4

Sean Owen
Sean Owen

Reputation: 66886

In MatrixToImageWriter.java (which I assume you are using), under javase/ change the constant BLACK. It is an int in ARGB format and currently has value 0xFF000000. Leave the alpha value at 0xFF. Change the rest to describe your color in hex format. You can do the same with WHITE if you like.

Upvotes: 7

Related Questions