附近小店
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

3 个月前
123456789101112131415161718192021222324252627282930313233343536373839
  1. package qrcode
  2. import (
  3. "bytes"
  4. "image/jpeg"
  5. "image/png"
  6. "net/http"
  7. "github.com/boombuler/barcode"
  8. "github.com/boombuler/barcode/qr"
  9. )
  10. func WritePng(w http.ResponseWriter, content string, edges ...int) error {
  11. edgeLen := 300
  12. if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
  13. edgeLen = edges[0]
  14. }
  15. img, _ := qr.Encode(content, qr.L, qr.Unicode)
  16. img, _ = barcode.Scale(img, edgeLen, edgeLen)
  17. buff := bytes.NewBuffer(nil)
  18. png.Encode(buff, img)
  19. w.Header().Set("Content-Type", "image/png")
  20. _, err := w.Write(buff.Bytes())
  21. return err
  22. }
  23. func WriteJpg(w http.ResponseWriter, content string, edges ...int) error {
  24. edgeLen := 300
  25. if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
  26. edgeLen = edges[0]
  27. }
  28. img, _ := qr.Encode(content, qr.L, qr.Unicode)
  29. img, _ = barcode.Scale(img, edgeLen, edgeLen)
  30. buff := bytes.NewBuffer(nil)
  31. jpeg.Encode(buff, img, nil)
  32. w.Header().Set("Content-Type", "image/jpg")
  33. _, err := w.Write(buff.Bytes())
  34. return err
  35. }