qr.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package image
  2. import (
  3. "github.com/skip2/go-qrcode"
  4. "gopkg.in/gographics/imagick.v3/imagick"
  5. "icloudapp.cn/tools/util"
  6. "image/color"
  7. )
  8. type QR struct {
  9. Cache
  10. Value string
  11. FrontColor string
  12. BackgroundColor string
  13. ViewSize *Size
  14. ViewPoint *Point
  15. Angle float64
  16. }
  17. func NewQR() *QR {
  18. QRInstance := &QR{}
  19. QRInstance.SetSize(0, 0)
  20. QRInstance.SetViewPoint(0, 0)
  21. return QRInstance
  22. }
  23. func (q *QR) SetCache(path string) {
  24. q.Cache.SetCache(path)
  25. }
  26. func (q *QR) SetValue(value string) {
  27. q.Value = value
  28. }
  29. func (q *QR) SetFrontColor(frontColor string) {
  30. q.FrontColor = frontColor
  31. }
  32. func (q *QR) SetBackgroundColor(backgroundColor string) {
  33. q.BackgroundColor = backgroundColor
  34. }
  35. func (q *QR) SetSize(width, height uint) {
  36. q.ViewSize = NewSize(width, height)
  37. }
  38. func (q *QR) SetViewPoint(x, y float64) {
  39. q.ViewPoint = NewPoint(x, y)
  40. }
  41. // SetAngle 设置旋转角度
  42. func (q *QR) SetAngle(angle float64) {
  43. q.Angle = angle
  44. }
  45. func (q *QR) Create() (*imagick.MagickWand, error) {
  46. if q.ViewSize.Width == 0 || q.ViewSize.Height == 0 {
  47. return nil, util.NewError("Create qr", "未指定元素未设置长宽")
  48. }
  49. if q.BackgroundColor == "" {
  50. q.BackgroundColor = "#000"
  51. }
  52. if q.FrontColor == "" {
  53. q.FrontColor = "#fff"
  54. }
  55. _, background, _ := Alpha(q.BackgroundColor)
  56. _, front, _ := Alpha(q.FrontColor)
  57. backgroundColor := color.RGBA{R: background.R, G: background.G, B: background.B, A: background.A}
  58. frontColor := color.RGBA{R: front.R, G: front.G, B: front.B, A: front.A}
  59. if q.ViewSize.Width == 0 {
  60. q.ViewSize.Width = 200
  61. }
  62. qr, err := qrcode.New(q.Value, qrcode.Medium)
  63. if err != nil {
  64. return nil, err
  65. }
  66. qr.DisableBorder = false
  67. qr.ForegroundColor = frontColor
  68. qr.BackgroundColor = backgroundColor
  69. qrByte, err := qr.PNG(int(q.ViewSize.Width))
  70. if err != nil {
  71. return nil, util.NewError("qr.PNG err : ", err.Error())
  72. }
  73. qrWM := imagick.NewMagickWand()
  74. if err = qrWM.ReadImageBlob(qrByte); err != nil {
  75. return nil, util.NewError("qrWM.SetImageFilename err : ", err.Error())
  76. }
  77. if err = qrWM.SetSize(q.ViewSize.Width, q.ViewSize.Height); err != nil {
  78. return nil, util.NewError("qrWM.SetSize err : ", err.Error())
  79. }
  80. if q.Angle > 0 {
  81. if err = qrWM.RotateImage(OpacityPixel(), q.Angle); err != nil {
  82. return nil, util.NewError("qrWM.RotateImage err : ", err.Error())
  83. }
  84. alignX := float64(qrWM.GetImageWidth()-q.ViewSize.Width) / 2
  85. alignY := float64(qrWM.GetImageHeight()-q.ViewSize.Height) / 2
  86. q.ViewPoint.X = q.ViewPoint.X - alignX
  87. q.ViewPoint.Y = q.ViewPoint.Y - alignY
  88. }
  89. return qrWM, nil
  90. }