Creating colored QR codes using zxing

2019-04-14 04:38发布

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

3条回答
Anthone
2楼-- · 2019-04-14 05:05

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);
查看更多
三岁会撩人
3楼-- · 2019-04-14 05:06

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.

查看更多
▲ chillily
4楼-- · 2019-04-14 05:11

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.

查看更多
登录 后发表回答