mirror of
https://gitee.com/openharmony/third_party_qrcodegen
synced 2024-11-23 07:10:22 +00:00
In Java version, added parameters for custom module colors when rendering to BufferedImage, changed some demo code to use non-black/white colors.
This commit is contained in:
parent
87db45ef9c
commit
bfd18b2fb8
@ -132,11 +132,11 @@ public final class QrCodeGeneratorDemo {
|
||||
// Illustration "Madoka": kanji, kana, Cyrillic, full-width Latin, Greek characters
|
||||
String madoka = "「魔法少女まどか☆マギカ」って、 ИАИ desu κα?";
|
||||
qr = QrCode.encodeText(madoka, QrCode.Ecc.LOW);
|
||||
writePng(toImage(qr, 9, 4), "madoka-utf8-QR.png");
|
||||
writePng(toImage(qr, 9, 4, 0xFFFFE0, 0x303080), "madoka-utf8-QR.png");
|
||||
|
||||
segs = Arrays.asList(QrSegmentAdvanced.makeKanji(madoka));
|
||||
qr = QrCode.encodeSegments(segs, QrCode.Ecc.LOW);
|
||||
writePng(toImage(qr, 9, 4), "madoka-kanji-QR.png");
|
||||
writePng(toImage(qr, 9, 4, 0xE0F0FF, 0x404040), "madoka-kanji-QR.png");
|
||||
}
|
||||
|
||||
|
||||
@ -148,9 +148,9 @@ public final class QrCodeGeneratorDemo {
|
||||
// Project Nayuki URL
|
||||
segs = QrSegment.makeSegments("https://www.nayuki.io/");
|
||||
qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, -1, true); // Automatic mask
|
||||
writePng(toImage(qr, 8, 6), "project-nayuki-automask-QR.png");
|
||||
writePng(toImage(qr, 8, 6, 0xE0FFE0, 0x206020), "project-nayuki-automask-QR.png");
|
||||
qr = QrCode.encodeSegments(segs, QrCode.Ecc.HIGH, QrCode.MIN_VERSION, QrCode.MAX_VERSION, 3, true); // Force mask 3
|
||||
writePng(toImage(qr, 8, 6), "project-nayuki-mask3-QR.png");
|
||||
writePng(toImage(qr, 8, 6, 0xFFE0E0, 0x602020), "project-nayuki-mask3-QR.png");
|
||||
|
||||
// Chinese text as UTF-8
|
||||
segs = QrSegment.makeSegments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫");
|
||||
@ -168,20 +168,27 @@ public final class QrCodeGeneratorDemo {
|
||||
|
||||
/*---- Utilities ----*/
|
||||
|
||||
private static BufferedImage toImage(QrCode qr, int scale, int border) {
|
||||
return toImage(qr, scale, border, 0xFFFFFF, 0x000000);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a raster image depicting the specified QR Code, with the specified module scale and border modules.
|
||||
* <p>For example, toImage(qr, scale=10, border=4) means to pad the QR Code with 4 light
|
||||
* border modules on all four sides, and use 10×10 pixels to represent each module.
|
||||
* The resulting image only contains the hex colors 000000 and FFFFFF.
|
||||
* Returns a raster image depicting the specified QR Code, with
|
||||
* the specified module scale, border modules, and module colors.
|
||||
* <p>For example, scale=10 and border=4 means to pad the QR Code with 4 light border
|
||||
* modules on all four sides, and use 10×10 pixels to represent each module.
|
||||
* @param qr the QR Code to render (not {@code null})
|
||||
* @param scale the side length (measured in pixels, must be positive) of each module
|
||||
* @param border the number of border modules to add, which must be non-negative
|
||||
* @param lightColor the color to use for light modules, in 0xRRGGBB format
|
||||
* @param darkColor the color to use for dark modules, in 0xRRGGBB format
|
||||
* @return a new image representing the QR Code, with padding and scaling
|
||||
* @throws NullPointerException if the QR Code is {@code null}
|
||||
* @throws IllegalArgumentException if the scale or border is out of range, or if
|
||||
* {scale, border, size} cause the image dimensions to exceed Integer.MAX_VALUE
|
||||
*/
|
||||
private static BufferedImage toImage(QrCode qr, int scale, int border) {
|
||||
private static BufferedImage toImage(QrCode qr, int scale, int border, int lightColor, int darkColor) {
|
||||
Objects.requireNonNull(qr);
|
||||
if (scale <= 0 || border < 0)
|
||||
throw new IllegalArgumentException("Value out of range");
|
||||
@ -192,7 +199,7 @@ public final class QrCodeGeneratorDemo {
|
||||
for (int y = 0; y < result.getHeight(); y++) {
|
||||
for (int x = 0; x < result.getWidth(); x++) {
|
||||
boolean color = qr.getModule(x / scale - border, y / scale - border);
|
||||
result.setRGB(x, y, color ? 0x000000 : 0xFFFFFF);
|
||||
result.setRGB(x, y, color ? darkColor : lightColor);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
Loading…
Reference in New Issue
Block a user