去base64图像解码 [英] Go base64 image decode

查看:147
本文介绍了去base64图像解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


$ b

 

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(与data:image ...)仍然没有成功

解决方案

一个href =https://en.wikipedia.org/wiki/Data_URI_scheme =nofollow noreferrer>数据URI方案,关于如何对它进行解码的信息以及更多关于这个的问题和答案:

使用base64.StdEncoding.DecodeString(str)时输入字节4处的非法base64数据



但请注意, image.Decodeconfig() 只会解码在调用此函数之前注册的图像格式,因此您需要预先注册图像格式处理程序。这可以通过导入来完成,如

  import _image / png

更多信息,请参阅 图像 。或者如果你知道确切的格式(例如在你的例子中是PNG),你可以直接使用 png.DecodeConfig()



所以它不适合你,因为你的实际编码图像是PNG格式的,但是你没有注册PNG格式处理程序,所以 image.DecodeConfig()不会使用PNG处理程序(所以它不会能够解码它=> 未知图像格式)。

另外请注意,替换不是部分Base64编码图像是摆脱它的糟糕解决方案。只需切分输入字符串即可:

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

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


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

解决方案

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

Illegal base64 data at input byte 4 when using base64.StdEncoding.DecodeString(str)

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.

这篇关于去base64图像解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆