1、生成字节形式二维码
会生成下图字节数据
2、生成文件二维码图片
会生成下图文件样式
3、生成可编译颜色二维码图片
会生成下图文件样式
4、生成带logo的二维码
会生成下图样式文件
5番外–生成logo圆形并收缩边框
会生成下图文件
同样的,当我们需要logo是圆角的时候,我们可以设计一个裁剪圆角的方法,这个可能比较麻烦,我就懒得想了
完整代码qrCode.go
circle.go
package file
import (
"image"
"image/color"
)
type Circle struct {
P image.Point
R int
}
func (c *Circle) ColorModel() color.Model {
return color.AlphaModel
}
func (c *Circle) Bounds() image.Rectangle {
return image.Rect(c.P.X-c.R, c.P.Y-c.R, c.P.X+c.R, c.P.Y+c.R)
}
func (c *Circle) At(x, y int) color.Color {
xx, yy, rr := float64(x-c.P.X), float64(y-c.P.Y), float64(c.R)
if xx*xx+yy*yy < rr*rr {
return color.Alpha{A: 255}
}
return color.Alpha{}
}
rectangle.go
package file
import (
"image"
"image/color"
)
type Rectangle struct {
P image.Point
W int
H int
}
func (r *Rectangle) ColorModel() color.Model {
return color.AlphaModel
}
func (r *Rectangle) Bounds() image.Rectangle {
return image.Rect(r.P.X-r.W, r.P.Y-r.H, r.P.X+r.W, r.P.Y+r.H)
}
func (r *Rectangle) At(x, y int) color.Color {
xx, yy, ww, hh := float64(x-r.P.X), float64(y-r.P.Y), float64(r.W), float64(r.H)
if xx*xx+yy*yy < ww*ww+hh*hh {
return color.Alpha{A: 255}
}
return color.Alpha{}
}
main.go