Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Uzair_Abdullah
Streaming Star

Convert Base64 string to an image

Jump to solution

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?

 

0 Kudos
1 Solution

Accepted Solutions
Uzair_Abdullah
Streaming Star

Re: Convert Base64 string to an image

Jump to solution

I tried it, but that didn't work. But I figured out what I was doing wrong.

  1. I was converting it into an ASCII string (that totally changed the content)
  2. I was writing it into an ASCII file
  3. When I set `uri = file`, I was setting the uri to the content of the file.

What I did now:

  1. Wrote the converted text to a byteArray file and provided the path as argument to the function
  2. 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!

View solution in original post

2 REPLIES 2
jcgeorge
Channel Surfer

Re: Convert Base64 string to an image

Jump to solution

Based on the API url, it looks like it can return PNGs, so why not use xfer.AsyncGetToFile() instead of xfer.GetToString() and skip all the conversions you have?

Another potential solution is skipping the task approach altogether, and just using Poster node. Something along the lines of

1. Create roHttpAgent
2. Use AddHeader() on the http agent to add your X-Api-Key header to it
3. Create Poster node
4. Use SetHttpAgent() on the Poster node to set the roHttpAgent from step 1 to the Poster node
5. Set Poster node uri field to your API url with the target link 

(Poster nodes might have an httpAgent by default, not too sure, in which case you can reduce the number of steps above)

Uzair_Abdullah
Streaming Star

Re: Convert Base64 string to an image

Jump to solution

I tried it, but that didn't work. But I figured out what I was doing wrong.

  1. I was converting it into an ASCII string (that totally changed the content)
  2. I was writing it into an ASCII file
  3. When I set `uri = file`, I was setting the uri to the content of the file.

What I did now:

  1. Wrote the converted text to a byteArray file and provided the path as argument to the function
  2. 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!

Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.