如何使用 HTML 颜色使用 zxing 编写二维码

我正在尝试使用 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);


// Color

MatrixToImageConfig conf = new MatrixToImageConfig(-10223615,-1);


BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix, conf);


File qrfile = new File (targetPath);


ImageIO.write(qrcode, "png", qrfile);


UYOU
浏览 94回答 1
1回答

德玛西亚99

回答我自己的问题:String hex = "#ffffff00";//-16711681 in ARGB int, for example used in Google's zxing library for colored qrcodesSystem.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) |&nbsp; &nbsp; &nbsp; &nbsp; ((b & 0xFF) << 16) |&nbsp; &nbsp; &nbsp; &nbsp; ((g & 0xFF) << 8)&nbsp; |&nbsp; &nbsp; &nbsp; &nbsp; ((r & 0xFF) << 0);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java