【发布时间】:2020-02-24 12:03:22
【问题描述】:
我有一个用 base64 字符串 mybase64 编码的 png 图像。如何将此 base64 字符串转换为 png 文件?
我试过了:
conn <- file("xxx.png", open = "wb")
base64enc::base64decode(what = mybase64, output = conn)
close(conn)
但这不会生成 png 文件。
【问题讨论】:
我有一个用 base64 字符串 mybase64 编码的 png 图像。如何将此 base64 字符串转换为 png 文件?
我试过了:
conn <- file("xxx.png", open = "wb")
base64enc::base64decode(what = mybase64, output = conn)
close(conn)
但这不会生成 png 文件。
【问题讨论】:
我找到了解决办法。
我的 base64 字符串:
mybase64 <- "data:image/png;base64,iVBORw0KGgoAAAANSU......"
然后:
raw <- base64enc::base64decode(what = substr(mybase64, 23, nchar(mybase64)))
png::writePNG(png::readPNG(raw), "mypng.png")
【讨论】:
应该这样做:)
library(base64enc)
inconn <- file("xxx.bin","rb")
outconn <- file("xxx.png","wb")
base64decode(what=inconn, output=outconn)
close(inconn)
close(outconn)
【讨论】:
xxx.bin?