Golang : How to generate Code 39 barcode?
Additional tutorial on how to use https://github.com/boombuler/barcode package to generate code 39 barcode. Code adapted from previous codabar tutorial.
Here you go :
package main
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/code39"
"github.com/disintegration/imaging"
"github.com/llgcode/draw2d"
"image"
"image/color"
"image/draw"
"os"
)
func main() {
code := "SOCKETLOOP PORT 443"
fmt.Println("Generating code39 barcode for : ", code)
// see https://godoc.org/github.com/boombuler/barcode/code39
bcode, err := code39.Encode(code, false, false)
if err != nil {
fmt.Printf("String %s cannot be encoded\n", code)
os.Exit(1)
}
// scale to 300x20
bcode, err = barcode.Scale(bcode, 300, 20)
if err != nil {
fmt.Println("Code39 scaling error : ", err)
os.Exit(1)
}
// now we want to append the code at the bottom
// of the Codabar
// 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(400, 200, color.NRGBA{255, 255, 255, 255})
//paste the codabar to new blank image
newImg = imaging.Paste(newImg, bcode, image.Pt(20, 50))
//paste the text to the new blank image
newImg = imaging.Paste(newImg, img, image.Pt(50, 90))
err = draw2d.SaveToPngFile("./code39.png", newImg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("Code39 barcode generated and saved to code39.png")
}
Sample output :
References :
See also : Golang : Generate Codabar
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
+4.8k Golang : Constant and variable names in native language
+10.7k Golang : Fix go.exe is not compatible with the version of Windows you're running
+13.7k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+19.5k Golang : Archive directory with tar and gzip
+8k Golang : Configure Apache and NGINX to access your Go service example
+5.9k Golang : Debug with Godebug
+12.9k Golang : How to get a user home directory path?
+13.8k Javascript : Prompt confirmation before exit
+18.3k Golang : Find IP address from string
+47.6k Golang : How to convert JSON string to map and slice
+12k Golang : How to check if a string starts or ends with certain characters or words?
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?