我正在尝试使用 Google 的 Java zxing 库编写彩色二维码。它适用于此示例,它似乎使用了 ARGB 颜色。不幸的是,我必须在我的应用程序中使用 HTML/Hex 值作为颜色,所以我试图弄清楚如何构建或转换它。我已经构建了一个 RGB 颜色并使用 alpha 值作为其前缀。但是,虽然 RGB 值可能高达 255 - 3 位,但 MatrixToImageWriter 中的参数似乎只适用于 8 位。这意味着每种颜色只有两位数。这个“MatrixToImageConfig(-10223615,-1)”有什么样的价值?有人可以向我解释那些颜色值或给我一个如何计算 HTML 的例子吗?谢谢你!QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(createQRCodeContent, BarcodeFormat.QR_CODE, createQRCodeSize, createQRCodeSize);// ColorMatrixToImageConfig conf = new MatrixToImageConfig(-10223615,-1);BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix, conf);File qrfile = new File (targetPath);ImageIO.write(qrcode, "png", qrfile);
1 回答
德玛西亚99
TA贡献1770条经验 获得超3个赞
回答我自己的问题:
String hex = "#ffffff00";
//-16711681 in ARGB int, for example used in Google's zxing library for colored qrcodes
System.out.println(toARGB(hex));
public static int toARGB(String nm) {
Long intval = Long.decode(nm);
long i = intval.intValue();
int a = (int) ((i >> 24) & 0xFF);
int r = (int) ((i >> 16) & 0xFF);
int g = (int) ((i >> 8) & 0xFF);
int b = (int) (i & 0xFF);
return ((a & 0xFF) << 24) |
((b & 0xFF) << 16) |
((g & 0xFF) << 8) |
((r & 0xFF) << 0);
}
添加回答
举报
0/150
提交
取消