package main
import (
"fmt"
"image"
"bytes"
"image/png"
"encoding/base64"
)
func Pic(dx, dy int) [][]uint8 {
//dx元素長(zhǎng)度纹磺,dy整個(gè)slice長(zhǎng)度
s := [][]uint8{}
ss := []uint8{}
for i := 0; i < dx; i++ {
ss = append(ss, 45)
}
for i := 0; i < dy; i++ {
s = append(s, ss)
}
return s
}
func Show(f func(int, int) [][]uint8) {
const (
dx = 256
dy = 256
)
data := f(dx, dy)
m := image.NewNRGBA(image.Rect(0, 0, dx, dy))
for y := 0; y < dy; y++ {
for x := 0; x < dx; x++ {
v := data[y][x]
i := y*m.Stride + x*4
m.Pix[i] = v
m.Pix[i+1] = v
m.Pix[i+2] = 255
m.Pix[i+3] = 255
}
}
ShowImage(m)
}
func ShowImage(m image.Image) {
var buf bytes.Buffer
err := png.Encode(&buf, m)
if err != nil {
panic(err)
}
enc := base64.StdEncoding.EncodeToString(buf.Bytes())
fmt.Println("IMAGE:" + enc)
}
func main() {
Show(Pic)
}
在瀏覽器運(yùn)行傍睹,可以看到運(yùn)行結(jié)果。
可在下面地址輸入此例代碼偷拔,并運(yùn)行和分析廓块。
https://tour.golang.org/welcome/1