-
Notifications
You must be signed in to change notification settings - Fork 168
Colorized barcode
Florian edited this page Dec 7, 2017
·
1 revision
To colorize a barcode you could use the following code:
package main
import (
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
"image/color"
"image/png"
"os"
)
type coloredBarcode struct {
barcode.Barcode
cm color.Model
fg color.Color
bg color.Color
}
func NewColoredBarcode(bc barcode.Barcode, cm color.Model, foreground color.Color, background color.Color) barcode.Barcode {
return &coloredBarcode{bc, cm, foreground, background}
}
func (bc *coloredBarcode) ColorModel() color.Model {
return bc.cm
}
func (bc *coloredBarcode) At(x, y int) color.Color {
c := bc.Barcode.At(x, y)
if c == color.Black {
return bc.fg
} else {
return bc.bg
}
}
func main() {
bc, _ := qr.Encode("foobar", qr.L, qr.Auto)
bc = NewColoredBarcode(bc, color.RGBAModel, color.RGBA{255, 0, 0, 255}, color.RGBA{255, 255, 0, 255})
f, _ := os.Create("foobar.png")
defer f.Close()
png.Encode(f, bc)
}