🐁
TinyGo × Wio Terminal で動的に QR コードを表示したい
はじめに
動的とは事前に QR コードの画像を埋め込むのではなくプログラム中で文字列から QR コードを組み立てて表示することを示します。
本記事では QR コードを TinyGo で動的に表示するためのライブラリを調査し使用してみた共有となります。
本記事で使う TinyGo バージョンは以下です。
❯ tinygo version
tinygo version 0.39.0 darwin/arm64 (using go version go1.25.5 and LLVM version 19.1.2)
go-qrcode
実装コード
package main
import (
"image/color"
"github.com/skip2/go-qrcode"
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
"tinygo.org/x/drivers/ili9341"
)
func main() {
display := initdisplay.InitDisplay()
width, height := display.Size()
if width < 320 || height < 240 {
display.SetRotation(ili9341.Rotation270)
}
qr, err := qrcode.New("https://x.com/otakakot", qrcode.Low)
if err != nil {
panic(err)
}
bm := qr.Bitmap()
qrSize := len(bm)
blockSize := 8
displayWidth := int16(qrSize * blockSize)
displayHeight := int16(qrSize * blockSize)
offsetX := int16((320 - int(displayWidth)) / 2)
offsetY := int16((240 - int(displayHeight)) / 2)
white := color.RGBA{255, 255, 255, 255}
display.FillRectangle(offsetX, offsetY, displayWidth, displayHeight, white)
black := color.RGBA{0, 0, 0, 255}
for y := 0; y < qrSize; y++ {
for x := 0; x < qrSize; x++ {
if bm[y][x] {
display.FillRectangle(
offsetX+int16(x*blockSize),
offsetY+int16(y*blockSize),
int16(blockSize),
int16(blockSize),
black,
)
}
}
}
}
❯ tinygo build -target=wioterminal --size short -o main.uf2 main.go
code data bss | flash ram
106048 7448 8940 | 113496 16388

qrencode-go
実装コード
package main
import (
"image/color"
"github.com/qpliu/qrencode-go/qrencode"
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
"tinygo.org/x/drivers/ili9341"
)
func main() {
display := initdisplay.InitDisplay()
width, height := display.Size()
if width < 320 || height < 240 {
display.SetRotation(ili9341.Rotation270)
}
grid, err := qrencode.Encode("https://x.com/otakakot", qrencode.ECLevelL)
if err != nil {
panic(err)
}
qrSize := grid.Width()
blockSize := 8
displayWidth := int16(qrSize * blockSize)
displayHeight := int16(qrSize * blockSize)
offsetX := int16((320 - int(displayWidth)) / 2)
offsetY := int16((240 - int(displayHeight)) / 2)
white := color.RGBA{255, 255, 255, 255}
display.FillRectangle(offsetX, offsetY, displayWidth, displayHeight, white)
black := color.RGBA{0, 0, 0, 255}
for y := 0; y < qrSize; y++ {
for x := 0; x < qrSize; x++ {
if grid.Get(x, y) {
display.FillRectangle(
offsetX+int16(x*blockSize),
offsetY+int16(y*blockSize),
int16(blockSize),
int16(blockSize),
black,
)
}
}
}
}
❯ tinygo build -target=wioterminal --size short -o main.uf2 main.go
code data bss | flash ram
23288 3724 6744 | 27012 10468

gozxing
実装コード
package main
import (
"image/color"
"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
"tinygo.org/x/drivers/examples/ili9341/initdisplay"
"tinygo.org/x/drivers/ili9341"
)
func main() {
display := initdisplay.InitDisplay()
width, height := display.Size()
if width < 320 || height < 240 {
display.SetRotation(ili9341.Rotation270)
}
enc := qrcode.NewQRCodeWriter()
bm, _ := enc.Encode("https://x.com/otakakot", gozxing.BarcodeFormat_QR_CODE, 240, 240, nil)
qrWidth := bm.GetWidth()
qrHeight := bm.GetHeight()
blockSize := 8
displayWidth := int16(qrWidth * blockSize)
displayHeight := int16(qrHeight * blockSize)
offsetX := int16((320 - int(displayWidth)) / 2)
offsetY := int16((240 - int(displayHeight)) / 2)
white := color.RGBA{255, 255, 255, 255}
display.FillRectangle(offsetX, offsetY, displayWidth, displayHeight, white)
black := color.RGBA{0, 0, 0, 255}
for y := 0; y < qrHeight; y++ {
for x := 0; x < qrWidth; x++ {
if bm.Get(x, y) {
display.FillRectangle(
offsetX+int16(x*blockSize),
offsetY+int16(y*blockSize),
int16(blockSize),
int16(blockSize),
black,
)
}
}
}
}
❯ tinygo build -target=wioterminal --size short -o main.uf2 main.go
program too large for this chip (flash overflowed by 291264 bytes)
optimization guide: https://tinygo.org/docs/guides/optimizing-binaries/
残念ながらプログラムのサイズが大きすぎて Wio Terminal の保存容量には入らないとのことでした...
おわりに
TinyGo を使用して Wio Terminal 上で動的に QR コードを生成・表示するためのライブラリを 3 つ調査し、動作検証を行いました。
結果をまとめると以下のようになります。
qrencode-go: Flash 使用量が約 27KB と非常に軽量で、今回の検証の中では最も TinyGo 向きのライブラリでした。
go-qrcode: 動作は問題ありませんが、Flash 使用量が約 113KB と qrencode-go に比べて 4 倍ほど大きくなりました。
gozxing: 高機能ですが、ライブラリのサイズが大きすぎて Wio Terminal の Flash 容量には収まりませんでした。
マイコン開発においてはリソース(特に Flash 容量や RAM)の制約が厳しいため、単純に QR コードを表示したいだけであれば、軽量な qrencode-go を採用するのが良さそうです。
今回実装したコードは以下に置いておきます。
Discussion