Skip to content

Commit 8901542

Browse files
committed
Changed how renderShaded() works to have a better Java Interop
1 parent 572b56d commit 8901542

11 files changed

+82
-41
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ and customizable way to create QRCodes into the JVM domain, especially in the ba
2424

2525
## Installation
2626

27-
This library is available from [Maven Central](https://search.maven.org/artifact/io.github.g0dkar/qrcode-kotlin/1.0.3/qrcode-kotlin)
27+
This library is available from [Maven Central](https://search.maven.org/artifact/io.github.g0dkar/qrcode-kotlin/1.1.0/qrcode-kotlin)
2828
so you can add `QRCode-Kotlin` to your project as a dependency like any other:
2929

3030
**If you're using Maven - pom.xml:**
@@ -33,18 +33,18 @@ so you can add `QRCode-Kotlin` to your project as a dependency like any other:
3333
<dependency>
3434
<groupId>io.github.g0dkar</groupId>
3535
<artifactId>qrcode-kotlin</artifactId>
36-
<version>1.0.3</version>
36+
<version>1.1.0</version>
3737
</dependency>
3838
```
3939

4040
**If you're using Gradle:**
4141

4242
```groovy
4343
// Kotlin ❤️
44-
implementation("io.github.g0dkar:qrcode-kotlin:1.0.3")
44+
implementation("io.github.g0dkar:qrcode-kotlin:1.1.0")
4545
4646
// Groovy
47-
implementation 'io.github.g0dkar:qrcode-kotlin:1.0.3'
47+
implementation 'io.github.g0dkar:qrcode-kotlin:1.1.0'
4848
```
4949

5050
## Examples and Usage
@@ -217,7 +217,7 @@ personalizável de se criar QRCodes para o domínio da JVM, especialmente no bac
217217

218218
## Instalação
219219

220-
Esta biblioteca está disponível a partir da [Central Maven](https://search.maven.org/artifact/io.github.g0dkar/qrcode-kotlin/1.0.3/qrcode-kotlin),
220+
Esta biblioteca está disponível a partir da [Central Maven](https://search.maven.org/artifact/io.github.g0dkar/qrcode-kotlin/1.1.0/qrcode-kotlin),
221221
então basta adicionar o `QRCode-Kotlin` a seu projeto como qualquer outra dependência:
222222

223223
**Se você utiliza Maven - pom.xml:**
@@ -226,18 +226,18 @@ então basta adicionar o `QRCode-Kotlin` a seu projeto como qualquer outra depen
226226
<dependency>
227227
<groupId>io.github.g0dkar</groupId>
228228
<artifactId>qrcode-kotlin</artifactId>
229-
<version>1.0.3</version>
229+
<version>1.1.0</version>
230230
</dependency>
231231
```
232232

233233
**Se você utiliza Gradle:**
234234

235235
```groovy
236236
// Kotlin ❤️
237-
implementation("io.github.g0dkar:qrcode-kotlin:1.0.3")
237+
implementation("io.github.g0dkar:qrcode-kotlin:1.1.0")
238238
239239
// Groovy
240-
implementation 'io.github.g0dkar:qrcode-kotlin:1.0.3'
240+
implementation 'io.github.g0dkar:qrcode-kotlin:1.1.0'
241241
```
242242

243243
## Exemplos e Usos

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ plugins {
1111
}
1212

1313
group = "io.github.g0dkar"
14-
version = "1.0.3"
14+
version = "1.1.0"
1515
java.sourceCompatibility = JavaVersion.VERSION_1_8
1616
java.targetCompatibility = JavaVersion.VERSION_1_8
1717

examples/java/build.gradle.kts

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,5 @@ repositories {
77
}
88

99
dependencies {
10-
implementation("io.github.g0dkar:qrcode-kotlin:1.0.1")
11-
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.0")
10+
implementation("io.github.g0dkar:qrcode-kotlin:1.1.0")
1211
}

examples/java/src/main/java/GradientColoredQRCode.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,21 @@ public void createQRCode(
3030
Color startColor,
3131
Color endColor
3232
) throws IOException {
33-
BufferedImage imageData = new QRCode(content).renderShaded((image, x, y, cellData) -> {
34-
if (cellData != null) {
35-
if (Boolean.TRUE.equals(cellData.getFirst())) {
36-
double topBottomPct = pct(x, y, image.getWidth(), image.getHeight());
33+
BufferedImage imageData = new QRCode(content).renderShaded(pixelData -> {
34+
if (!pixelData.isMargin()) {
35+
if (pixelData.isDark()) {
36+
double topBottomPct = pct(pixelData.getX(), pixelData.getY(), pixelData.getImage().getWidth(), pixelData.getImage().getHeight());
3737
double bottomTopPct = 1 - topBottomPct;
3838
return new Color(
3939
(int) (startColor.getRed() * topBottomPct + endColor.getRed() * bottomTopPct),
4040
(int) (startColor.getGreen() * topBottomPct + endColor.getGreen() * bottomTopPct),
4141
(int) (startColor.getBlue() * topBottomPct + endColor.getBlue() * bottomTopPct)
42-
).getRGB();
42+
);
4343
} else {
44-
return Color.white.getRGB();
44+
return Color.white;
4545
}
4646
} else {
47-
return Color.white.getRGB();
47+
return Color.white;
4848
}
4949
});
5050
ImageIO.write(imageData, "PNG", new File("java-gradient.png"));

examples/java/src/main/java/RandomColoredQRCode.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,19 @@ public void createQRCode(
1818
Color backgroundColor
1919
) throws IOException {
2020
HashMap<Point, Color> colorMap = new HashMap<>();
21-
BufferedImage imageData = new QRCode(content).renderShaded((image, x, y, cellData) -> {
22-
if (cellData != null) {
23-
Integer row = cellData.getSecond();
24-
Integer col = cellData.getThird();
25-
Point point = new Point(row, col);
21+
BufferedImage imageData = new QRCode(content).renderShaded(pixelData -> {
22+
if (!pixelData.isMargin()) {
23+
Point point = new Point(pixelData.getRow(), pixelData.getCol());
2624

27-
if (Boolean.TRUE.equals(cellData.getFirst())) {
28-
return colorMap.computeIfAbsent(point, p -> colors.get(new Random().nextInt(colors.size()))).getRGB();
25+
if (pixelData.isDark()) {
26+
return colorMap.computeIfAbsent(point, p -> colors.get(new Random().nextInt(colors.size())));
2927
}
3028
else {
31-
return backgroundColor.getRGB();
29+
return backgroundColor;
3230
}
3331
}
3432
else {
35-
return backgroundColor.getRGB();
33+
return backgroundColor;
3634
}
3735
});
3836
ImageIO.write(imageData, "PNG", new File("java-random-colored.png"));

examples/kotlin-zxing-hello-world.png

2.31 KB
Loading

examples/kotlin/build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ repositories {
77
}
88

99
dependencies {
10-
implementation("io.github.g0dkar:qrcode-kotlin:1.0.1")
10+
implementation("io.github.g0dkar:qrcode-kotlin:1.1.0")
1111
}

examples/kotlin/src/main/kotlin/GradientColorQRCode.kt

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ class GradientColorQRCode(
1818
startColor: Color,
1919
endColor: Color,
2020
) {
21-
val imageData = QRCode(content).renderShaded { image, x, y, cellData ->
22-
cellData?.let { (isDark, _, _) ->
23-
if (isDark) {
24-
val topBottomPct = pct(x, y, image.width, image.height)
21+
val imageData = QRCode(content).renderShaded { pixelData ->
22+
if (!pixelData.isMargin) {
23+
if (pixelData.isDark) {
24+
val topBottomPct = pct(pixelData.x, pixelData.y, pixelData.image.width, pixelData.image.height)
2525
val bottomTopPct = 1 - topBottomPct
2626
Color(
2727
(startColor.red * topBottomPct + endColor.red * bottomTopPct).toInt(),
2828
(startColor.green * topBottomPct + endColor.green * bottomTopPct).toInt(),
2929
(startColor.blue * topBottomPct + endColor.blue * bottomTopPct).toInt()
30-
).rgb
30+
)
3131
} else {
32-
Color.white.rgb
32+
Color.white
3333
}
34+
} else {
35+
Color.white
3436
}
35-
?: Color.white.rgb
3637
}
3738

3839
ImageIO.write(imageData, "PNG", File("kotlin-gradient.png"))

examples/kotlin/src/main/kotlin/RandomColoredQRCode.kt

+13-6
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class RandomColoredQRCode {
1212
backgroundColor: Color
1313
) {
1414
val colorMap = mutableMapOf<Point, Color>()
15-
val imageData = QRCode(content).renderShaded { _, _, _, cellData ->
16-
cellData?.let { (isDark, row, col) ->
17-
if (isDark) {
18-
colorMap.computeIfAbsent(Point(row, col)) { colors[Random().nextInt(colors.size)] }.rgb
15+
val imageData = QRCode(content).renderShaded { pixelData ->
16+
if (pixelData.isMargin) {
17+
if (pixelData.isDark) {
18+
colorMap.computeIfAbsent(
19+
Point(
20+
pixelData.row,
21+
pixelData.col
22+
)
23+
) { colors[Random().nextInt(colors.size)] }
1924
} else {
20-
backgroundColor.rgb
25+
backgroundColor
2126
}
22-
} ?: backgroundColor.rgb
27+
} else {
28+
backgroundColor
29+
}
2330
}
2431
ImageIO.write(imageData, "PNG", File("kotlin-random-colored.png"))
2532
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import io.github.g0dkar.qrcode.ErrorCorrectionLevel
2+
import io.github.g0dkar.qrcode.MaskPattern
3+
import io.github.g0dkar.qrcode.QRCode
4+
import java.io.File
5+
import javax.imageio.ImageIO
6+
7+
/**
8+
* Tries to generate a QRCode as close as possible to the one generated by the following URL:
9+
*
10+
* [https://zxing.org/w/chart?cht=qr&chs=350x350&chld=M&choe=UTF-8&chl=Hello%2C+world!](https://zxing.org/w/chart?cht=qr&chs=350x350&chld=M&choe=UTF-8&chl=Hello%2C+world!)
11+
*
12+
* Which is a 350x350, Medium ECL, UTF-8 QRCode which encodes the string "Hello, world!"
13+
*
14+
* >**Disclaimer:** The resulting QRCode will likely be visually different, but contain the same data. You can check it
15+
* >by using a QRCode reader, or even Google's ZXing Own Online Decoder, available at
16+
* >[https://zxing.org/w/decode.jspx](https://zxing.org/w/decode.jspx)
17+
*/
18+
class ZXingQRCode {
19+
/**
20+
* The same result would be achieved by calling `QRCode(content).render(cellSize, margin)`, but we do it explicitly
21+
* to show all the steps of how you can fine-tune your QRCode.
22+
*/
23+
fun createQRCode(content: String) {
24+
val cellSize = 12
25+
val margin = 49
26+
val qrCode = QRCode(content, ErrorCorrectionLevel.M)
27+
val qrCodeData = qrCode.encode(type = 1, maskPattern = MaskPattern.PATTERN000)
28+
val imageData = qrCode.render(cellSize, margin, qrCodeData)
29+
ImageIO.write(imageData, "PNG", File("kotlin-zxing-hello-world.png"))
30+
}
31+
}
32+
33+
fun main() {
34+
ZXingQRCode()
35+
.createQRCode("Hello, world!")
36+
}

examples/zxing-generated-qrcode.png

342 Bytes
Loading

0 commit comments

Comments
 (0)