如何使用Zxing.net为QR码着色

时间:2018-05-01 14:41:00

标签: .net model-view-controller qr-code zxing.net

我使用ZXing.net生成了二维码,我想为生成的二维码提供颜色。如何在MVC.net中使用ZXing.net为QR码赋予颜色?

代码如下

Use private Maven repository

请有人告诉你如何给人以色彩。

2 个答案:

答案 0 :(得分:1)

我已经创建了一个GitHub Repo“ ColorZXing.Net”,基本上,您可以生成具有单调颜色的QR代码或完全彩色的QR代码。

GitHub:https://github.com/HainanZhao/ColorZXing.Net

Fully colorful QR Code

Monotone QR Code

答案 1 :(得分:0)

好吧,如果您只想要单色(例如蓝色或任何hexadecimal值),则可以尝试这种方式

  private Bitmap generate() {
    Map<EncodeHintType, Object> hintsMap = new HashMap<>();
    hintsMap.put(EncodeHintType.CHARACTER_SET, "utf-8");
    hintsMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.Q);
    hintsMap.put(EncodeHintType.MARGIN, 2);
    int mWidth = 100;
    int mHeight = 100;
    try {
        BitMatrix bitMatrix = new QRCodeWriter().encode(finalText, BarcodeFormat.QR_CODE, mWidth, mHeight, hintsMap);
        int[] pixels = new int[mWidth * mHeight];
        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                if (bitMatrix.get(j, i)) {// True if is is Black
                    pixels[i * mWidth + j] = 0xFFFFFFFF; //White
                } else {
                    pixels[i * mWidth + j] = 0x282946; //Insert the color here. 
                }
            }
        }
        Bitmap bitmap1 = Bitmap.createBitmap(pixels, mWidth, mHeight, Bitmap.Config.ARGB_8888);
        //SaveImage(bitmap1);
        return bitmap1;
    } catch (WriterException e) {
        e.printStackTrace();
    }
    return null;
}