Convert gif image into base64 in GO

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

Convert gif image into base64 in GO

问题

我正在尝试在Go语言中通过UDP发送一个gif文件,我认为最简单的方法是将gif图像转换为base64字符串并发送,但是当我将文件字节转换为base64时,它返回一个空字符串。我尝试了使用"chilkat"模块的以下代码:

bd := chilkat.NewBinData()
success := bd.LoadFile(path.Join(folderPath, "final.gif"))
if success != true {
    panic("Fail!")
    bd.DisposeBinData()
    return
}
b64Data := *bd.GetEncoded("base64")

但是它不起作用,如果有人可以帮忙的话。

英文:

I am trying to send a gif file over UDP in go, the simplest method I think is to convert the gif image into a base64 string and send it, but when I convert the file bytes into base64 it returns an empty string, I've tried the module "chilkat" with this code :

bd := chilkat.NewBinData()
success := bd.LoadFile(path.Join(folderPath, "final.gif"))
if success != true {
	panic("Fail!")
	bd.DisposeBinData()
	return
}
b64Data := *bd.GetEncoded("base64")

But its not working, if someone can help me

答案1

得分: 0

我建议使用标准库中的encoding/base64包。

您可能希望将其转换或附加到表示您想要发送的UDP数据包的[]byte切片中。

以下是将文件路径或io.Reader编码为[]byte的一些示例:


import (
    "bytes"
    "encoding/base64"
    "io"
    "os"
)

func EncodePath(path string) ([]byte, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, err
    }
    output := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
    base64.StdEncoding.Encode(output, data)
    return output, nil
}

func EncodeReader(rd io.Reader) ([]byte, error) {
    var b bytes.Buffer
    enc := base64.NewEncoder(base64.StdEncoding, &b)
    if _, err := io.Copy(enc, rd); err != nil {
        return nil, err
    }
    if err := enc.Close(); err != nil {
        return nil, err
    }
    return b.Bytes(), nil
}

英文:

I would recommend using the encoding/base64 package in the standard library.

You will most likely want to convert or append it into a []byte slice that represents the UDP packet you want to send.

Here are some examples for encoding a file path or io.Reader into a []byte:


import (
    "bytes"
    "encoding/base64"
    "io"
    "os"
)

func EncodePath(path string) ([]byte, error) {
    data, err := os.ReadFile(path)
    if err != nil {
        return nil, err
    }
    output := make([]byte, base64.StdEncoding.EncodedLen(len(data)))
    base64.StdEncoding.Encode(output, data)
    return output, nil
}

func EncodeReader(rd io.Reader) ([]byte, error) {
    var b bytes.Buffer
    enc := base64.NewEncoder(base64.StdEncoding, &b)
    if _, err := io.Copy(enc, rd); err != nil {
        return nil, err
    }
    if err := enc.Close(); err != nil {
        return nil, err
    }
    return b.Bytes(), nil
}

答案2

得分: 0

我的解决方案是这样的:

var buff bytes.Buffer
gif.EncodeAll(&buff, &anim)
based := base64.StdEncoding.EncodeToString(buff.Bytes())

但是base64字符串明显太长了,无法通过UDP发送,所以正确发送它的解决方案是这样的:

encoded := recordScreenToGIF(seconds) // 录制X秒的屏幕,并将结果转换为base64 GIF
splited := SplitSubN(encoded, 10000) // 将"encoded"每10K个字符拆分
conn.Write([]byte(strconv.Itoa(len(splited))))
for _, elm := range splited {
	_, err = conn.Write([]byte(elm))
	if err != nil {
		panic(err)
	}
	time.Sleep(50 * time.Millisecond)
}
conn.Write([]byte("Done"))
英文:

My solution was this:

var buff bytes.Buffer
gif.EncodeAll(&buff, &anim)
based := base64.StdEncoding.EncodeToString(buff.Bytes())

But the base64 string was clearly too long to be sent in UDP,
so the solution to send it correctly is this:

encoded := recordScreenToGIF(seconds) // Record X seconds of screen, & convert the result into a base64 GIF
splited := SplitSubN(encoded, 10000) // Split "encoded" each 10K characters
conn.Write([]byte(strconv.Itoa(len(splited))))
for _, elm := range splited {
	_, err = conn.Write([]byte(elm))
	if err != nil {
		panic(err)
	}
	time.Sleep(50 * time.Millisecond)
}
conn.Write([]byte("Done"))

huangapple
  • 本文由 发表于 2023年1月2日 10:32:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/74978621.html
匿名

发表评论

匿名网友

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

确定