1 Star 0 Fork 0

Uni-Minds/utils

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
imageGenerator.go 1.59 KB
一键复制 编辑 原始数据 按行查看 历史
Liuxy 提交于 2022-10-15 14:14 +08:00 . Release 1.0.0
/*
* Copyright (c) 2019-2022
* Author: LIU Xiangyu
* File: imageGenerator.go
*/
package media
import (
"github.com/golang/freetype"
"github.com/golang/freetype/truetype"
"image"
"image/color"
"io/ioutil"
"os"
"time"
)
var font *truetype.Font
var FontFilePath = []string{"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", "/Library/Fonts/Arial Unicode.ttf"}
func ImageGen(width, height int, str string) image.Image {
i := image.NewAlpha(image.Rect(0, 0, width, height))
size := 20
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
i.Set(x, y, color.White)
}
for y := height - size; y < height; y++ {
i.Set(x, y, color.White)
}
}
for x := width - size; x < width; x++ {
for y := 0; y < size; y++ {
i.Set(x, y, color.White)
}
for y := height - size; y < height; y++ {
i.Set(x, y, color.White)
}
}
if font == nil {
for _, fpFont := range FontFilePath {
if _, err := os.Stat(fpFont); err != nil {
continue
}
fByte, _ := ioutil.ReadFile(fpFont)
font, _ = freetype.ParseFont(fByte)
break
}
}
fontContext := freetype.NewContext()
fontContext.SetDPI(72)
fontContext.SetFont(font)
fontContext.SetFontSize(40)
fontContext.SetClip(i.Bounds())
fontContext.SetDst(i)
fontContext.SetSrc(image.White)
//pt := freetype.Pt(width>>5, height>>1+int(fontContext.PointToFixed(40)>>8))
pt := freetype.Pt(size+10, 50)
_, _ = fontContext.DrawString(str, pt)
pt = freetype.Pt(width-300, 50)
_, _ = fontContext.DrawString("Emulator Mode", pt)
pt = freetype.Pt(size+10, height-10)
_, _ = fontContext.DrawString(time.Now().String(), pt)
return i
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/uni-minds/utils.git
git@gitee.com:uni-minds/utils.git
uni-minds
utils
utils
v1.0.2

搜索帮助