### NPM Installation for NodeJS Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Install the qrcode-kotlin package for your NodeJS projects using npm. ```shell npm install qrcode-kotlin@4.5.0 ``` -------------------------------- ### Generate Custom Shape QR Code (Hollow Squares) Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Allows for completely custom per-cell rendering by providing a `QRCodeShapeFunction`. This example draws each cell as an outline-only square. ```kotlin import qrcode.QRCode import qrcode.color.QRCodeColorFunction import qrcode.internals.QRCodeSquare import qrcode.render.QRCodeGraphics import qrcode.shape.DefaultShapeFunction // Draw each cell as an outline-only square (hollow) class HollowShapeFunction(squareSize: Int = 25) : DefaultShapeFunction(squareSize, innerSpace = 1) { override fun fillRect(x: Int, y: Int, width: Int, height: Int, color: Int, canvas: QRCodeGraphics) { canvas.drawRect(x, y, width, height, color, thickness = 2.0) } } val pngBytes = QRCode.ofCustomShape(HollowShapeFunction(squareSize = 25)) .build("Custom shape demo") .renderToBytes() FileOutputStream("hollow.png").use { it.write(pngBytes) } ``` -------------------------------- ### Generate and Render a Basic QRCode Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Create a QR code with custom color and size, then render it as a PNG byte array and save it to a file. ```kotlin val helloWorld = QRCode.ofSquares() .withColor(Colors.DEEP_SKY_BLUE) // Default is Colors.BLACK .withSize(10) // Default is 25 .build("Hello world!") // By default, QRCodes are rendered as PNGs. val pngBytes = helloWorld.render() FileOutputStream("hello-world.png").use { it.write(pngBytes) } ``` -------------------------------- ### Render QR Codes to Bytes or Native Image Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Shows how to render a QR code directly to a byte array in various formats (default PNG) or obtain a platform-native image canvas. The `renderToBytes()` method is used for byte output, while `render()` provides a graphics canvas. ```kotlin import qrcode.QRCode import java.awt.image.BufferedImage import java.io.FileOutputStream import javax.imageio.ImageIO val qrCode = QRCode.ofSquares() .withSize(25) .build("https://example.com") // Get raw PNG bytes val pngBytes: ByteArray = qrCode.renderToBytes() // PNG (default) val jpgBytes: ByteArray = qrCode.renderToBytes(format = "JPG") // Get the native BufferedImage (JVM only) val canvas = qrCode.render() val bufferedImage = canvas.nativeImage() as BufferedImage ImageIO.write(bufferedImage, "PNG", FileOutputStream("native.png")) // Reset and re-render (e.g. for transparent-then-opaque strategy) qrCode.reset() val pngBytes2: ByteArray = qrCode.renderToBytes() ``` -------------------------------- ### QRCode.render() and QRCode.renderToBytes() Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Renders the QR code. `render()` returns a platform-native image canvas, while `renderToBytes(format)` encodes it directly to a byte array in a specified format (defaulting to PNG). ```APIDOC ## `QRCode.render()` / `QRCode.renderToBytes()` — Rendering `render()` returns a `QRCodeGraphics` canvas (platform-native image); `renderToBytes(format)` encodes it directly to a `ByteArray` in the given image format (default `"PNG"`). ```kotlin import qrcode.QRCode import java.awt.image.BufferedImage import java.io.FileOutputStream import javax.imageio.ImageIO val qrCode = QRCode.ofSquares() .withSize(25) .build("https://example.com") // Get raw PNG bytes val pngBytes: ByteArray = qrCode.renderToBytes() // PNG (default) val jpgBytes: ByteArray = qrCode.renderToBytes(format = "JPG") // Get the native BufferedImage (JVM only) val canvas = qrCode.render() val bufferedImage = canvas.nativeImage() as BufferedImage ImageIO.write(bufferedImage, "PNG", FileOutputStream("native.png")) // Reset and re-render (e.g. for transparent-then-opaque strategy) qrCode.reset() val pngBytes2: ByteArray = qrCode.renderToBytes() ``` ``` -------------------------------- ### Initialize QRCode Builders Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Instantiate QRCode builders for different shapes like squares, circles, or rounded squares. ```kotlin val squares = QRCode.ofSquares() val circles = QRCode.ofCircles() val roundedSquares = QRCode.ofRoundedSquares() ``` -------------------------------- ### Serve QR Code as PNG HTTP Response in Spring Boot Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Generate a QR code and return it as a PNG image in an HTTP response using Spring Boot. Sets appropriate headers for file attachment. ```kotlin import org.springframework.core.io.ByteArrayResource import org.springframework.http.HttpHeaders.CONTENT_DISPOSITION import org.springframework.http.MediaType.IMAGE_PNG_VALUE import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.RestController import qrcode.QRCode import qrcode.raw.ErrorCorrectionLevel @RestController class QRCodeController { @GetMapping("/qrcode", produces = [IMAGE_PNG_VALUE]) fun generate(@RequestParam content: String): ResponseEntity { val png = QRCode.ofRoundedSquares() .withSize(20) .withErrorCorrectionLevel(ErrorCorrectionLevel.MEDIUM) .build(content) .renderToBytes() return ResponseEntity.ok() .header(CONTENT_DISPOSITION, "attachment; filename=\"qrcode.png\"") .body(ByteArrayResource(png, IMAGE_PNG_VALUE)) } } // GET /qrcode?content=https://example.com // → 200 OK, Content-Type: image/png, body: ``` -------------------------------- ### Configure Solid Foreground and Background Colors Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Sets the foreground (cell) and background colors using integer RGBA values. Use the `Colors` object for named CSS colors or helper functions. Useful for dark-mode or transparent backgrounds. ```kotlin import qrcode.QRCode import qrcode.color.Colors // Dark-mode style: light cells on a dark background val pngBytes = QRCode.ofSquares() .withColor(Colors.css("#e0e0e0")) // light grey cells .withBackgroundColor(Colors.css("#1e1f22")) // dark background .build("Dark mode QRCode") .renderToBytes() FileOutputStream("dark-mode.png").use { it.write(pngBytes) } // Transparent background (useful for overlaying on images) val transparentPngBytes = QRCode.ofSquares() .withColor(Colors.BLACK) .withBackgroundColor(Colors.TRANSPARENT) .build("Transparent background") .renderToBytes() ``` -------------------------------- ### Dynamically Fit and Resize QR Codes Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Demonstrates methods for adjusting QR code dimensions after initial creation. `fitIntoArea` recalculates cell size to fit specified dimensions, while `resize` overrides the canvas size. ```kotlin import qrcode.QRCode val qrCode = QRCode.ofSquares().build("https://example.com") // Fit into a 300×300 bounding box qrCode.fitIntoArea(300, 300) val fitted: ByteArray = qrCode.renderToBytes() // Override canvas to exactly 512px square qrCode.resize(512) val resized: ByteArray = qrCode.renderToBytes() FileOutputStream("fitted.png").use { it.write(fitted) } ``` -------------------------------- ### Generate and Render QR Code in JavaScript Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/examples/js/qrcode-example.html This snippet shows how to generate a QR code from input data and render it as an image in the browser. It includes functionality to update the QR code dynamically as the user types and to adjust the cell size. The generated image can be downloaded. ```javascript const QRCode = window['qrcode-kotlin'].io.github.g0dkar.qrcode.QRCode const qrcodeData = document.getElementById("qrcodeData") const timeEl = document.getElementById("generatedTime") const imgSizeEl = document.getElementById("imgSize") const imgEl = document.getElementById("qrcodeResult") const sizeEl = document.getElementById("qrcodeCellSize") const downloadBtn = document.getElementById("downloadBtn") const doRender = (data) => { const size = sizeEl.value const start = Date.now() const result = new QRCode(data).render(size) const finish = Date.now() const dataURL = result.toDataURL() downloadBtn.href = dataURL imgEl.src = dataURL timeEl.textContent = `${finish - start}ms` imgSizeEl.textContent = `${result.width}x${result.height}, each square is ${size}x${size}` } qrcodeData.onkeyup = (evt) => { const data = evt.target.value const prev = evt.target.$prev if (data !== prev) { if (evt.target.$timer) { clearTimeout(evt.target.$timer) } evt.target.$prev = data evt.target.$timer = setTimeout(() => { doRender(data) }, 500) } } sizeEl.onchange = () => { doRender(qrcodeData.value) } doRender(qrcodeData.value) ``` -------------------------------- ### QRCodeBuilder.withSize(), withMargin(), withCanvasSize() Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Controls the sizing of the QR code. `withSize(px)` sets the cell size, `withMargin(px)` adds a border, and `withCanvasSize(px)` fixes the total image dimensions. ```APIDOC ## `QRCodeBuilder.withSize()` / `withMargin()` / `withCanvasSize()` — Sizing controls `withSize(px)` sets the pixel size of each individual cell. `withMargin(px)` adds a uniform border around the QR code. `withCanvasSize(px)` fixes the total image size (overrides cell-based sizing). ```kotlin import qrcode.QRCode // Cell-based sizing: 15px per cell, 20px margin val pngBytes = QRCode.ofSquares() .withSize(15) .withMargin(20) .build("https://example.com") .renderToBytes() FileOutputStream("sized.png").use { it.write(pngBytes) } // Fixed canvas: fit the QRCode into a 400×400 image val fixed = QRCode.ofRoundedSquares() .withCanvasSize(400) .build("https://example.com") .renderToBytes() ``` ``` -------------------------------- ### Control QR Code Sizing and Margins Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Demonstrates how to control the size of QR code cells, add margins, and fix the total canvas size. Use `withSize` and `withMargin` for cell-based sizing, or `withCanvasSize` for a fixed image dimension. ```kotlin import qrcode.QRCode // Cell-based sizing: 15px per cell, 20px margin val pngBytes = QRCode.ofSquares() .withSize(15) .withMargin(20) .build("https://example.com") .renderToBytes() FileOutputStream("sized.png").use { it.write(pngBytes) } // Fixed canvas: fit the QRCode into a 400×400 image val fixed = QRCode.ofRoundedSquares() .withCanvasSize(400) .build("https://example.com") .renderToBytes() ``` -------------------------------- ### QRCode.fitIntoArea() and QRCode.resize() Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Dynamically adjusts the QR code's size. `fitIntoArea(width, height)` resizes the QR code to fill specified dimensions, while `resize(size)` overrides the canvas size while maintaining the current cell size. ```APIDOC ## `QRCode.fitIntoArea()` / `QRCode.resize()` — Dynamic sizing `fitIntoArea(width, height)` recomputes `squareSize` so the QR code fills the given dimensions. `resize(size)` overrides the canvas size while keeping the current cell size. ```kotlin import qrcode.QRCode val qrCode = QRCode.ofSquares().build("https://example.com") // Fit into a 300×300 bounding box qrCode.fitIntoArea(300, 300) val fitted: ByteArray = qrCode.renderToBytes() // Override canvas to exactly 512px square qrCode.resize(512) val resized: ByteArray = qrCode.renderToBytes() FileOutputStream("fitted.png").use { it.write(fitted) } ``` ``` -------------------------------- ### Generate Minimal Square QR Code Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Encode a URL and render it to PNG bytes using the default square shape. This is the recommended entry point for most use cases. ```kotlin import qrcode.QRCode import java.io.FileOutputStream // Minimal: encode a URL, get PNG bytes val pngBytes = QRCode.ofSquares() .build("https://example.com") .renderToBytes() FileOutputStream("qrcode.png").use { it.write(pngBytes) } // Output: PNG file, ~475×475 px, black squares on white background ``` -------------------------------- ### Browser Script Tag Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Include this script tag in your HTML to use QRCode-Kotlin in the browser. ```html ``` -------------------------------- ### Utilize Color Utilities Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Provides access to predefined CSS named colors, and functions to create colors from hex strings, RGBA components, or modify existing colors' alpha channel. The `Colors` object offers constants and utility methods for color manipulation. ```kotlin import qrcode.color.Colors // Named CSS constants val black: Int = Colors.BLACK // 0xFF000000 val blue: Int = Colors.DEEP_SKY_BLUE // 0xFF00BFFF val clear: Int = Colors.TRANSPARENT // 0x00000000 // Build from CSS hex string val brand: Int = Colors.css("#ff5733") // opaque orange-red // Build from RGBA components (0..255 each) val semiTransparentRed: Int = Colors.rgba(255, 0, 0, 128) // Add / change alpha on an existing color val faded: Int = Colors.withAlpha(Colors.BLUE, 80) // 80/255 ≈ 31% opacity // Decompose a color into [R, G, B, A] components val (r, g, b, a) = Colors.getRGBA(brand).let { Triple(it[0], it[1], it[2]).also { _ -> println("alpha=${it[3]}") } } ``` -------------------------------- ### Low-level QR Code Encoding with `QRCodeProcessor` Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Use `QRCodeProcessor` for direct access to raw encoded data and full control over rendering. This is the underlying engine for the high-level `QRCode` class. ```kotlin import qrcode.raw.ErrorCorrectionLevel import qrcode.raw.QRCodeProcessor val processor = QRCodeProcessor( data = "Hello QRCode!", errorCorrectionLevel = ErrorCorrectionLevel.MEDIUM, ) // Compute the minimum information density for this data + ECL val density = QRCodeProcessor.infoDensityForDataAndECL("Hello QRCode!", ErrorCorrectionLevel.MEDIUM) println("Min info density: $density") // Encode to raw grid data val rawData = processor.encode(typeNum = density) // Compute the pixel size for a given cell size val imageSize = processor.computeImageSize(cellSize = 25, rawData = rawData) println("Image will be ${imageSize}×${imageSize} px") ``` -------------------------------- ### Apply Mask Patterns to QR Codes Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Iterates through all available mask patterns to generate QR codes with different visual styles. The default pattern is PATTERN000. ```kotlin import qrcode.QRCode import qrcode.raw.MaskPattern // Generate the same QRCode with every mask pattern MaskPattern.entries.forEach { pattern -> val pngBytes = QRCode.ofSquares() .withMaskPattern(pattern) .build("Mask pattern demo") .renderToBytes() FileOutputStream("qrcode-${pattern.name}.png").use { it.write(pngBytes) } } ``` -------------------------------- ### Create Custom SVG QR Code Graphics Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Extend `QRCodeGraphicsFactory` to redirect drawing to a custom rendering target like SVG. Requires the JFree SVG library. ```kotlin // Requires: implementation("org.jfree:org.jfree.svg:5.0.5") import qrcode.QRCode import qrcode.render.QRCodeGraphicsFactory import qrcode.render.QRCodeGraphics import org.jfree.svg.SVGGraphics2D import java.io.FileOutputStream class SVGGraphicsFactory : QRCodeGraphicsFactory() { override fun newGraphics(width: Int, height: Int): QRCodeGraphics = SVGQRCodeGraphics(width, height) // custom QRCodeGraphics that wraps SVGGraphics2D } val svgBytes = QRCode.ofSquares() .withGraphicsFactory(SVGGraphicsFactory()) .build("SVG QRCode!") .renderToBytes() FileOutputStream("qrcode.svg").use { it.write(svgBytes) } ``` -------------------------------- ### Implement Custom Color Logic for QR Codes Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Allows full control over per-cell coloring by implementing the `QRCodeColorFunction` interface. The `fg()` and `bg()` methods can use cell position for dynamic color decisions. ```kotlin import qrcode.QRCode import qrcode.color.Colors import qrcode.color.QRCodeColorFunction import qrcode.render.QRCodeGraphics // Checkerboard: alternate cell color based on position parity class CheckerColorFunction : QRCodeColorFunction { override fun fg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = if ((row + col) % 2 == 0) Colors.DEEP_SKY_BLUE else Colors.DARK_VIOLET override fun bg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = Colors.WHITE } val pngBytes = QRCode.ofSquares() .withCustomColorFunction(CheckerColorFunction()) .build("Custom color function demo") .renderToBytes() FileOutputStream("checker.png").use { it.write(pngBytes) } ``` -------------------------------- ### Generate QR Code in Spring Controller Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md This controller method generates a QR code image from a given content string and returns it as a PNG image. Ensure necessary Spring dependencies are included. ```kotlin import org.springframework.core.io.ByteArrayResource import org.springframework.http.HttpHeaders.CONTENT_DISPOSITION import org.springframework.http.MediaType.IMAGE_PNG_VALUE @GetMapping("/qrcode") fun generateQrCode(content: String): ResponseEntity { val pngData = QRCode.ofSquares() .build(content) .render() val resource = ByteArrayResource(pngData, IMAGE_PNG_VALUE) return ResponseEntity.ok() .header(CONTENT_DISPOSITION, "attachment; filename=\"qrcode.png\"") .body(resource) } ``` -------------------------------- ### Configure QR Code Information Density (Type Number) Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Sets the QR code "type number" (1–40), controlling how many cells the grid has. Defaults to `0`, which auto-computes the minimum required value. Setting it manually is useful when embedding logos (bump it up to improve scanner success rate). ```kotlin import qrcode.QRCode import qrcode.raw.ErrorCorrectionLevel // Auto-computed (default): tightest possible grid for this data val auto = QRCode.ofSquares() .build("Short URL") .renderToBytes() // Manually set to 6: more cells than needed, aids scanability with a logo val manualDensity = QRCode.ofSquares() .withErrorCorrectionLevel(ErrorCorrectionLevel.HIGH) .withInformationDensity(6) .build("Short URL") .renderToBytes() // Maximum density (40): very large grid regardless of data length val maxDensity = QRCode.ofSquares() .withInformationDensity(40) .build("Short URL") .renderToBytes() ``` -------------------------------- ### Gradle Dependency for QRCode-Kotlin Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Add this dependency to your Gradle project for both Android and JVM applications. ```groovy implementation("io.github.g0dkar:qrcode-kotlin:4.5.0") ``` -------------------------------- ### Generate Circle-Shaped QR Code Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Renders QR code data cells as filled circles with customizable colors and size. Corner-finder squares remain standard squares for scanability. ```kotlin import qrcode.QRCode import qrcode.color.Colors val pngBytes = QRCode.ofCircles() .withColor(Colors.DEEP_SKY_BLUE) .withBackgroundColor(Colors.WHITE) .withSize(20) // 20px per cell .build("https://example.com") .renderToBytes() FileOutputStream("circles.png").use { it.write(pngBytes) } ``` -------------------------------- ### QRCodeColorFunction interface Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt An interface to implement for custom per-cell color logic. The `fg()` and `bg()` methods determine the dark and light cell colors, respectively, based on cell position. ```APIDOC ## `QRCodeColorFunction` — Custom color logic Implement this interface to take full control of per-cell color selection. The `fg()` method returns the dark-cell color and `bg()` returns the light-cell color, both receiving the current row/column for position-aware decisions. ```kotlin import qrcode.QRCode import qrcode.color.Colors import qrcode.color.QRCodeColorFunction import qrcode.render.QRCodeGraphics // Checkerboard: alternate cell color based on position parity class CheckerColorFunction : QRCodeColorFunction { override fun fg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = if ((row + col) % 2 == 0) Colors.DEEP_SKY_BLUE else Colors.DARK_VIOLET override fun bg(row: Int, col: Int, qrCode: QRCode, qrCodeGraphics: QRCodeGraphics): Int = Colors.WHITE } val pngBytes = QRCode.ofSquares() .withCustomColorFunction(CheckerColorFunction()) .build("Custom color function demo") .renderToBytes() FileOutputStream("checker.png").use { it.write(pngBytes) } ``` ``` -------------------------------- ### Browser CDN for JavaScript Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Include the JavaScript file from the CDN for browser-based usage. ```html ``` -------------------------------- ### Colors utility object Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Provides utility functions for color manipulation, including named CSS color constants, building colors from hex strings or RGBA components, and modifying alpha channels. ```APIDOC ## `Colors` — Color utilities A utility object exposing all CSS named colors as constants plus helpers to build colors from CSS hex strings, RGBA components, or add an alpha channel. ```kotlin import qrcode.color.Colors // Named CSS constants val black: Int = Colors.BLACK // 0xFF000000 val blue: Int = Colors.DEEP_SKY_BLUE // 0xFF00BFFF val clear: Int = Colors.TRANSPARENT // 0x00000000 // Build from CSS hex string val brand: Int = Colors.css("#ff5733") // opaque orange-red // Build from RGBA components (0..255 each) val semiTransparentRed: Int = Colors.rgba(255, 0, 0, 128) // Add / change alpha on an existing color val faded: Int = Colors.withAlpha(Colors.BLUE, 80) // 80/255 ≈ 31% opacity // Decompose a color into [R, G, B, A] components val (r, g, b, a) = Colors.getRGBA(brand).let { Triple(it[0], it[1], it[2]).also { _ -> println("alpha=${it[3]}") } } ``` ``` -------------------------------- ### QRCodeBuilder.withMaskPattern() Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Applies one of eight mask patterns to modify the appearance of the QR code cells. PATTERN000 is the default. ```APIDOC ## `QRCodeBuilder.withMaskPattern()` — Mask pattern Applies one of eight mask patterns (PATTERN000–PATTERN111) that slightly modify which cells are dark/light. Primarily aesthetic; `PATTERN000` (no mask) is the default. ```kotlin import qrcode.QRCode import qrcode.raw.MaskPattern // Generate the same QRCode with every mask pattern MaskPattern.entries.forEach { pattern -> val pngBytes = QRCode.ofSquares() .withMaskPattern(pattern) .build("Mask pattern demo") .renderToBytes() FileOutputStream("qrcode-${pattern.name}.png").use { it.write(pngBytes) } } ``` ``` -------------------------------- ### Generate Rounded Square QR Code Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Creates QR codes with cells shaped as squares with rounded corners. The radius, inner spacing, and size can be configured. ```kotlin import qrcode.QRCode import qrcode.color.Colors val pngBytes = QRCode.ofRoundedSquares() .withColor(Colors.DARK_VIOLET) .withRadius(8) // corner radius in pixels .withInnerSpacing(2) // gap between cells .withSize(25) .build("Hello, Rounded Squares!") .renderToBytes() FileOutputStream("rounded.png").use { it.write(pngBytes) } ``` -------------------------------- ### Configure Linear Gradient Colors for QR Code Cells Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Applies a `LinearGradientColorFunction` that interpolates the cell color from `startColor` to `endColor` along the vertical (default) or horizontal axis. Useful for visually distinct QR codes. ```kotlin import qrcode.QRCode import qrcode.color.Colors // Vertical gradient: bisque → blue (top to bottom) val verticalGradient = QRCode.ofSquares() .withGradientColor(Colors.BISQUE, Colors.BLUE, vertical = true) .build("Gradient QRCode") .renderToBytes() // Horizontal gradient: purple → orange (left to right) val horizontalGradient = QRCode.ofRoundedSquares() .withGradientColor(Colors.PURPLE, Colors.ORANGE, vertical = false) .withSize(20) .build("Horizontal gradient") .renderToBytes() FileOutputStream("gradient.png").use { it.write(verticalGradient) } ``` -------------------------------- ### Render QR Code in Android Jetpack Compose Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Utilize the `DrawScope.drawQRCode()` extension function to render QR codes directly onto a Compose `Canvas`. This avoids intermediate `ByteArray` creation. ```kotlin import androidx.compose.foundation.Canvas import androidx.compose.foundation.layout.aspectRatio import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import qrcode.QRCode import qrcode.render.extensions.drawQRCode @Composable fun QRCodeView(text: String, modifier: Modifier = Modifier) { val qrCode = remember(text) { QRCode.ofRoundedSquares() .withSize(20) .build(text) } Canvas(modifier = modifier.aspectRatio(1f)) { drawQRCode(qrCode) // renders at (0,0), fills the full Canvas area } } ``` -------------------------------- ### Maven Dependency for JVM Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Include this Maven dependency for JVM projects. ```xml io.github.g0dkar qrcode-kotlin-jvm 4.5.0 ``` -------------------------------- ### Embed a Logo Image in the QR Code Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Places a logo (supplied as a `ByteArray`) centered on the QR code. By default, the cells behind the logo are hidden (`clearLogoArea = true`); pass `false` to keep them visible. Higher error correction levels are recommended when embedding logos. ```kotlin import qrcode.QRCode val logoBytes: ByteArray = ClassLoader.getSystemResourceAsStream("logo.png")!!.readBytes() // Logo with cells cleared behind it (default) val withLogo = QRCode.ofCircles() .withLogo(logoBytes, width = 150, height = 150) // clearLogoArea = true by default .withErrorCorrectionLevel(qrcode.raw.ErrorCorrectionLevel.HIGH) // higher ECL improves readability with logo .build("https://example.com") .renderToBytes() // Logo rendered on top of cells (cells still visible) val withLogoOverlay = QRCode.ofCircles() .withLogo(logoBytes, width = 100, height = 100, clearLogoArea = false) .build("https://example.com") .renderToBytes() FileOutputStream("logo-qrcode.png").use { it.write(withLogo) } ``` -------------------------------- ### Maven Dependency for Android Source: https://github.com/g0dkar/qrcode-kotlin/blob/main/README.md Include this Maven dependency for Android projects. ```xml io.github.g0dkar qrcode-kotlin-android 4.5.0 ``` -------------------------------- ### Configure QR Code Error Correction Level Source: https://context7.com/g0dkar/qrcode-kotlin/llms.txt Controls how much of the QR code can be damaged or obscured while still remaining scannable. Four levels are available: `LOW`, `MEDIUM`, `HIGH`, `VERY_HIGH`. Higher levels increase the number of cells (larger image) but improve damage resilience. ```kotlin import qrcode.QRCode import qrcode.raw.ErrorCorrectionLevel val data = "Hello, world." // LOW – smallest QR code, least resilient val lowEcl = QRCode.ofSquares() .withErrorCorrectionLevel(ErrorCorrectionLevel.LOW) .build(data) .renderToBytes() // HIGH – larger QR code, tolerates ~30% damage val highEcl = QRCode.ofSquares() .withErrorCorrectionLevel(ErrorCorrectionLevel.HIGH) .build(data) .renderToBytes() // VERY_HIGH – maximum resilience (~40% damage tolerance); recommended when embedding logos val veryHighEcl = QRCode.ofSquares() .withErrorCorrectionLevel(ErrorCorrectionLevel.VERY_HIGH) .build(data) .renderToBytes() FileOutputStream("ecl-high.png").use { it.write(highEcl) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.