代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。