亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

golang pic.ShowImage 為什么它不生成圖像而是向我發送 base64 值

golang pic.ShowImage 為什么它不生成圖像而是向我發送 base64 值

Go
holdtom 2023-02-28 20:33:44
我用golang學習golang,是不是我的goland有問題?需要更改配置信息?我的代碼package mainimport (    "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) //調用}我使用https://tour.go-zh.org/methods/24 并運行 golang 返回圖片
查看完整描述

1 回答

?
弒天下

TA貢獻1818條經驗 獲得超8個贊

您的代碼或您的 Goland 沒有任何問題。


之所以在Go Playground上顯示圖片是因為Go Playground本身就支持這個。這有點像一個功能。


pic.ShowImage()沒有什么神奇的只是將文本打印到標準輸出。它打印IMAGE:您傳遞的圖像的 Base64 編碼數據,編碼為 PNG 圖像。解釋標準輸出的是 Go Playground,如果輸出以 開頭IMAGE:,后端將輸出的其余部分解釋為圖像的 Base64 編碼形式,生成<img>顯示該圖像的 HTML 標記。


請參閱此示例以驗證:


func main() {

    img := image.NewRGBA(image.Rect(0, 0, 100, 100))

    red := color.RGBA{R: 255, A: 255}

    for i := 0; i < 100; i++ {

        img.Set(i, i, red)

    }


    buf := &bytes.Buffer{}

    if err := png.Encode(buf, img); err != nil {

        panic(err)

    }


    fmt.Println("IMAGE:" + base64.StdEncoding.EncodeToString(buf.Bytes()))

}

在 Go Playground 上運行的這段代碼將產生(試試看:https ://go.dev/play/p/N0RQSTBcqdE ):

http://img1.sycdn.imooc.com//63fdf5a30001608511390851.jpg

查看完整回答
反對 回復 2023-02-28
  • 1 回答
  • 0 關注
  • 524 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號