Skip to content

Instantly share code, notes, and snippets.

@minoki
Created May 18, 2015 09:18
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/7d6a610fe03fd84122d5 to your computer and use it in GitHub Desktop.
Save minoki/7d6a610fe03fd84122d5 to your computer and use it in GitHub Desktop.
An example program using haskell-qrcode library.
import qualified Codec.Binary.QRCode as Q -- from `qrcode' package
import qualified Codec.Picture as P -- from `JuicyPixel' package
import qualified Codec.Picture.Png as P
import qualified Data.ByteString.Lazy as B
import Data.Array (Array,bounds,(!))
pixelPerCell = 5
main :: IO ()
main = do l <- getLine
let Just version = Q.version 1
errorLevel = Q.L -- or Q.M, Q.Q, Q.H
mode = Q.Alphanumeric -- or Q.Numeric
array :: Array (Int,Int) P.Pixel8
Just array = fmap Q.toArray (Q.encode version errorLevel mode l)
((y0,x0),(y1,x1)) = bounds array
pixelAt x y = let x' = x `div` pixelPerCell
y' = y `div` pixelPerCell
i = y'+y0
j = x'+x0
in array!(i,j)
image = P.generateImage pixelAt ((x1-x0+1)*pixelPerCell) ((y1-y0+1)*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