Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use go language programming to realize the generation and recognition of QR code

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

In this article, the editor introduces in detail "how to use go language programming to achieve QR code generation and recognition". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use go language programming to achieve QR code generation and recognition" can help you solve your doubts. Let's follow the editor's ideas to learn new knowledge.

Install go-qrcode

We have to rejoice that the ecology of go has become more and more abundant, and many Daniel have written the library for us. We don't have to build wheels, just bring them over and use them.

First, we install the go- qrcode library we used.

Go get-u github.com/skip2/go-qrcode/...

Generate ordinary QR code

Using this library, you will find that QR code generation used to be so simple, now let's demonstrate it.

Package mainimport "github.com/skip2/go-qrcode" func main () {qrcode.WriteFile ("https://blog.csdn.net/yang731227",qrcode.Medium,256,"./qrcode.png")}"

So we can generate a QR code.

Let's first look at the parameters of func WriteFile (content string, level RecoveryLevel, size int, filename string) error.

Content string is simple and clear. This is the content of QR code.

Level RecoveryLevel this is the fault tolerance level of the QR code. The values include Low, Medium, High, and Highest.

Size int needless to say that this is the definition of QR code size

The saving path of filename string QR code

Generate a QR code with front and back background colors

Just now we generated a pre-black and white QR code, this time we want to do some tricks to generate a colorful QR code, we directly on the code

Package mainimport ("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")}

Let's take a look at the parameters of func WriteColorFile (content string, level RecoveryLevel, size int, background, foreground color.Color, filename string) error, which has two more parameters background and foreground color.Color than WriteFile. We can know literally that background is the background color and foreground is the foreground color.

We can define colors using color, which defines two default colors for us, Black and White. What if we want to use other colors? It provides us with the method color.RGBA (), which has four parameters that are the value of RGB and the value of transparency, respectively.

For example:

Package mainimport ("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")} identify QR codes

Above we talked about how to generate two-dimensional, now let's practice parsing QR code, of course, we still need to use the library written by others.

First, we install the library.

Go get github.com/tuotoo/qrcode

And then we go straight to the code.

Package mainimport ("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)} here This article "how to use go language programming to achieve QR code generation and recognition" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 297

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report