go語言編程二維碼生成及識別
我們在做go web開發(fā)的時(shí)候,應(yīng)該都遇到生成二維碼分享的應(yīng)用場景,下面我將介紹下使用go如何生成二維碼。
安裝 go-qrcode
我們不得不慶幸go的生態(tài)已經(jīng)越來越豐富,有很多大牛已經(jīng)幫我們寫好了庫,我們不必造輪子,直接拿過來用就好。
首先,我們安裝我們用到的go-qrcode庫。
https://github.com/skip2/go-qrcode/
go get -u github.com/skip2/go-qrcode/…
生成普通二維碼
使用了這個庫,你會發(fā)現(xiàn)二維碼生成原來是如此的簡單,現(xiàn)在我們就來演示一下。
package main
import "github.com/skip2/go-qrcode"
func main() {
qrcode.WriteFile("https://blog.csdn.net/yang731227",qrcode.Medium,256,"./qrcode.png")
}

這樣我們就可以生成了一個二維碼。
我們首先看下func WriteFile(content string, level RecoveryLevel, size int, filename string) error的參數(shù)。
- content string 簡單明了,這個是二維碼內(nèi)容
- level RecoveryLevel 這個是二維碼容錯等級,取值有Low、Medium、High、Highest。
- size int 不用說都知道這個是定義二維碼大小
- filename string 二維碼的保存路徑
生成有前后背景顏色的二維碼
剛剛我們生成了一個前黑后白的二維碼,這次我們想搞點(diǎn)花樣,生成一個花花綠綠的二維碼,我們直接上代碼
package main
import (
"github.com/skip2/go-qrcode"
"image/color"
)
func main() {
//qrcode.WriteFile("https://blog.csdn.net/yang731227",qrcode.High,200,"./qrcode.png")
qrcode.WriteColorFile("https://blog.csdn.net/yang731227", qrcode.High, 256, color.Black, color.White, "./qrcode.png")
}
我們來看下func WriteColorFile(content string, level RecoveryLevel, size int, background, foreground color.Color, filename string) error的參數(shù),比WriteFile 多了兩個參數(shù) background, foreground color.Color 。我們可以從字面意思就知道,background 是背景顏色,foreground是前景顏色。
顏色我們可以使用 color定義 ,它為我們定義了兩個默認(rèn)顏色,Black和White。如果我們想用其他顏色怎么辦呢?它為我們提供了color.RGBA() 這個方法,RGBA()有4個參數(shù) 分別是RGB的值和透明值。
例如:
package main
import (
"github.com/skip2/go-qrcode"
"image/color"
)
func main() {
//qrcode.WriteFile("https://blog.csdn.net/yang731227",qrcode.High,200,"./qrcode.png")
qrcode.WriteColorFile("https://blog.csdn.net/yang731227", qrcode.High, 256, color.Black, color.White, "./qrcode.png")
}
識別二維碼
上面我們講了怎么生成二維,現(xiàn)在我們來實(shí)習(xí)解析二維碼,當(dāng)然我們還是需要借助別人寫的庫。
首先我們安裝庫
go get github.com/tuotoo/qrcode
然后我們直接上代碼
package main
import (
"fmt"
"os"
"github.com/tuotoo/qrcode"
)
func main() {
fi, err := os.Open("./qrcode.png")
if err != nil {
fmt.Println(err.Error())
return
}
defer fi.Close()
qrmatrix, err := qrcode.Decode(fi)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(qrmatrix.Content)
}
以上就是go語言編程二維碼生成及識別的詳細(xì)內(nèi)容,更多關(guān)于go語言二維碼生成識別的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
gin使用自定義結(jié)構(gòu)綁定表單數(shù)據(jù)的示例代碼
這篇文章主要介紹了gin使用自定義結(jié)構(gòu)綁定表單數(shù)據(jù)的示例代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
Go調(diào)用C++動態(tài)庫實(shí)現(xiàn)車牌識別的示例代碼
本文主要介紹了如何利用C++中Opencv、TensorRT等庫編譯出動態(tài)庫供Go調(diào)用,再寫個簡單的api對上傳的車輛圖片進(jìn)行車牌識別,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
golang中package?is?not?in?GOROOT報(bào)錯的真正解決辦法
這篇文章主要給大家介紹了關(guān)于golang中package?is?not?in?GOROOT報(bào)錯的真正解決辦法,文中通過圖文介紹的非常詳細(xì),對同樣遇到這個問題的朋友具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
Golang cron 定時(shí)器和定時(shí)任務(wù)的使用場景
Ticker是一個周期觸發(fā)定時(shí)的計(jì)時(shí)器,它會按照一個時(shí)間間隔往channel發(fā)送系統(tǒng)當(dāng)前時(shí)間,而channel的接收者可以以固定的時(shí)間間隔從channel中讀取事件,這篇文章主要介紹了Golang cron 定時(shí)器和定時(shí)任務(wù),需要的朋友可以參考下2022-09-09

