Skip to content

Instantly share code, notes, and snippets.

@minoki
Created May 18, 2015 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minoki/054265a32884ce7d9e6e to your computer and use it in GitHub Desktop.
Save minoki/054265a32884ce7d9e6e to your computer and use it in GitHub Desktop.
An example program using haskell-qrencode library.
import qualified Data.QRCode as Q -- from `haskell-qrencode' package
import qualified Codec.Picture as P -- from `JuicyPixels' package
import qualified Codec.Picture.Png as P
import qualified Data.ByteString.Lazy as B
pixelPerCell = 5
main :: IO ()
main = do l <- getLine
let version = Nothing -- auto
errorLevel = Q.QR_ECLEVEL_M
mode = Q.QR_MODE_EIGHT
qr <- Q.encodeString l version errorLevel mode True
let width = Q.getQRCodeWidth qr
mat = Q.toMatrix qr
pixelAt x y = let x' = x `div` pixelPerCell
y' = y `div` pixelPerCell
i = y'
j = x'
in if mat!!i!!j == 0 then maxBound else minBound :: P.Pixel8
image = P.generateImage pixelAt (width*pixelPerCell) (width*pixelPerCell)
encodedImage = P.encodePng image
B.writeFile "out.png" encodedImage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment