pablohirafuji / elm-qrcode / QRCode

QR Code encoding and rendering.


type QRCode

QRCode type.


type ErrorCorrection
    = Low
    | Medium
    | Quartile
    | High

Error correction level. Provides the following error correction capability:

Encoding

fromString : String -> Result Error QRCode

Transform a string into a result Error or a QRCode using Quartile ErrorCorrection.

fromStringWith : ErrorCorrection -> String -> Result Error QRCode

Transform a string with a given ErrorCorrection into a result Error or a QRCode.

Rendering

toSvg : List (Svg.Attribute msg) -> QRCode -> Html msg

Transform a QRCode into a svg element.

import Svg.Attributes as SvgA

qrCodeView : String -> Html msg
qrCodeView message =
    QRCode.fromString message
        |> Result.map
            (QRCode.toSvg
                [ SvgA.width "500px"
                , SvgA.height "500px"
                ]
            )
        |> Result.withDefault
            (Html.text "Error while encoding to QRCode.")

Beware: You must set some width and height to render anything. You can set it by defining width and height svg's CSS properties or by passing the Svg.Attributes.width and Svg.Attributes.height svg attributes.

toSvgWithoutQuietZone : List (Svg.Attribute msg) -> QRCode -> Html msg

Same as toSvg, but without the quiet zone.

toImage : QRCode -> Image

Transform a QRCode into an Image. You can transform the Image into a PNG or BMP.

import Html
import Html.Attribute
import Image
import QRCode

viewQRCode : String -> Html msg
viewQRCode message =
    QRCode.fromString message
        |> Result.map
            (\qrCode ->
                Html.img
                    [ QRCode.toImage qrCode
                        |> Image.toPngUrl
                        |> Html.Attribute.src
                    ]
                    []
            )
        |> Result.withDefault
            (Html.text "Error while encoding to QRCode.")

Note: You must install the justgook/elm-image package in order to use the functions to convert the Image type to something else:

elm install "justgook/elm-image"

toImageWithOptions : ImageOptions -> QRCode -> Image

Transform a QRCode into an Image with an ImageOptions.


type alias ImageOptions =
{ moduleSize : Basics.Int
, darkColor : Basics.Int
, lightColor : Basics.Int
, quietZoneSize : Basics.Int 
}

Available options to transform a QRCode into an Image with toImageWithOptions.

defaultImageOptions : ImageOptions

Default options used by toImage.

defaultImageOptions =
    { moduleSize = 5
    , darkColor = 0xFF
    , lightColor = 0xFFFFFFFF
    , quietZoneSize = 4
    }

Extracting

toMatrix : QRCode -> List (List Basics.Bool)

Transform a QRCode into a list of list of booleans.

"Hello World!"
    |> QRCode.fromString
    |> Result.map QRCode.toMatrix

version : QRCode -> Basics.Int

Get the version of the QRCode.

"Hello World!"
    |> QRCode.fromString
    |> Result.map QRCode.version
    -- (==) Ok 1

Error


type Error
    = AlignmentPatternNotFound
    | InvalidNumericChar
    | InvalidAlphanumericChar
    | InvalidUTF8Char
    | LogTableException Basics.Int
    | PolynomialMultiplyException
    | PolynomialModException
    | InputLengthOverflow

Possible encoding errors.