0

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.

Jimmy
  • 275
  • 2
  • 6
  • 27
  • 2
    IMO, Base64 conversion should have zero effect on image quality. Furthermore, PNG is a lossless format. So if your iPhone cannot scan it, maybe it is due to some other factor like scaling on screen instead of the image being shown in original size (100% zoom). Or maybe the image is too small on a high-res screen with high DPI. Maybe it would help to display a screenshot here, so others can try to scan the QR code with their iOS devices (I only have an Android one). Also, have you tried without the centre logo? – kriegaex Nov 05 '22 at 08:55
  • If you doubt the integrity of your data, create a checksum using a hashing function like MD5 on each end. That said @kriegaex is correct that base64 should have no effect, in fact corruption of the base64 stream could result in an un-decodable output resulting in total corruption, not a quality loss. – Geoffrey Nov 05 '22 at 10:49

1 Answers1

0

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.

Hash
  • 4,647
  • 5
  • 21
  • 39
  • Turns out the developer(swish) had some problems with their outgoing qr-code generator. Your answer did provide with a better resolution image without the blockiness though. If i increased the size to 1000 istead the image would be clear but still not able to be read from iphones. All the data could be contained in the smaller size though. The solution came from the devs, a v2 of the generator. – Jimmy Nov 07 '22 at 11:57