我正在嘗試使用 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
提交
取消