QRose Documentation

repository·main·Indexed 19 days ago

https://github.com/alexzhirkevich/qrose

A lightweight, multiplatform library for generating QR codes and various barcode formats (UPC, EAN, Code 128, etc.) using Compose Multiplatform. It provides tools for rendering codes via rememberQrCodePainter and rememberBarcodePainter, custom design options through a DSL for shapes and colors, and support for exporting QR codes to PNG, JPEG, or WEBP formats.

Tokens
1.2K
Snippets
6
Records
6
Agent score
18%

What's inside QRose

  1. Install QRose for QR codes and barcodes

    main

    To use QRose in your Compose Multiplatform project, add the appropriate dependency to your build.gradle file. Use qrose for QR code generation and qrose-oned for single-dimension barcodes (such as UPC, EAN, Code 128, etc.).

    dependencies {
    
        // For QR codes
        implementation("io.github.alexzhirkevich:qrose:<latest_version>")
        
        // For single-dimension barcodes (UPC,EAN, Code128, ...)
        implementation("io.github.alexzhirkevich:qrose-oned:<latest_version>")
    }
  2. Generate QR codes and barcodes in Compose

    main

    You can render codes directly within a Composable function using rememberQrCodePainter for QR codes and rememberBarcodePainter for barcodes. For use outside of the Compose lifecycle, use QrCodePainter and BarcodePainter respectively.

    // QR Code
    Image(
        painter = rememberQrCodePainter("https://example.com"),
        contentDescription = "QR code referring to the example.com website"
    )
    
    // Barcode
    Image(
        painter = rememberBarcodePainter("9780201379624", BarcodeType.EAN13),
        contentDescription = "EAN barcode for some product"
    )
  3. Customize QR code design using parameters or DSL

    main

    QR codes can be highly customized using either direct parameters in rememberQrCodePainter or a DSL constructor. You can customize the ball shape, dark pixel shape, frame shape, brushes (colors), and logo properties (painter, padding, shape, and size).

    // Using DSL constructor
    val logoPainter : Painter = painterResource("logo.png")
    
    val qrcodePainter : Painter = rememberQrCodePainter("https://example.com") {
        logo {
            painter = logoPainter
            padding = QrLogoPadding.Natural(.1f)
            shape = QrLogoShape.circle()
            size = 0.2f
        }
    
        shapes {
            ball = QrBallShape.circle()
            darkPixel = QrPixelShape.roundCorners()
            frame = QrFrameShape.roundCorners(.25f)
        }
        colors {
            dark = QrBrush.brush { 
                Brush.linearGradient(0f to Color.Red, 1f to Color.Blue, end = Offset(it, it)) 
            }
            frame = QrBrush.solid(Color.Black)
        }
    }
  4. Export QR codes to image bytes

    main

    You can export a QR code to PNG, JPEG, or WEBP formats by calling toByteArray on a QrCodePainter instance. You must specify the desired width, height, and ImageFormat.

    val painter : Painter = QrCodePainter(
        data = "https://example.com",
        options =  QrOptions {
            colors {
                //...
            }
        }
    )
    
    val bytes : ByteArray = painter.toByteArray(1024, 1024, ImageFormat.PNG)
  5. Implement custom QR shapes

    main

    To create custom shapes for QR code components, implement the corresponding interface (e.g., QrBallShape). You must override the path function, which provides a Path object to draw into. Note that the path uses PathFillType.EvenOdd and this cannot be changed.

    class MyCircleBallShape : QrBallShape {
        
        override fun Path.path(size: Float, neighbors: Neighbors): Path = apply {
            addOval(Rect(0f,0f, size, size))
        }
    }
  6. Encode different data types for QR codes

    main

    Use the QrData object to encode specific payload types like Wi-Fi, E-mail, or vCard into a format suitable for QR codes.

    val wifiData : String = QrData.wifi(ssid = "My Network", psk = "12345678")
    
    val wifiCode = rememberQrCodePainter(wifiData)