Skip to content

Commit cb5ea1f

Browse files
feat(ecl-auto): Fixes and new example
1 parent ae14e04 commit cb5ea1f

32 files changed

+47
-12
lines changed

examples/kotlin/example01-circles.png

-12.3 KB
Loading
-7.17 KB
Loading

examples/kotlin/example01-custom.png

-14.1 KB
Loading
-6.5 KB
Loading

examples/kotlin/example01-squares.png

-12.3 KB
Loading

examples/kotlin/example02-color.png

521 Bytes
Loading
-13.4 KB
Loading
-15.2 KB
Loading
-29.8 KB
Loading
-6.31 KB
Loading

examples/kotlin/example03-logo.png

-6.96 KB
Loading

examples/kotlin/example04-circles.svg

+1-1
Loading

examples/kotlin/example04-rounded-squares.svg

+1-1
Loading

examples/kotlin/example04-squares.svg

+1-1
Loading

examples/kotlin/example05-colored.png

-17.2 KB
Binary file not shown.
-11.3 KB
Loading

examples/kotlin/example05-shaded.png

-54.6 KB
Binary file not shown.
21.6 KB
Loading
20.3 KB
Loading
21.3 KB
Loading
20.3 KB
Loading
20.8 KB
Loading
21.4 KB
Loading
Loading
Loading

examples/kotlin/project-banner.png

30.3 KB
Loading

examples/kotlin/project-logo.png

50.9 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import qrcode.QRCode
2+
import qrcode.raw.MaskPattern
3+
import java.io.FileOutputStream
4+
5+
fun main() {
6+
// This example shows that the same data can be encoded with many different patterns.
7+
// The default (000) is no pattern at all :)
8+
// As far as I know, this is only for aesthetics.
9+
// For more info: https://www.thonky.com/qr-code-tutorial/mask-patterns (accessed 03/2025)
10+
11+
// All supported platforms
12+
// -----------------------
13+
MaskPattern.entries.forEachIndexed { index, entry ->
14+
val number = index.toString(2).padStart(3, '0')
15+
qrcodeForMaskPattern(number, entry)
16+
}
17+
}
18+
19+
private fun qrcodeForMaskPattern(number: String, maskPattern: MaskPattern) {
20+
val qrCodeData = "Hello, Mask Pattern."
21+
22+
val maskPatternQRCode = QRCode.ofSquares()
23+
.withMaskPattern(maskPattern)
24+
.build(qrCodeData)
25+
val maskPatternPngData = maskPatternQRCode.renderToBytes()
26+
27+
FileOutputStream("examples/kotlin/example07-mask-pattern-$number.png").use { it.write(maskPatternPngData) }
28+
}

examples/kotlin/src/main/kotlin/ProjectLogo.kt

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import qrcode.QRCode
22
import qrcode.color.Colors
33
import qrcode.color.Colors.css
4+
import qrcode.raw.ErrorCorrectionLevel
45
import java.awt.Color
56
import java.awt.MultipleGradientPaint.CycleMethod.NO_CYCLE
67
import java.awt.RadialGradientPaint
@@ -17,6 +18,8 @@ fun main() {
1718
.withColor(Colors.rgba(255, 255, 255, 180))
1819
.withBackgroundColor(Colors.TRANSPARENT)
1920
.withLogo(logoBytes, 150, 150)
21+
.withInformationDensity(6)
22+
.withErrorCorrectionLevel(ErrorCorrectionLevel.VERY_HIGH)
2023
.build("https://qrcodekotlin.com")
2124

2225
// Before drawing the QRCode, draw our gradient as the background

src/commonMain/kotlin/qrcode/QRCodeBuilder.kt

+4-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,10 @@ class QRCodeBuilder @JvmOverloads constructor(
328328
shapeFunction,
329329
graphicsFactory,
330330
errorCorrectionLevel,
331-
informationDensity,
331+
when (informationDensity) {
332+
0 -> QRCodeProcessor.infoDensityForDataAndECL(data, errorCorrectionLevel)
333+
else -> informationDensity
334+
},
332335
maskPattern,
333336
beforeFn,
334337
afterFn,
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package qrcode.exception
22

3+
/**
4+
* Thrown when the Information Density parameter is not enough to hold all the data that needs to be encoded in the
5+
* QRCode.
6+
*/
37
class InsufficientInformationDensityException(
4-
val informationDensity: Int,
5-
val neededBytes: Int,
6-
val maximumBytesSupported: Int,
8+
override val message: String? = null,
79
override val cause: Throwable? = null,
8-
) : IllegalArgumentException(
9-
message = "Insufficient Information Density Parameter: $informationDensity [neededBytes=$neededBytes, maximumBytesSupported=$maximumBytesSupported] - Try increasing the parameter value or use 0 (zero) to automatically compute the least amount needed to fit the QRCode data being encoded.",
10-
cause,
11-
)
10+
) : IllegalArgumentException(message, cause)

src/commonMain/kotlin/qrcode/raw/QRCodeProcessor.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import qrcode.internals.RSBlock
2525
import qrcode.raw.QRCodeDataType.DEFAULT
2626
import qrcode.raw.QRCodeDataType.NUMBERS
2727
import qrcode.raw.QRCodeDataType.UPPER_ALPHA_NUM
28+
import qrcode.raw.QRCodeProcessor.Companion.MAXIMUM_INFO_DENSITY
2829
import qrcode.raw.QRCodeProcessor.Companion.infoDensityForDataAndECL
2930
import qrcode.render.QRCodeGraphics
3031
import qrcode.render.QRCodeGraphicsFactory
@@ -365,7 +366,8 @@ class QRCodeProcessor @JvmOverloads constructor(
365366
val totalDataCount = rsBlocks.sumOf { it.dataCount } * 8
366367

367368
if (buffer.lengthInBits > totalDataCount) {
368-
throw InsufficientInformationDensityException(type, buffer.lengthInBits, totalDataCount)
369+
val errorMessage = "Insufficient Information Density Parameter: $type [neededBits=${buffer.lengthInBits}, maximumBitsForDensityLevel=$totalDataCount] - Try increasing the Information Density parameter value or use 0 (zero) to automatically compute the least amount needed to fit the QRCode data being encoded."
370+
throw InsufficientInformationDensityException(errorMessage)
369371
}
370372

371373
if (buffer.lengthInBits + 4 <= totalDataCount) {

0 commit comments

Comments
 (0)