2 Star 0 Fork 0

hero/momo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
jpegdata.go 2.14 KB
一键复制 编辑 原始数据 按行查看 历史
hero 提交于 2024-11-04 11:45 . upd:目录结构调整
/**
* @Author Awen
* @Date 2024/06/01
* @Email wengaolng@gmail.com
**/
package imagedata
import (
"fmt"
"image"
"gitee.com/linqwen/momo/app/rbac/captcha/base/codec"
"gitee.com/linqwen/momo/app/rbac/captcha/base/option"
)
// JPEGImageData .
type JPEGImageData interface {
Get() image.Image
ToBytes() []byte
ToBytesWithQuality(imageQuality int) []byte
ToBase64() string
ToBase64WithQuality(imageQuality int) string
SaveToFile(filepath string, quality int) error
}
var _ JPEGImageData = (*jpegImageDta)(nil)
// jpegImageDta .
type jpegImageDta struct {
image image.Image
}
// NewJPEGImageData .
func NewJPEGImageData(img image.Image) JPEGImageData {
return &jpegImageDta{
image: img,
}
}
// Get is get the original picture
func (c *jpegImageDta) Get() image.Image {
return c.image
}
// SaveToFile is to save JPEG as a file
func (c *jpegImageDta) SaveToFile(filepath string, quality int) error {
if c.image == nil {
return fmt.Errorf("missing image data")
}
return saveToFile(c.image, filepath, false, quality)
}
// ToBytes is to convert JPEG into byte array
func (c *jpegImageDta) ToBytes() []byte {
if c.image == nil {
return []byte{}
}
return codec.EncodeJPEGToByte(c.image, option.QualityNone)
}
// ToBytesWithQuality is to convert JPEG into byte array with quality
func (c *jpegImageDta) ToBytesWithQuality(imageQuality int) []byte {
if c.image == nil {
return []byte{}
}
if imageQuality <= option.QualityNone && imageQuality >= option.QualityLevel5 {
return codec.EncodeJPEGToByte(c.image, imageQuality)
}
return codec.EncodeJPEGToByte(c.image, option.QualityNone)
}
// ToBase64 is to convert JPEG into base64
func (c *jpegImageDta) ToBase64() string {
if c.image == nil {
return ""
}
return codec.EncodeJPEGToBase64(c.image, option.QualityNone)
}
// ToBase64WithQuality is to convert JPEG into base64 with quality
func (c *jpegImageDta) ToBase64WithQuality(imageQuality int) string {
if c.image == nil {
return ""
}
if imageQuality <= option.QualityNone && imageQuality >= option.QualityLevel5 {
return codec.EncodeJPEGToBase64(c.image, imageQuality)
}
return codec.EncodeJPEGToBase64(c.image, option.QualityNone)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/linqwen/momo.git
git@gitee.com:linqwen/momo.git
linqwen
momo
momo
v1.0.8

搜索帮助