工具包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.6 KiB

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