GolangNote

Golang笔记

golang 把http.Client 读取的图片直接放到image.Decode 处理

Permalink

用http.Client 从网络上读到图像文件,response body 是 io.Reader,可以直接输给 image.Decode

简单示例如下:

Go: image.Decode
1
2
3
4
5
6
7
8
9
res, err = http.Get("URL HERE")
if err != nil || res.StatusCode != 200 {
    // handle errors
}
defer res.Body.Close()
m, _, err := image.Decode(res.Body)
if err != nil {
   // handle error
}

官方示例:

image.Decode 例子

参考文档 https://golang.org/pkg/image/#Decode

本文网址: https://golangnote.com/topic/237.html 转摘请注明来源

Related articles

Golang http client 处理重定向网页

假设一个网址有多个重定向,A-B-C-D,使用 http.Client.Get 最后取得的内容是网址D的内容,我们该手动处理包含重定向的网址。...

Golang http IPv4/IPv6 服务

Golang 的网络服务,如果不指定IPv4 或 IPv6,如果VPS 同时支持 IPv4 和 IPv6,`net.Listen()` 只会监听 IPv6 地址。但这不影响客户端使用 IPv4 地址来访问。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

Write a Comment to "golang 把http.Client 读取的图片直接放到image.Decode 处理"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms