png_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package qr
  5. import (
  6. "bytes"
  7. "image/png"
  8. "io/ioutil"
  9. "os"
  10. "testing"
  11. )
  12. func Test2File(t *testing.T) {
  13. r, _ := Encode("http://www.baidu.com", L)
  14. fi, _ := os.OpenFile("C:\\Users\\admin\\Desktop\\2.png", os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0x666)
  15. fi.Write(r.PNG())
  16. }
  17. func TestPNG(t *testing.T) {
  18. c, err := Encode("http://www.baidu.com", H)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. pngdat := c.PNG()
  23. if true {
  24. ioutil.WriteFile("x.png", pngdat, 0666)
  25. }
  26. }
  27. func BenchmarkPNG(b *testing.B) {
  28. c, err := Encode("0123456789012345678901234567890123456789", L)
  29. if err != nil {
  30. panic(err)
  31. }
  32. var bytes []byte
  33. for i := 0; i < b.N; i++ {
  34. bytes = c.PNG()
  35. }
  36. b.SetBytes(int64(len(bytes)))
  37. }
  38. func BenchmarkImagePNG(b *testing.B) {
  39. c, err := Encode("0123456789012345678901234567890123456789", L)
  40. if err != nil {
  41. panic(err)
  42. }
  43. var buf bytes.Buffer
  44. for i := 0; i < b.N; i++ {
  45. buf.Reset()
  46. png.Encode(&buf, c.Image())
  47. }
  48. b.SetBytes(int64(buf.Len()))
  49. }