Forum Discussion

Uzair_Abdullah's avatar
Uzair_Abdullah
Streaming Star
2 years ago
Solved

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.

    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!

2 Replies

  • jcgeorge's avatar
    jcgeorge
    Channel Surfer

    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's avatar
      Uzair_Abdullah
      Streaming Star

      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!