广告平台(站长下代理使用)
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.
 
 
 
 
 
 

40 line
977 B

  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. }