Recieving POST data and converting to base64 produces qualityloss

141 Views Asked by At

Im sending in data through post and get a png back in the response. Convert it to base64 so i dont have to save the file. The image is a qr code and it seems to suffer from some quality loss since iPhones cant seem to scan it, androids are fine. Ive tried setting some encoding settings but that didnt do anything.

The data seems to be read correctly but im guessing that the center logo is to choppy to be read by the iphones. Any ideas?

Public Sub updateSwish()

    Dim getPrice As Integer = 100

    Try
        Dim data As String = "{'format':'png','size':300,'message':{'value':'test','editable':false},'amount':{'value':" + Total.ToString + ",'editable':false},'payee':{'value':'123456789','editable':false}}"
        Dim json As JObject = JObject.Parse(data)

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
        Dim s As HttpWebRequest = HttpWebRequest.Create("https://mpc.getswish.net/qrg-swish/api/v1/prefilled")
        Dim enc As UTF8Encoding
        Dim postdata As String
        Dim postdatabytes As Byte()
        enc = New System.Text.UTF8Encoding()
        postdata = json.ToString
        postdatabytes = enc.GetBytes(postdata)
        s.Method = "POST"
        s.ContentType = "application/json"
        s.ContentLength = postdatabytes.Length

        Dim myresponse As HttpWebResponse
        Dim returnval As System.Drawing.Image

        Using stream = s.GetRequestStream()
            stream.Write(postdatabytes, 0, postdatabytes.Length)
        End Using

        Using mStream As New MemoryStream()
            Dim imgByte As Byte()

            myresponse = CType(s.GetResponse(), HttpWebResponse)
            returnval = Image.FromStream(myresponse.GetResponseStream(), True, True)

            returnval.Save(mStream, returnval.RawFormat)
            imgByte = mStream.ToArray()

            Dim base64String As String = Convert.ToBase64String(imgByte, 0, imgByte.Length)
            imgSwish.Src = "data:image/png;base64," & base64String

        End Using

    Catch ex As Exception
        Me.Page.ClientScript.RegisterStartupScript(Me.GetType(), "ex", "alert('" + ex.Message + "');", True)
    End Try

End Sub

EDIT: Turns out that the provider had a v2 coming out due to problems from iphones. The code was fine all along, and the base64 conversion worked as it should. I tried doing the same project i PHP and got the same result.

1

There are 1 best solutions below

1
On

The problem should not the base64 encoding in here. Encodings do not change the data itself, it just represents it in an other format. Like a substitution cipher called ROT13 you can change the representation of your plaintext of

EXAMPLE

to

RKNZCYR

You can play with this behaviour with a lot of online tool like this one. Therefore BASE64 must not be the problem here. (You can also test your generated image before the encode and decode, this will be problematic as well for some products to be read.)

The line where you specify the data

Dim data As String = "{'format':'png','size':300,'message':{'value':'test','editable':false},'amount':{'value':" + Total.ToString + ",'editable':false},'payee':{'value':'123456789','editable':false}}"

seems suspicious. This is quite a lot of characters to be encoded in a QR code if the size variable refers to the pixel size. The minimum size I can read with that size of data is at least 400px*400px sized. What I see from your code it should work if you can increase the size to generate somethig like this image:

enter image description here

TL.DR.: increase the resolution of your QR code for your large data, to allow every platform to read the QR code in a reliable manner or simply change the generator.