Golang : Generate EAN barcode
Short tutorial on how to use https://github.com/boombuler/barcode package to generate EAN(European Article Number, but now known as International Article Number) barcode. Code adapted from previous codabar tutorial.
Here you go :
package main
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/ean"
"github.com/disintegration/imaging"
"github.com/llgcode/draw2d"
"image"
"image/color"
"image/draw"
"os"
)
func main() {
// 13 digits
code := "5901234123457"
fmt.Println("Generating Datamatrix barcode for : ", code)
// see https://godoc.org/github.com/boombuler/barcode/ean
bcode, err := ean.Encode(code)
// uncomment if checksum missmatch
//fmt.Println(err)
if err != nil {
fmt.Printf("String %s cannot be encoded\n", code)
os.Exit(1)
}
// scale to 100x100
bcode, err = barcode.Scale(bcode, 100, 100)
if err != nil {
fmt.Println("EAN scaling error : ", err)
os.Exit(1)
}
// now we want to append the code at the bottom
// of the EAN
// Create an new image with text data
// From https://github.com/llgcode/draw2d.samples/tree/master/helloworld
// Set the global folder for searching fonts
draw2d.SetFontFolder(".")
// Initialize the graphic context on an RGBA image
img := image.NewRGBA(image.Rect(0, 0, 250, 50))
// set background to white
white := color.RGBA{255, 255, 255, 255}
draw.Draw(img, img.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)
gc := draw2d.NewGraphicContext(img)
gc.FillStroke()
// Set the font Montereymbi.ttf
gc.SetFontData(draw2d.FontData{"Monterey", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
// Set the fill text color to black
gc.SetFillColor(image.Black)
gc.SetFontSize(14)
gc.FillStringAt(code, 50, 20)
// create a new blank image with white background
newImg := imaging.New(300, 200, color.NRGBA{255, 255, 255, 255})
//paste the codabar to new blank image
newImg = imaging.Paste(newImg, bcode, image.Pt(100, 30))
//paste the text to the new blank image
newImg = imaging.Paste(newImg, img, image.Pt(50, 150))
err = draw2d.SaveToPngFile("./ean.png", newImg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("EAN barcode generated and saved to ean.png")
}
Sample output :
References :
See also : Golang : How to generate Code 39 barcode?
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+6.8k Golang : Pat multiplexer routing example
+22k Golang : How to run Golang application such as web server in the background or as daemon?
+23.9k Golang : Find biggest/largest number in array
+6.9k Golang : Gargish-English language translator
+8.1k Golang : Add build version and other information in executables
+6.7k Golang : Find the longest line of text example
+30.3k Golang : How to redirect to new page with net/http?
+6.1k PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
+19.1k Golang : Populate dropdown with html/template example
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode
+25.1k Golang : Storing cookies in http.CookieJar example
+15.8k Golang : Read large file with bufio.Scanner cause token too long error