进行Base64图像解码

huangapple go评论97阅读模式
英文:

Go base64 image decode

问题

我目前正在从画布中获取一个 base64 图像数据 URL,类似于以下格式(这只是展示字符串的样子,并非我实际获取的 dataurl):

data:image/png;base64,iVkhdfjdAjdfirtn=

我需要解码该图像以检查图像的宽度和高度:

dataurl := strings.Replace(req.PostFormValue("dataurl"), "data:image/png;base64,", "", 1)

reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(dataurl))
c, _, err := image.DecodeConfig(reader)
if err != nil {
    log.Fatal(err)
}
log.Println(c.Width)

但是,在尝试解码配置时出现错误:

未知的图像格式

所以,我制作 dataurl 的方式可能是错误的,但是我不知道该怎么做。我还尝试了传递完整的 dataurl(包括 data:image...),但仍然没有成功。

英文:

Im currently getting a base64 image data url from a canvas something like this (not the dataurl im getting just to show how the string looks like)

data:image/png;base64,iVkhdfjdAjdfirtn=

I need to decode that image to check the width and the height of the image

    dataurl := strings.Replace(req.PostFormValue("dataurl"), "data:image/png;base64,", "", 1)

	reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(dataurl))
   	c, _, err := image.DecodeConfig(reader)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(c.Width)

But Im getting an error while trying to decode the config

Unknown image format

So yeah the way Im making the dataurl must be wrong but cant figure what to do. I also tried passing the full dataurl (with data:image...) still no success

答案1

得分: 10

你所拥有的是一个数据URI方案,关于如何解码它以及更多相关信息可以在这个问题和答案中找到:

https://stackoverflow.com/questions/31031589/illegal-base64-data-at-input-byte-4-when-using-base64-stdencoding-decodestrings

但请注意,image.DecodeConfig()只能解码在调用该函数之前注册的图像格式,所以你需要提前注册图像格式处理程序。可以通过导入以下代码来完成:

import _ "image/png"

关于此内容的更多信息可以在image的包文档中找到。或者,如果你知道确切的格式(例如,在你的示例中是PNG),你可以直接使用png.DecodeConfig()

所以它对你不起作用,是因为你实际编码的图像是PNG格式,但你没有注册PNG格式处理程序,所以image.DecodeConfig()不会使用PNG处理程序(因此无法解码它,会显示"未知图像格式")。

另外,请注意,替换不属于Base64编码图像的前缀是一个不好的解决方案。相反,只需切片输入字符串即可:

input := "data:image/png;base64,iVkhdfjdAjdfirtn="
b64data := input[strings.IndexByte(input, ',')+1:]

切片字符串甚至不会在内存中复制字符串,它只会创建一个新的(两个字的)字符串头。

英文:

What you have is a Data URI scheme, info on how to decode it and more on this is in this question and answer:

https://stackoverflow.com/questions/31031589/illegal-base64-data-at-input-byte-4-when-using-base64-stdencoding-decodestrings

But note that image.Decodeconfig() will only decode image formats that are registered prior to calling this function, so you need the image format handlers to be registered in advance. This can be done with imports like

import _ "image/png"

More on this is in the package doc of image. Or if you know the exact format (e.g. in your example it's PNG), you can directly use png.DecodeConfig().

So it doesn't work for you because your actual encoded image is of PNG format, but you didn't register the the PNG format handler and so image.DecodeConfig() won't use the PNG handler (and so it will not be able to decode it => "Unknown image format").

Also note that replacing the prefix that is not part of the Base64 encoded image is a poor solution to get rid of it. Instead simply slice the input string:

input := "data:image/png;base64,iVkhdfjdAjdfirtn="
b64data := input[strings.IndexByte(input, ',')+1:]

Slicing a string will not even copy the string in memory, it will just create a new (two-word) string header.

huangapple
  • 本文由 发表于 2015年10月24日 23:21:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/33319759.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定