-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7837b27
commit 26e6abb
Showing
22 changed files
with
249 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,7 +78,7 @@ kotlin { | |
} | ||
} | ||
|
||
ios() | ||
// ios() | ||
|
||
sourceSets { | ||
val commonTest by getting { | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import qrcode.QRCode | ||
import qrcode.raw.QRCodeProcessor | ||
import qrcode.render.QRCodeGraphics | ||
import qrcode.shape.DefaultShapeFunction | ||
import qrcode.shape.QRCodeShapeFunction | ||
import java.awt.Color | ||
import java.awt.Polygon | ||
import java.io.FileOutputStream | ||
|
||
fun main() { | ||
// All supported platforms | ||
// ----------------------- | ||
// Squares (default) | ||
val squareQRCode = QRCode.ofSquares() // <- See Here | ||
.build("Hello, Squares!") | ||
val squarePngData = squareQRCode.render() | ||
|
||
// Circles | ||
val circleQRCode = QRCode.ofCircles() // <- See Here | ||
.build("Hello, Circles!") | ||
val circlePngData = circleQRCode.render() | ||
|
||
// Rounded Squares | ||
val roundedSquareQRCode = QRCode.ofRoundedSquares() // <- See Here | ||
.build("Hello, Rounded Squares!") | ||
val roundedSquarePngData = roundedSquareQRCode.render() | ||
|
||
// Custom Shape | ||
// WARNING: For demonstration purposes only. My phone camera couldn't read it. | ||
val customShapeQRCode = QRCode.ofCustomShape(TriangleShapeFunction()) // <- See Here | ||
.build("Hello, Triangles!") | ||
val customShapePngData = customShapeQRCode.render() | ||
|
||
// Custom Shape - JVM-specific implementation | ||
// WARNING: For demonstration purposes only. My phone camera couldn't read it. | ||
val customShapeQRCodeJVM = QRCode.ofCustomShape(JVMTriangleShapeFunction()) // <- See Here | ||
.build("Hello, Triangles... from the JVM!") | ||
val customShapePngDataJVM = customShapeQRCodeJVM.render() | ||
|
||
// --------------------------- | ||
// JVM-only code (saves the PNG Bytes to a file) | ||
FileOutputStream("examples/kotlin/example01-squares.png").write(squarePngData) | ||
FileOutputStream("examples/kotlin/example01-circles.png").write(circlePngData) | ||
FileOutputStream("examples/kotlin/example01-rounded-squares.png").write(roundedSquarePngData) | ||
FileOutputStream("examples/kotlin/example01-custom.png").write(customShapePngData) | ||
FileOutputStream("examples/kotlin/example01-custom-jvm.png").write(customShapePngDataJVM) | ||
} | ||
|
||
class TriangleShapeFunction( | ||
squareSize: Int = QRCodeProcessor.DEFAULT_CELL_SIZE, | ||
innerSpace: Int = 1, | ||
) : DefaultShapeFunction(squareSize, innerSpace) { | ||
override fun fillRect(x: Int, y: Int, width: Int, height: Int, color: Int, canvas: QRCodeGraphics) { | ||
val topCenterX = x + width / 2 | ||
val topCenterY = y | ||
val bottomLeftX = x | ||
val bottomLeftY = y + height | ||
val bottomRightX = x + width | ||
val bottomRightY = y + height | ||
|
||
// Line from top-center to bottom-left | ||
canvas.drawLine(topCenterX, topCenterY, bottomLeftX, bottomLeftY, color, 1.0) | ||
// Line from top-center to bottom-right | ||
canvas.drawLine(topCenterX, topCenterY, bottomRightX, bottomRightY, color, 1.0) | ||
// Line from bottom-left to bottom-right | ||
canvas.drawLine(bottomLeftX, bottomLeftY, bottomRightX, bottomRightY, color, 1.0) | ||
} | ||
} | ||
|
||
/** | ||
* Simple [QRCodeShapeFunction] that draws filled triangles instead of squares using AWT's Polygon + Graphics2D fill(). | ||
*/ | ||
class JVMTriangleShapeFunction( | ||
squareSize: Int = QRCodeProcessor.DEFAULT_CELL_SIZE, | ||
innerSpace: Int = 1, | ||
) : DefaultShapeFunction(squareSize, innerSpace) { | ||
override fun fillRect(x: Int, y: Int, width: Int, height: Int, color: Int, canvas: QRCodeGraphics) { | ||
val topCenterX = x + width / 2 | ||
val topCenterY = y | ||
val bottomLeftX = x | ||
val bottomLeftY = y + height | ||
val bottomRightX = x + width | ||
val bottomRightY = y + height | ||
|
||
canvas.directDraw { // Platform-specific. On the JVM, it'll receive the raw Graphics2D object to draw on. | ||
val triangle = Polygon() | ||
triangle.addPoint(topCenterX, topCenterY) | ||
triangle.addPoint(bottomLeftX, bottomLeftY) | ||
triangle.addPoint(bottomRightX, bottomRightY) | ||
triangle.addPoint(topCenterX, topCenterY) | ||
|
||
it.paint = Color(color) | ||
|
||
it.fill(triangle) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import qrcode.QRCode | ||
import qrcode.color.Colors | ||
import qrcode.color.LinearGradientColorFunction | ||
import java.io.FileOutputStream | ||
|
||
fun main() { | ||
// All supported platforms | ||
// ----------------------- | ||
// Plain color | ||
val orangeQRCode = QRCode.ofSquares() | ||
.withColor(Colors.ORANGE) // <- See Here | ||
.build("Orange") | ||
val orangePngData = orangeQRCode.render() | ||
|
||
// Changing both background and foreground | ||
val darkModeQRCode = QRCode.ofSquares() | ||
.withColor(Colors.css("#43454a")) // <- See Here | ||
.withBackgroundColor(Colors.css("#1e1f22")) // <- See Here | ||
.build("Dark Mode QRCode") | ||
val darkModePngData = darkModeQRCode.render() | ||
|
||
val gradientQRCode = QRCode.ofSquares() | ||
.withCustomColorFunction( | ||
LinearGradientColorFunction( | ||
startForegroundColor = Colors.BISQUE, | ||
endForegroundColor = Colors.BLUE, | ||
), | ||
) | ||
.build("Dark Mode QRCode") | ||
val gradientPngData = gradientQRCode.render() | ||
|
||
// --------------------------- | ||
// JVM-only code (saves the PNG Bytes to a file) | ||
FileOutputStream("examples/kotlin/example02-color.png").write(orangePngData) | ||
FileOutputStream("examples/kotlin/example02-dark-mode.png").write(darkModePngData) | ||
FileOutputStream("examples/kotlin/example02-gradient.png").write(gradientPngData) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/commonMain/kotlin/qrcode/color/LinearGradientColorFunction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package qrcode.color | ||
|
||
import qrcode.QRCode | ||
import qrcode.render.QRCodeGraphics | ||
import kotlin.js.ExperimentalJsExport | ||
import kotlin.js.JsExport | ||
import kotlin.jvm.JvmOverloads | ||
import kotlin.math.roundToInt | ||
|
||
@JsExport | ||
@OptIn(ExperimentalJsExport::class) | ||
@Suppress("MemberVisibilityCanBePrivate") | ||
class LinearGradientColorFunction @JvmOverloads constructor( | ||
val startForegroundColor: Int, | ||
val endForegroundColor: Int, | ||
val backgroundColor: Int = Colors.WHITE, | ||
var vertical: Boolean = true, | ||
) : QRCodeColorFunction { | ||
private val startComponents = Colors.getRGBA(startForegroundColor) | ||
private val endComponents = Colors.getRGBA(endForegroundColor) | ||
|
||
override fun fg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int { | ||
val pct: Double = if (vertical) { | ||
row | ||
} else { | ||
col | ||
} / qrCode.rawData.size.toDouble() | ||
|
||
val r = startComponents[0] * (1 - pct) + endComponents[0] * pct | ||
val g = startComponents[1] * (1 - pct) + endComponents[1] * pct | ||
val b = startComponents[2] * (1 - pct) + endComponents[2] * pct | ||
|
||
return Colors.rgba( | ||
r.roundToInt().coerceIn(0..255), | ||
g.roundToInt().coerceIn(0..255), | ||
b.roundToInt().coerceIn(0..255), | ||
255, | ||
) | ||
} | ||
|
||
override fun bg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = backgroundColor | ||
|
||
override fun margin(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = backgroundColor | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.