package utils import ( "fmt" "github.com/beego/beego/v2/core/logs" "github.com/boombuler/barcode" "github.com/boombuler/barcode/code128" "github.com/signintech/gopdf" "image" "image/color" "image/jpeg" "image/png" "net/http" "os" "testing" "time" ) // TestGet is a sample to run an endpoint test func TestAmountConvert(t *testing.T) { en := AmountConvert(521.71, true) logs.Info(en) cols := []float64{20, 24, 20, 18, 20, 12, 22, 20, 12, 22, 20, 12, 22, 20, 15} sum := 0. for _, col := range cols { sum += col } cols1 := []string{} for _, col := range cols { fmt.Print(fmt.Sprintf("%.2f", col/sum*820), " ") cols1 = append(cols1, fmt.Sprintf("%.2f,", col/sum*820)) } fmt.Println(cols1) } func TestGetQRCode(t *testing.T) { // 创建一个code128编码的 BarcodeIntCS cs, _ := code128.Encode("123456") // 创建一个要输出数据的文件 file, _ := os.Create("qr3.png") defer file.Close() // 设置图片像素大小 qrCode, _ := barcode.Scale(cs, 350, 70) // 将code128的条形码编码为png图片 png.Encode(file, qrCode) } func TestDownloadImage(t *testing.T) { resp, err := http.Get("https://bzdcdn.baozhida.cn/2024-02-18/9be06bd0aa654c4c80fa7ce858db73be.png") //resp, err := http.Get("https://bzdcdn.baozhida.cn/2024-02-19/85219d304dc44d0ea4ce7f29bbc2c89e.jpg") if err != nil { fmt.Println(err) } defer resp.Body.Close() //imageBytes, err := io.ReadAll(resp.Body) // 解码图片 imgH, _, err := image.Decode(resp.Body) if err != nil { fmt.Println("解码图片失败", err) } // 检查图像的 Bounds 是否为 16 位深度 bounds := imgH.Bounds() if _, ok := imgH.At(bounds.Min.X, bounds.Min.Y).(color.Gray16); !ok { fmt.Println("Image does not have 16-bit depth.") } else { fmt.Println("Image has 16-bit depth.") } pdf := &gopdf.GoPdf{} pdf.Start(gopdf.Config{PageSize: gopdf.Rect{W: 595.28, H: 841.89}}) pdf.SetMarginTop(40) pdf.SetMarginBottom(40) pdf.AddPage() imgWidth := float64(imgH.Bounds().Dx()) imgHeight := float64(imgH.Bounds().Dy()) W, H := imgWidth, imgHeight if imgWidth > imgHeight && imgWidth > 555 { W = 555 H = 555 * float64(imgHeight) / float64(imgWidth) } if imgWidth < imgHeight && imgWidth > 760 { W = 760 * float64(imgWidth) / float64(imgHeight) H = 760 if W > 555 { H = 555 * 760 / W W = 555 } } file, _ := os.Create("./test.jpg") defer file.Close() jpeg.Encode(file, imgH, &jpeg.Options{100}) //err = pdf.ImageFrom(imgH, (595-W)/2, (840-H)/2, &gopdf.Rect{W: W, H: H}) err = pdf.Image("./test.jpg", (595-W)/2, (840-H)/2, &gopdf.Rect{W: W, H: H}) if err != nil { fmt.Println(err) } filename := "运输记录表" + time.Now().Format("20060102150405") + ".pdf" // 保存文件 if err = pdf.WritePdf("./" + filename); err != nil { fmt.Println(err) } }