golang 的 rabbitmq 消费项目
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getBase64.go 1.4 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package qrcode
  2. // 生成登录二维码图片, 方便在网页上显示
  3. import (
  4. "bytes"
  5. "encoding/base64"
  6. "image/jpeg"
  7. "image/png"
  8. "github.com/boombuler/barcode"
  9. "github.com/boombuler/barcode/qr"
  10. )
  11. func GetJPGBase64(content string, edges ...int) string {
  12. edgeLen := 300
  13. if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
  14. edgeLen = edges[0]
  15. }
  16. img, _ := qr.Encode(content, qr.L, qr.Unicode)
  17. img, _ = barcode.Scale(img, edgeLen, edgeLen)
  18. emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区
  19. jpeg.Encode(emptyBuff, img, nil)
  20. dist := make([]byte, 50000) // 开辟存储空间
  21. base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64
  22. return "data:image/png;base64," + string(dist) // 输出图片base64(type = []byte)
  23. }
  24. func GetPNGBase64(content string, edges ...int) string {
  25. edgeLen := 300
  26. if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
  27. edgeLen = edges[0]
  28. }
  29. img, _ := qr.Encode(content, qr.L, qr.Unicode)
  30. img, _ = barcode.Scale(img, edgeLen, edgeLen)
  31. emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区
  32. png.Encode(emptyBuff, img)
  33. dist := make([]byte, 50000) // 开辟存储空间
  34. base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64
  35. return string(dist) // 输出图片base64(type = []byte)
  36. }