comm_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/core/logs"
  5. "github.com/boombuler/barcode"
  6. "github.com/boombuler/barcode/code128"
  7. "github.com/signintech/gopdf"
  8. "image"
  9. "image/color"
  10. "image/jpeg"
  11. "image/png"
  12. "net/http"
  13. "os"
  14. "testing"
  15. "time"
  16. )
  17. // TestGet is a sample to run an endpoint test
  18. func TestAmountConvert(t *testing.T) {
  19. en := AmountConvert(521.71, true)
  20. logs.Info(en)
  21. cols := []float64{20, 24, 20, 18, 20, 12, 22, 20, 12, 22, 20, 12, 22, 20, 15}
  22. sum := 0.
  23. for _, col := range cols {
  24. sum += col
  25. }
  26. cols1 := []string{}
  27. for _, col := range cols {
  28. fmt.Print(fmt.Sprintf("%.2f", col/sum*820), " ")
  29. cols1 = append(cols1, fmt.Sprintf("%.2f,", col/sum*820))
  30. }
  31. fmt.Println(cols1)
  32. }
  33. func TestGetQRCode(t *testing.T) {
  34. // 创建一个code128编码的 BarcodeIntCS
  35. cs, _ := code128.Encode("123456")
  36. // 创建一个要输出数据的文件
  37. file, _ := os.Create("qr3.png")
  38. defer file.Close()
  39. // 设置图片像素大小
  40. qrCode, _ := barcode.Scale(cs, 350, 70)
  41. // 将code128的条形码编码为png图片
  42. png.Encode(file, qrCode)
  43. }
  44. func TestDownloadImage(t *testing.T) {
  45. resp, err := http.Get("https://bzdcdn.baozhida.cn/2024-02-18/9be06bd0aa654c4c80fa7ce858db73be.png")
  46. //resp, err := http.Get("https://bzdcdn.baozhida.cn/2024-02-19/85219d304dc44d0ea4ce7f29bbc2c89e.jpg")
  47. if err != nil {
  48. fmt.Println(err)
  49. }
  50. defer resp.Body.Close()
  51. //imageBytes, err := io.ReadAll(resp.Body)
  52. // 解码图片
  53. imgH, _, err := image.Decode(resp.Body)
  54. if err != nil {
  55. fmt.Println("解码图片失败", err)
  56. }
  57. // 检查图像的 Bounds 是否为 16 位深度
  58. bounds := imgH.Bounds()
  59. if _, ok := imgH.At(bounds.Min.X, bounds.Min.Y).(color.Gray16); !ok {
  60. fmt.Println("Image does not have 16-bit depth.")
  61. } else {
  62. fmt.Println("Image has 16-bit depth.")
  63. }
  64. pdf := &gopdf.GoPdf{}
  65. pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}})
  66. pdf.SetMarginTop(40)
  67. pdf.SetMarginBottom(40)
  68. pdf.AddPage()
  69. imgWidth := float64(imgH.Bounds().Dx())
  70. imgHeight := float64(imgH.Bounds().Dy())
  71. W, H := imgWidth, imgHeight
  72. if imgWidth > imgHeight && imgWidth > 555 {
  73. W = 555
  74. H = 555 * float64(imgHeight) / float64(imgWidth)
  75. }
  76. if imgWidth < imgHeight && imgWidth > 760 {
  77. W = 760 * float64(imgWidth) / float64(imgHeight)
  78. H = 760
  79. if W > 555 {
  80. H = 555 * 760 / W
  81. W = 555
  82. }
  83. }
  84. file, _ := os.Create("./test.jpg")
  85. defer file.Close()
  86. jpeg.Encode(file, imgH, &jpeg.Options{100})
  87. //err = pdf.ImageFrom(imgH, (595-W)/2, (840-H)/2, &gopdf.Rect{W: W, H: H})
  88. err = pdf.Image("./test.jpg", (595-W)/2, (840-H)/2, &gopdf.Rect{W: W, H: H})
  89. if err != nil {
  90. fmt.Println(err)
  91. }
  92. filename := "运输记录表" + time.Now().Format("20060102150405") + ".pdf"
  93. // 保存文件
  94. if err = pdf.WritePdf("./" + filename); err != nil {
  95. fmt.Println(err)
  96. }
  97. }