4
golang把base64转图片,转语音
source link: https://blog.51cto.com/u_15714369/5614601
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
golang把base64转图片,转语音
精选 原创wx62cbd84cdb743 2022-08-24 09:48:59 ©著作权
func SavePic(photo string) string {
log.Println("----接收图片----------")
i := strings.Index(photo, ",")
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(photo[i+1:]))
picName := tools.RandomStr(20) + ".jpg"
f, err := os.Create(tools.Dir() + picName)
if err != nil {
log.Println(err)
}
defer f.Close()
_, err = io.Copy(f, dec)
if err != nil {
log.Println(err)
}
log.Println("---图片接收完成------名称是", picName)
return picName
}
log.Println("----接收图片----------")
i := strings.Index(photo, ",")
dec := base64.NewDecoder(base64.StdEncoding, strings.NewReader(photo[i+1:]))
picName := tools.RandomStr(20) + ".jpg"
f, err := os.Create(tools.Dir() + picName)
if err != nil {
log.Println(err)
}
defer f.Close()
_, err = io.Copy(f, dec)
if err != nil {
log.Println(err)
}
log.Println("---图片接收完成------名称是", picName)
return picName
}
func SaveAudio(voice string) string {
log.Println("----接收语音----------")
i := strings.Index(voice, ",")
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(voice[i+1:]))
fileName := tools.RandomStr(20) + `.mp3`
// 以流式解码
buf := make([]byte, 2)
// 保存解码后的数据
dst := ``
for {
n, err := decoder.Read(buf)
if n == 0 || err != nil {
break
}
dst += string(buf[:n])
}
err := ioutil.WriteFile(tools.Dir()+fileName, []byte(dst), 0666)
if err != nil {
log.Println(err)
}
log.Println("----语音接收完成----------")
return fileName
}
log.Println("----接收语音----------")
i := strings.Index(voice, ",")
decoder := base64.NewDecoder(base64.StdEncoding, strings.NewReader(voice[i+1:]))
fileName := tools.RandomStr(20) + `.mp3`
// 以流式解码
buf := make([]byte, 2)
// 保存解码后的数据
dst := ``
for {
n, err := decoder.Read(buf)
if n == 0 || err != nil {
break
}
dst += string(buf[:n])
}
err := ioutil.WriteFile(tools.Dir()+fileName, []byte(dst), 0666)
if err != nil {
log.Println(err)
}
log.Println("----语音接收完成----------")
return fileName
}
- 赞
- 收藏
- 2评论
- 分享
- 举报
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK