Uzair_Abdullah
2 years agoStreaming Star
Convert Base64 string to an image
I am trying to convert the Base64 string received from the API, into a usable path for the image. The image will then be displayed in a Poster node. Here's what I have done so far:
Task.brs:
xfer = CreateObject("roURLTransfer")
xfer.SetCertificatesFile("common:/certs/ca-bundle.crt")
xfer.SetURL("https://api.api-ninjas.com/v1/qrcode?data=" + link + "&format=png")
xfer.addHeader("X-Api-Key", "my-key")
xfer.InitClientCertificates()
rsp = xfer.GetToString()
ba = CreateObject("roByteArray")
ba.FromBase64String(rsp)
WriteAsciiFile("tmp:/poster.txt", ba.ToAsciiString())
print "Decoded (?):" ba
file = ReadAsciiFile("tmp:/poster.txt")
uri = file
m.top.uri = uri
This is the response I get:
*** ERROR: Invalid path: '?PNG
?
'
*** ERROR: Invalid path: '?PNG
?
'
What am I doing wrong?
I tried it, but that didn't work. But I figured out what I was doing wrong.
- I was converting it into an ASCII string (that totally changed the content)
- I was writing it into an ASCII file
- When I set `uri = file`, I was setting the uri to the content of the file.
What I did now:
- Wrote the converted text to a byteArray file and provided the path as argument to the function
- Set the file path as the uri.
This worked, here's the refactored code:
// fetching data from API xfer = CreateObject("roURLTransfer") xfer.SetCertificatesFile("common:/certs/ca-bundle.crt") xfer.SetURL("https://api.api-ninjas.com/v1/qrcode?data=" + link + "&format=png") xfer.addHeader("X-Api-Key", "my-key") xfer.InitClientCertificates() rsp = xfer.GetToString() // Conversion from Base64 uri = "tmp:/poster.png" //the path ba = CreateObject("roByteArray") ba.fromBase64String(rsp) ba.writeFile(uri) m.top.uri = uri // setting the path
Hope this helps someone in the future!