加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
pictureSolve.go 3.79 KB
一键复制 编辑 原始数据 按行查看 历史
程儒 提交于 2021-08-29 20:09 . 2.0version
package main
import (
"errors"
"fmt"
"github.com/nfnt/resize"
"image"
"image/color"
"image/draw"
"image/jpeg"
"os"
)
type ImageBasicInformation struct {
length int
width int
pixelPerL int
pixelPerW int
quality int
}
func CreateBackground(information ImageBasicInformation,filePath string,barrierPath string) (*image.NRGBA,error) {
imgFile, err := os.Create(filePath)
if err!=nil {
return nil,err
}
defer imgFile.Close()
barrFile, err := os.Open(barrierPath)
if err!=nil {
return nil,err
}
defer barrFile.Close()
barr, _, err := image.Decode(barrFile)
if err != nil {
return nil,err
}
maxL:=information.length
maxW:=information.width
ppL:=information.pixelPerL
ppW:=information.pixelPerW
pixelForL:=maxL*ppL
pixelForW:=maxW*ppW
newImg := image.NewNRGBA(image.Rect(0, 0, pixelForL, pixelForW)) //创建一个新RGBA图像
for i := 0; i < pixelForL; i++ {
for j := 0; j < pixelForW; j++ {
newImg.SetNRGBA(i,j,color.NRGBA{
R: 255,
G: 255,
B: 255,
A: 255,
})
}
}
barrResized := resize.Resize(uint(information.pixelPerL), uint(information.pixelPerW), barr, resize.Lanczos3)
for i:=0;i<maxL;i++ {
draw.Draw(newImg,newImg.Bounds(),barrResized,image.Pt(-1*i*ppL,0),draw.Over)
fmt.Println("a")
draw.Draw(newImg,newImg.Bounds(),barrResized,image.Pt(-1*i*ppL,-1*(maxW-1)*ppW),draw.Over)
fmt.Println("b")
}
for i:=1;i<maxW-1;i++ {
draw.Draw(newImg,newImg.Bounds(),barrResized,image.Pt(0,-1*i*ppW),draw.Over)
fmt.Println("c")
draw.Draw(newImg,newImg.Bounds(),barrResized,image.Pt(-1*(maxL-1)*ppL,-1*i*ppL),draw.Over)
fmt.Println("d")
}
err=jpeg.Encode(imgFile, newImg, &jpeg.Options{information.quality})
if err!=nil {
return nil,err
}
return newImg,nil
}
func OpenImageAndGetData(path string,information ImageBasicInformation) (*image.Image,error) {
imgFile, err := os.Open(path)
if err!=nil {
return nil,err
}
defer imgFile.Close()
img, _, err := image.Decode(imgFile)
if err != nil {
return nil,err
}
imgResized := resize.Resize(uint(information.pixelPerL), uint(information.pixelPerW), img, resize.Lanczos3)
return &imgResized,err
}
func RenderCandyAndSnake(filePath string,candy *Candy,snake *Snake,background *image.NRGBA,candyData *image.Image,snakeData *image.Image,information *ImageBasicInformation) error {
ppL:=information.pixelPerL
ppW:=information.pixelPerW
draw.Draw(background,background.Bounds(),*candyData,image.Pt(-1*candy.Abscissa*ppL,-1*candy.Ordinate*ppW),draw.Over)
for i := 0; i < len(snake.SnakeLine)-1; i++ {
if snake.SnakeLine[i].Direction == Up {
j := snake.SnakeLine[i].Abscissa
for k := snake.SnakeLine[i+1].Ordinate; k < snake.SnakeLine[i].Ordinate+1; k++ {
draw.Draw(background,background.Bounds(),*snakeData,image.Pt(-1*j*ppL,-1*k*ppW),draw.Over)
}
}
if snake.SnakeLine[i].Direction==Right {
j:=snake.SnakeLine[i].Ordinate
for k:=snake.SnakeLine[i].Abscissa;k<snake.SnakeLine[i+1].Abscissa+1;k++ {
draw.Draw(background,background.Bounds(),*snakeData,image.Pt(-1*k*ppL,-1*j*ppW),draw.Over)
}
}
if snake.SnakeLine[i].Direction==Down {
j:=snake.SnakeLine[i].Abscissa
for k:=snake.SnakeLine[i].Ordinate;k<snake.SnakeLine[i+1].Ordinate+1;k++ {
draw.Draw(background,background.Bounds(),*snakeData,image.Pt(-1*j*ppL,-1*k*ppW),draw.Over)
}
}
if snake.SnakeLine[i].Direction==Left {
j:=snake.SnakeLine[i].Ordinate
for k:=snake.SnakeLine[i+1].Abscissa;k<snake.SnakeLine[i].Abscissa+1;k++ {
draw.Draw(background,background.Bounds(),*snakeData,image.Pt(-1*k*ppL,-1*j*ppW),draw.Over)
}
}
if snake.SnakeLine[i].Direction==End {
return errors.New("蛇末提前到来")
}
}
imgFile, err := os.Create(filePath)
if err!=nil {
return err
}
defer imgFile.Close()
err=jpeg.Encode(imgFile, background, &jpeg.Options{information.quality})
if err!=nil {
return err
}
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化