在go语言中生成EAN(European Article Number)条形码示例
Go  /  管理员 发布于 2年前   609
在Go语言中生成EAN条码可以通过这个包:barcode
https://github.com/boombuler/barcode
以前也我也写过关于生成Code128条形码的文章,有兴趣的自行搜索,也可以拉到底部相关文章,
下面关于如何使用barcode包来生成EAN(European Article Number)条形码,看下面例子。
代码示例:
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位数
code := "5901234123457"
fmt.Println("生成Datamatrix条形码,用于 : ", code)
//看看https://godoc.org/github.com/boombuler/barcode/ean
bcode, err := ean.Encode(code)
//如果校验和不匹配,请取消注释
//fmt.Println(err)
if err != nil {
fmt.Printf("String %s cannot be encoded\n", code)
os.Exit(1)
}
//比例为100x100
bcode, err = barcode.Scale(bcode, 100, 100)
if err != nil {
fmt.Println("EAN比例错误 : ", err)
os.Exit(1)
}
// 现在,我们要将代码附加在 EAN 的底部。
// 的底部。
// 创建一个带有文本数据的新图像
// 来自https://github.com/llgcode/draw2d.samples/tree/master/helloworld
// 设置用于搜索字体的全局文件夹
draw2d.SetFontFolder(".")
//在一个RGBA图像上初始化图形上下文
img := image.NewRGBA(image.Rect(0, 0, 250, 50))
//设置背景为白色
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()
//设置字体 Montereymbi.ttf
gc.SetFontData(draw2d.FontData{"Monterey", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
//将文本填充颜色设为黑色
gc.SetFillColor(image.Black)
gc.SetFontSize(14)
gc.FillStringAt(code, 50, 20)
//创建一个新的白色背景的空白图像
newImg := imaging.New(300, 200, color.NRGBA{255, 255, 255, 255})
//将代码栏粘贴到新的空白图像上
newImg = imaging.Paste(newImg, bcode, image.Pt(100, 30))
//将文本粘贴到新的空白图像上
newImg = imaging.Paste(newImg, img, image.Pt(50, 150))
err = draw2d.SaveToPngFile("./ean.png", newImg)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//完成
fmt.Println("生成EAN条形码并保存为ean.png")
}
输出:
相关文章
在go语言中生成Code128条形码示例
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号