使用 zxing 创建彩色二维码

发布于 2024-10-12 17:04:43 字数 319 浏览 4 评论 0原文

我正在使用 google 开源 java zxing 创建者:http://code.google.com/p/zxing /

创建二维码。我已经准备好了一切并正在工作(我正在使用 Coldfusion 加载 java 文件并将图像写入浏览器。)

我现在想要的是将黑色 QR 颜色更改为其他颜色。有一个简单的方法可以做到这一点吗?

我需要编辑编码器 java 文件的反编译版本吗?或者有什么方法可以将颜色参数添加到编码例程中?

谢谢 肖恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放赐 2024-10-19 17:04:43

MatrixToImageWriter.java(我假设您正在使用)中,在javase/下更改常量BLACK。它是 ARGB 格式的 int,当前值为 0xFF000000。将 alpha 值保留为 0xFF。更改其余部分以十六进制格式描述您的颜色。如果您愿意,您可以对 WHITE 执行相同的操作。

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.

卖梦商人 2024-10-19 17:04:43

试试这个::

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);

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);
荭秂 2024-10-19 17:04:43

我假设您正在生成如下所示的二维码:

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

您的输出尺寸将基于您的代码。将尺寸设置得尽可能低。这样你就可以找到你想要改变颜色的二维码的确切位置。

比从位图中获取像素:

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

以及您想要的颜色像素:

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

将每个设备的 dp 转换为 px:

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

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

最后将彩色像素设置为位图:

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

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

这就是我解决问题的方法。我希望这会帮助你。

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.

感情旳空白 2024-10-19 17:04:43

我希望您使用此代码来打印您的二维码图像:

    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);

您可以指定您自己的配置文件(MatrixToImageConfig)并将其传递给 MatrixToImageWriter,如下所示:

    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);

您可以在 onColor 和 offColor 中传递颜色,它们是整数。将颜色代码附加到 onColor 和 offColor 变量中的“0xFF”,然后就可以开始了。
干杯。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文