Skip to content
Proyectos

Acerca de

Una biblioteca pura de Kotlin para generar códigos de barras, con una implementación gráfica personalizable para Compose Multiplatform

Herramientas de desarrollo

Cómo se miden estas cifras

Crecimiento de estrellas medido

in 24h
0
in 7 days
+1
in 15 days
+4
in 1 month
Recopilando historial

Cambios netos en períodos que se solapan, no totales diarios. Los datos no disponibles se dejan en blanco. Las estrellas indican interés, no calidad.

README.md

Original

QRose

badge-Android badge-iOS badge-JVM badge-macOS badge-web

A pure-Kotlin barcode generation library with a customizable graphical implementation for Compose Multiplatform

Screenshot 2023-10-10 at 10 34 05

Why QRose?

  • Lightweight - doesn't bring any dependencies except of compose.ui.
  • Multiformat - multiple formats supported: QR, Data Matrix, Aztec, PDF417, UPC, EAN, Code 128/93/39, Codabar, ITF.
  • Multiplatform - Encoders are available for each Kotlin target. Graphical components are available for all targets supported by the Compose Multiplatform.
  • Flexible - high customization ability that is open for extension.
  • Efficient - declare and render codes synchronously right from the composition in 60+ fps.
  • Scalable - no raster bitmaps, only scalable vector graphics.

Installation

Maven Central

[versions]
qrose="<version>"

[libraries]
# For QR code painter
qrose-qr = { module = "io.github.alexzhirkevich:qrose", version.ref = "qrose" }
# For 2D matrix & stacked code painters (Data Matrix, Aztec, PDF417)
qrose-matrix = { module = "io.github.alexzhirkevich:qrose-matrix", version.ref = "qrose" }
# For single-dimension barcode painters (UPC, EAN, Code128, ...)
qrose-oned = { module = "io.github.alexzhirkevich:qrose-oned", version.ref = "qrose" }

Encoder modules do additionally support all K/native targets and Node

# For matrix encoders without Compose (optional, included in qrose-matrix)
qrose-encoder-matrix = { module = "io.github.alexzhirkevich:qrose-encoder-matrix", version.ref = "qrose" }
# For barcode encoders without Compose (optional, included in qrose-oned)
qrose-encoder-oned = { module = "io.github.alexzhirkevich:qrose-encoder-oned", version.ref = "qrose" }

Usage

Basic

You can create code right in composition using rememberQrCodePainter, rememberDataMatrixPainter, rememberAztecPainter, rememberPdf417Painter, rememberBarcodePainter. Or use QrCodePainter, DataMatrixPainter, AztecPainter, Pdf417Painter, BarcodePainter to create it outside of Compose.

Image(
    painter = rememberQrCodePainter("https://example.com"),
    contentDescription = "QR code referring to the example.com website"
)

Image(
    painter = rememberDataMatrixPainter("https://example.com"),
    contentDescription = "Data Matrix code"
)

Image(
    painter = rememberAztecPainter("https://example.com"),
    contentDescription = "Aztec code"
)

Image(
    painter = rememberPdf417Painter("https://example.com"),
    contentDescription = "PDF417 barcode"
)

Image(
    painter = rememberBarcodePainter("9780201379624", BarcodeType.EAN13),
    contentDescription = "EAN barcode for some product"
)

Design

QR codes have flexible styling options, for example:

val qrcodePainter = rememberQrCodePainter(
    data = "https://example.com",
    ballShape = QrBallShape.circle(),
    darkPixelShape = QrPixelShape.roundCorners(),
    frameShape = QrFrameShape.roundCorners(.25f),
    darkBrush = QrBrush.brush { size ->
        Brush.linearGradient(
            0f to Color.Red,
            1f to Color.Blue,
            end = Offset(size, size)
        )
    },
    frameBrush = QrBrush.solid(Color.Black),
    logoPainter = painterResource(Res.drawable.logo),
    logoPadding = QrLogoPadding.Natural(.1f),
    logoShape = QrLogoShape.circle(),
    logoSize = 0.2f,
)

Or with DSL constructor:

val logoPainter : Painter = painterResource(Res.drawable.logo)

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 { size ->
            Brush.linearGradient(
                0f to Color.Red,
                1f to Color.Blue,
                end = Offset(size, size)
            )
        }
        frame = QrBrush.solid(Color.Black)
    }
}

Customize

You can create your own shapes for each QR code part, for example:

class MyCircleBallShape : QrBallShape {
    
    override fun Path.path(size: Float, neighbors: Neighbors): Path = apply {
        addOval(Rect(0f,0f, size, size))
    }
}

Note A path here uses PathFillType.EvenOdd that cannot be changed.

Data types

QR codes can hold various payload types: Text, Wi-Fi, E-mail, vCard, etc.

QrData object can be used to perform such encodings, for example:

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

val wifiCode = rememberQrCodePainter(wifiData)

Export

QR codes can be exported to PNG, JPEG and WEBP formats using toByteArray function:


val painter : Painter = QrCodePainter(
    data = "https://example.com",
    options =  QrOptions { 
        colors {
            //...
        }
    }
)

val bytes : ByteArray = painter.toByteArray(1024, 1024, ImageFormat.PNG)

Encoders

Using qrose-encoder-matrix and qrose-encoder-oned modules you can get barcode bit matrices/arrays without Compose graphical implementation.

The QroseEncoders object is used as an encoder factory for all types of codes. You can create encoders using the extension functions, for example:

/**
 * Creates a [MatrixCodeEncoder] that generates QR codes.
 */
fun QroseEncoders.QR(
    errorCorrection: QrErrorCorrection = QrErrorCorrection.L,
    maskPattern: QrMaskPattern = QrMaskPattern.PATTERN000
) : MatrixCodeEncoder

/**
 * Creates a [BarcodeEncoder] that generates Code 128 barcodes.
 */
fun QroseEncoders.Code128(
    compact : Boolean = true,
    forceCodeSet : Code128Type? = null
) : BarcodeEncoder
val encoder : MatrixCodeEncoder = QroseEncoders.QR()
val code : Matrix2D = encoder.encode("https://example.com")

Explorar proyectos relacionados

Cargando…