无法从blob url扫描二维码

rjzwgtxy  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(322)

在谷歌搜索了好几个小时后,我想不出一个解决方案,如何在基于java的selenium测试中扫描二维码。我会列出我尝试过的事情和我犯过的错误。
使用zxing库。这个库对于blob URL似乎根本不起作用。

private String decodeQRCode(URL qrCodeImage) throws IOException, NotFoundException {
    BufferedImage bufferedImage = ImageIO.read(qrCodeImage);
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Result result = new MultiFormatReader().decode(bitmap);
    return result.getText(); 
}

尝试此操作时出错:

unknown protocol: blob
java.net.MalformedURLException: unknown protocol: blob
        at java.net.URL.<init>(URL.java:617)
        at java.net.URL.<init>(URL.java:507)
        at java.net.URL.<init>(URL.java:456)

使用zxing库,但使用base64而不是url。

private String decodeQRCode(String qrCodeImage) throws IOException, NotFoundException {
    byte[] decoded = Base64.decodeBase64(qrCodeImage);
    BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(decoded));
    LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
    BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

    Result result = new MultiFormatReader().decode(bitmap);
    return result.getText();
}

使用此选项时出错:

null
java.lang.NullPointerException
        at com.google.zxing.client.j2se.BufferedImageLuminanceSource.<init>(BufferedImageLuminanceSource.java:42)

尝试了上述两种方法,但这次从url的开头删除了“blob:”,仍然得到了与上述情况相同的nullpointerexception。
还使用executeasyncscript()函数尝试了javascript注入。

private String getBytesBase64FromBlobURI(String uri) {
    String script = " "
         + "var uri = arguments[0];"
         + "var callback = arguments[1];"
         + "var toBase64 = function(buffer){for(var r,n=new Uint8Array(buffer),t=n.length,a=new Uint8Array(4*Math.ceil(t/3)),i=new Uint8Array(64),o=0,c=0;64>c;++c)i[c]='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.charCodeAt(c);for(c=0;t-t%3>c;c+=3,o+=4)r=n[c]<<16|n[c+1]<<8|n[c+2],a[o]=i[r>>18],a[o+1]=i[r>>12&63],a[o+2]=i[r>>6&63],a[o+3]=i[63&r];return t%3===1?(r=n[t-1],a[o]=i[r>>2],a[o+1]=i[r<<4&63],a[o+2]=61,a[o+3]=61):t%3===2&&(r=(n[t-2]<<8)+n[t-1],a[o]=i[r>>10],a[o+1]=i[r>>4&63],a[o+2]=i[r<<2&63],a[o+3]=61),new TextDecoder('ascii').decode(a)};"
         + "var xhr = new XMLHttpRequest();"
         + "xhr.responseType = 'arraybuffer';"
         + "xhr.onload = function(){ callback(toBase64(xhr.response)) };"
         + "xhr.onerror = function(){ callback(xhr.status) };"
         + "xhr.open('GET','"+ uri +"');"
         + "xhr.send();";

    JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
    String result = (jsExecutor.executeAsyncScript(script, uri)).toString();
    return result;
 }

此方法没有得到任何错误,但我得到的值不正确。
从移动设备扫描二维码时的预期值: ecfe09ff-ca31-4e16-9550-82da38a45966 运行此代码后的值: iVBORw0KGgoAAAANSUhEUgAAAMgAAADIAQAAAACFI5MzAAABfElEQVR4Xu2XQWrDQAxFFbyYpY/gm9gXCziQi9U3mSPMchbG6v8aSOK0Xfa7BQsMkZ8Xn9GXNDH/Kez9xSNOchLG3yDZzDqvo10HX/yGrFcTPPdcp2JDHfu7+yYn1SK3OZNkGw8hzHEwBxJPzvTihxBnfdJHvw4ozdfKCQg92kpT44j27hWQFnEwTVbLhQTaoCgtvsIhaFb8UpNqyDEpOsfLqJScjKwPJoXFwAq3SoljRsKoGwVO/PXQJiNQZEMbEjwnm/hSShJkNZG+lO5Vm4hk99I5cnoUbm3faMlSroY+vcEhPSv11KYh3jbVBX2aljJzZohJTl7maBWvIfAxLWWkXuALYBuQrbsOFpFoUQ5KyML14QASZxL2RL9gYE1qEpFjPrA0L/NaRTihOl5gVpoDy5vfaAmee9iTdxdoe25NFQlF3Nu0yb5ySmJcndxevtvoQhL/JCyW93NWqYjzDhsXp1gbIVBLwqNp4xFNKNK7e3+ffB8nOQnjf5JPtWpjdkLLguMAAAAASUVORK5CYII= 请帮忙。我到处都找遍了,找不到一个读取二维码的解决方案。任何帮助都将不胜感激。非常感谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题