Forum Discussion

RohitBBorse's avatar
RohitBBorse
Contributor
14 years ago

Decode base64 string to an image(pdf or png) using groovy

Hi,

I wanted to decode base64 string to an image(pdf or png)

Originally, image type (pdf, png) is getting encoded into base64 file in xml response as below
<ns1:Label>
<ns1:Parts>
<ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
<ns1:Image>HOJBAAAElFTkSuQmCC.....</ns1:Image>
</ns1:Parts>
</ns1:Label>

I am trying to decode value from Image body to pdf or png.

Please suggest a groovy script for this.


Rohit
  • Finan's avatar
    Finan
    Frequent Contributor
    I do not know how to switch the output thats base64 encode to a img, but I assume its an encoded bytestream, so I've written the following script that should do the trick.
    The script is added to a new testCase that has the encoded String as testCase property. (So it can be run as runTestCase testStep).
    The filename contains the path you want to write to relative to the projectpath. (Path should also include the extension)

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
    import org.apache.commons.codec.binary.Base64;
    Base64 coder = new Base64();

    def filename = context.expand( '${#TestCase#filename}' )
    def encodedString = context.expand( '${#TestCase#encodedString}' );
    def decoded = encodedString.decodeBase64();
    def f = new File(groovyUtils.projectPath + filename);
    f.delete();
    f << decoded

    log.info(groovyUtils.projectPath + filename);
  • Thanks Finan

    But I am still not able to switch into desired output i.e. pdf or png image type.

    I have my code as code below which saves the encodeed string as base64 extension into D drive. I then needs to extract the file from this base64 manually and view the image output.

    Could you please look into the below code and suggest how I can make it to the expected final one.

    ----------- Code is as below ---------------
    teststep=context.testCase.getTestStepAt(context.getCurrentStepIndex()).getLabel()

    Image='$'+"{"+teststep+"#Response#//ns1:Label/ns1:Parts/ns1:Image}"
    Label=context.expand(Image)

    //Specify the location of the label to be saved as below.
    def File = new PrintWriter ("D:/"+teststep+".b64")

    File.println(Label)
    File.flush()
    File.close()
    -------------------------------

    Thanks in advance
    Rohit
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Rohit,

    No more cross-posting (posting the same topic in multiple forums) please. For more information, see the forum rules.

    /Henrik
    eviware
  • Hi Henrik,

    Sorry ! It happened by mistake. Ahead to this a due care will be taken.


    Thanks
    Rohit
  • Hi,

    I am able to switch the base64 string to image using Base64 class and swing builder as below:


    import groovy.swing.SwingBuilder
    import javax.swing.ImageIcon
    import groovy.swing.SwingBuilder
    import org.apache.commons.codec.binary.Base64

    teststep=context.testCase.getTestStepAt(context.getCurrentStepIndex()).getLabel()
    Image='$'+"{"+teststep+"#Response#//ns1:Label/ns1:Parts/ns1:Image}"
    Label=context.expand(Image)
    b64 = new Base64()
    swing = new groovy.swing.SwingBuilder()
    i1 = swing.label(icon:new ImageIcon(b64.decode(Label.getBytes())))

    frame = swing.frame(title:"LABEL IMAGE", defaultCloseOperation:javax.swing.WindowConstants.DISPOSE_ON_CLOSE) {
    panel(){
    widget(i1)
    }
    }
    frame.pack()
    frame.show()


    However, I wanted the frame to be saved in local repository rather being displayed for the time being.

    Secondly, I again need help for generating images if there exists more than 1 image parts in an xml response as below:

    <ns1:Label>
    <ns1:Name>Label01</ns1:Parts>
    <ns1:Parts>
    <ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
    <ns1:Image>HOJBAAAElFTkSuQmCC.....</ns1:Image>
    </ns1:Parts>
    </ns1:Label>
    <ns1:Label>
    <ns1:Name>Label02</ns1:Parts>
    <ns1:Parts>
    <ns1:DocumentPartSequenceNumber>1</ns1:DocumentPartSequenceNumber>
    <ns1:Image>dfdJBAAAElFTkSuQmCC.....</ns1:Image>
    </ns1:Parts>
    </ns1:Label>


    I am working on the same. Please to-time help me with groovy script for the above two issues.


    Thanks
    Rohit Borse
  • Hi,

    Its again me here.

    A groovy script for genearting image from base64 string has been created. We can save this image in the desired local repository. Now there need to address the second issue.


    Thanks
    Rohit Borse