Haskell使用qrcode库生成Google Authenticator二维码出错求助
Problem 1: Corrupted QR Code Content with qrcode Library
Looking at your code and the parsed QR content, two key issues are causing the broken result:
- Wrong QR Encoding Mode: You’re using the
Alphanumericmode, which has strict character set constraints. Even though your URL-encoded characters (like%3A/%2F) are technically allowed in this mode, the library’s encoding logic can fail with long, structured URL-encoded strings, leading to the garbled:00000000...output you see. - Unnecessary URL Encoding: Google Authenticator expects the raw
otpauthURI as QR code content—not a URL-encoded version. Your input string is double-encoded, which breaks the parsing flow for both the QR library and Google Authenticator.
Solution for Problem 1
Adjust your code to use the Byte (binary) mode (which reliably handles any 8-bit data) and pass the raw, unencoded otpauth URI:
testQR :: IO () testQR = do let path = "hello.pgm" otpURI = "otpauth://totp/Label:James?secret=GEZDGNBVGY3TQOJQGEZDGNBVGY3TQOJQ&issuer=MyCompany" arrayToFile path . toArray . fromJust . encode (fromJust $ version 1) M Byte $ otpURI
This eliminates character set limitations and ensures the otpauth URI is encoded exactly as Google Authenticator expects it.
Problem 2: Missing C Library Error for haskell-qrencode
The haskell-qrencode package is a Haskell binding to the C library libqrencode, so you need to install the system-level development files for libqrencode first before setting up the Haskell package.
Solution for Problem 2
Install the C library based on your operating system:
- Ubuntu/Debian:
sudo apt-get update && sudo apt-get install libqrencode-dev - Fedora/RHEL:
sudo dnf install libqrencode-devel - macOS (with Homebrew):
brew install qrencode
Once the C library is installed, install the Haskell package via Cabal or Stack:
# Using Cabal cabal install haskell-qrencode # Using Stack stack install haskell-qrencode
内容的提问来源于stack exchange,提问作者James Burton

