Golang : Generate Codabar
In this tutorial, we will learn how to generate Codabar with Golang. We will use couple of third party packages to achieve our objectives as some of the images packages lack certain functions and clear documentation at the time of writing.
This code example below will :
- Generate the coda barcode from the code := "A80186B"
- Scale to larger size.
- Write the string code "A80186B" to another image.
- Paste both barcode and string into a new image.
- Save the image to png file.
Here you go:
package main
import (
"fmt"
"github.com/boombuler/barcode"
"github.com/boombuler/barcode/codabar"
"github.com/disintegration/imaging"
"github.com/llgcode/draw2d"
"image"
"image/color"
"image/draw"
"os"
)
func main() {
code := "A80186B"
fmt.Println("Generating Codabar for : ", code)
bcode, err := codabar.Encode(code)
if err != nil {
fmt.Printf("String %s cannot be encoded", code)
os.Exit(1)
}
// scale to 250x20
bcode, err = barcode.Scale(bcode, 250, 20)
if err != nil {
fmt.Println("Codabar scaling error!")
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(300, 300, color.NRGBA{255, 255, 255, 255})
//paste the codabar to new blank image
newImg = imaging.Paste(newImg, bcode, image.Pt(50, 50))
//paste the text to the new blank image
newImg = imaging.Paste(newImg, img, image.Pt(90, 90))
err = draw2d.SaveToPngFile("./codabar.png", newImg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// everything ok
fmt.Println("CodaBar code generated and saved to codabar.png")
}
Sample output :
NOTE : For some odd reason, the luximbi.ttf font from https://github.com/llgcode/draw2d.samples/tree/master/helloworld cannot be used. Therefore, I've replaced it with the Monterey font.
You can get the Monterey font from http://www.1001freefonts.com/monterey.font , unzip the zip file and rename the MontereyFLF.ttf
to Montereymbi.ttf
with this command and execute the code above in the same directory as the 'Montereymbi.ttf' file.
cp MontereyFLF.ttf Montereymbi.ttf
References :
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
+10.2k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+12.1k Golang : "https://" not allowed in import path
+20.1k Golang : Secure(TLS) connection between server and client
+10.6k Golang : Simple image viewer with Go-GTK
+19k Golang : Fix cannot download, $GOPATH not set error
+50.3k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+6.5k Get Facebook friends working in same company
+18.2k Golang : Implement getters and setters
+4.4k Golang : How to pass data between controllers with JSON Web Token
+27.4k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+4.2k Java : Generate multiplication table example
+35.6k Golang : How to split or chunking a file to smaller pieces?