POST获取PNG转Base64后iPhone无法扫描二维码问题排查
Swish二维码iPhone无法扫描的问题排查与解决
问题背景
- 通过POST请求获取PNG格式的Swish预填充二维码,转换为Base64编码后直接在页面展示(避免本地保存文件)
- 该二维码在Android设备上可正常扫描识别,但iPhone设备无法扫描
- 曾尝试调整编码相关设置,无任何效果,一度推测是二维码中心的Swish logo模糊导致识别失败
- 后续验证:原VB.NET代码及Base64转换逻辑本身无问题,使用PHP复现相同流程也得到同样的iPhone扫描失败结果
相关VB.NET代码
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
解决方法
问题根源在于Swish的v1 API生成的二维码存在iPhone扫描兼容性问题,服务商已针对该问题推出了v2版本API。只需将请求的API端点从v1更换为v2即可解决iPhone无法扫描的问题,无需修改原有的Base64转换或图片处理逻辑。
内容的提问来源于stack exchange,提问作者Jimmy
相关产品推荐
相关产品推荐

