qr.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.BackgroundColor == "" {
  47. q.BackgroundColor = "#000"
  48. }
  49. if q.FrontColor == "" {
  50. q.FrontColor = "#fff"
  51. }
  52. _, background, _ := Alpha(q.BackgroundColor)
  53. _, front, _ := Alpha(q.FrontColor)
  54. backgroundColor := color.RGBA{R: background.R, G: background.G, B: background.B, A: background.A}
  55. frontColor := color.RGBA{R: front.R, G: front.G, B: front.B, A: front.A}
  56. if q.ViewSize.Width == 0 {
  57. q.ViewSize.Width = 200
  58. }
  59. qr, err := qrcode.New(q.Value, qrcode.Medium)
  60. if err != nil {
  61. return nil, err
  62. }
  63. qr.DisableBorder = true
  64. qr.ForegroundColor = frontColor
  65. qr.BackgroundColor = backgroundColor
  66. qrByte, err := qr.PNG(int(q.ViewSize.Width))
  67. if err != nil {
  68. return nil, util.NewError("qr.PNG err : ", err.Error())
  69. }
  70. qrWM := imagick.NewMagickWand()
  71. if err = qrWM.ReadImageBlob(qrByte); err != nil {
  72. return nil, util.NewError("qrWM.SetImageFilename err : ", err.Error())
  73. }
  74. if err = qrWM.SetSize(q.ViewSize.Width, q.ViewSize.Height); err != nil {
  75. return nil, util.NewError("qrWM.SetSize err : ", err.Error())
  76. }
  77. if q.Angle > 0 {
  78. if err = qrWM.RotateImage(OpacityPixel(), q.Angle); err != nil {
  79. return nil, util.NewError("qrWM.RotateImage err : ", err.Error())
  80. }
  81. alignX := float64(qrWM.GetImageWidth()-q.ViewSize.Width) / 2
  82. alignY := float64(qrWM.GetImageHeight()-q.ViewSize.Height) / 2
  83. q.ViewPoint.X = q.ViewPoint.X - alignX
  84. q.ViewPoint.Y = q.ViewPoint.Y - alignY
  85. }
  86. return qrWM, nil
  87. }