golang pic.ShowImage 为什么它不生成图像而是向我发送 base64 值

我用golang学习golang,是不是我的goland有问题?需要更改配置信息?我的代码


package main


import (

    "golang.org/x/tour/pic"

    "image"

    "image/color"

)


type Image struct{} //新建一个Image结构体


func (i Image) ColorModel() color.Model { //实现Image包中颜色模式的方法

    return color.RGBAModel

}


func (i Image) Bounds() image.Rectangle { //实现Image包中生成图片边界的方法

    return image.Rect(0, 0, 200, 200)

}


func (i Image) At(x, y int) color.Color { //实现Image包中生成图像某个点的方法

    return color.RGBA{uint8(x), uint8(y), uint8(255), uint8(255)}

}


func main() {

    m := Image{}

    pic.ShowImage(m) //调用

}

http://img4.mukewang.com/63fdf577000142d425580414.jpg

我使用https://tour.go-zh.org/methods/24 并运行 golang 返回图片

http://img.mukewang.com/63fdf5910001e0a713751296.jpg



holdtom
浏览 636回答 1
1回答

弑天下

您的代码或您的 Goland 没有任何问题。之所以在Go Playground上显示图片是因为Go Playground本身就支持这个。这有点像一个功能。pic.ShowImage()没有什么神奇的只是将文本打印到标准输出。它打印IMAGE:您传递的图像的 Base64 编码数据,编码为 PNG 图像。解释标准输出的是 Go Playground,如果输出以 开头IMAGE:,后端将输出的其余部分解释为图像的 Base64 编码形式,生成<img>显示该图像的 HTML 标记。请参阅此示例以验证:func main() {&nbsp; &nbsp; img := image.NewRGBA(image.Rect(0, 0, 100, 100))&nbsp; &nbsp; red := color.RGBA{R: 255, A: 255}&nbsp; &nbsp; for i := 0; i < 100; i++ {&nbsp; &nbsp; &nbsp; &nbsp; img.Set(i, i, red)&nbsp; &nbsp; }&nbsp; &nbsp; buf := &bytes.Buffer{}&nbsp; &nbsp; if err := png.Encode(buf, img); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; fmt.Println("IMAGE:" + base64.StdEncoding.EncodeToString(buf.Bytes()))}在 Go Playground 上运行的这段代码将产生(试试看:https ://go.dev/play/p/N0RQSTBcqdE ):
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go