简体   繁体   中英

Cannot properly decode Base64 MIME image to byte array (Java)

I'm trying to write some selenium/java test that checks 2FA configuration process. Thus I have to scan some QR code from a page in order to process it with zxing. The image format is Base64 and I'm struggling with decoding it to the byte array. The following code should convert base64 string to byte array, and then write it to the file.

Here is the code I wrote:

String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[@class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(",")[1];
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);

try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
                stream.write(decoded);
            }

This code compiles with no errors, but when I try to open generated png file I get only "Fatal error reading PNG image file: Decompression error in IDAT". I know that base64 string is valid as I was able to convert it to the image using some online converter. Also, I checked the string with online validator and it said that this is a valid base64 MIME string. Example of the base64 code below:

iVBORw0KGgoAAAANSUhEUgAAAeoAAAHqAQAAAADjFjCXAAAET0lEQVR4nO2dXYrrOgyAP50E5jGB%0AWUCX4uxgljScJd0dxEvpAgacx4KDzoPsxJ3hcqHppadUegiZxB9uQEjWjz2iHJD46wgNjjvuuOOO%0AO+64447fF5ciPbCUi8i4CnEEYBWZAJmWOnS63+yOvygeVFU1AfF0MV3TmU5lWnobob+lB+hUVVWv%0A8YOzO/6i+FLMl3yee2DI9kznzcx9pjK02MR7zu74a+H99wdx7LJAnyUoENJ7FpZ3lXi6yL1nd/w1%0A8R9aB6DxI6EsIxo/LqKQ/5/ZHX9NvGrdoMACwCp11dZlCQmVMK890Cks0OaVn/rbHX8wHkVEZARC%0A6lQ+zz0yATWkfVOCPVsthL3r7I6/GG62rjFfcezQeMooZLuD4SIwZPTa0j36xzv+pDiWBwkJ2NIi%0AJvOgqpo61XnI9e31uPmpv93xR+FUDcuoasZ0babk6wiaG8Xctc4yfK51jt8ku23D4oXdpA3F4Jlz%0AnSlWz+7c1jl+EJdpEYHhIjoDMgGq5x5gFQskPtMqsLypTEO2IX/Hj3f86fDqPrX4VXOpxa6pElJX%0AXli9YvO/buscv132qmrxmqlWWkMqvraEFECJOgb1dZ3jR6TRsNmebIu2Tbk21Su6NmS3dY4fkK0i%0AtgpB154wr6Iso9k1jeNXr1a5GPayWOc9J44fkbZvqfmzU8uc2LN0nUhJnjlx/JDUaGJP1e2BBJbD%0AI9SL5+scvwdeK2LLiIRznyWkUQhp7ZWly7C8F4MXpWtt4l1md/w18epShx91sN3+WeQaUmdRh8ew%0Ajt+pIgatXpWkSb3MUPWvZvNc6xy/Xbb+uq+eeLqIWgfdAqXTBASGhICAeWJdvdPJ8SPS5Ib3Smux%0AfzWugGrr2LtP3NY5frPsHtYammzlBm1tTBM0RbNmEfjU3+74o/A2XxdSUb1duZoti00M4bbO8WOy%0Ax7BNa1MJKaoSliE/1dG1zvGbpGSJm/r+FrTWAkXTbrf7Wrd1jt8uu4e9ustX6zrdfK3WdifXOsdv%0AlxrD0uThml46bdLCuyaCe1jHbxfL10mYFdvszzLaG2XpswCi8aOWwOKYtD0B4Km/3fFH4TVLvIxo%0AFNBy4gRQtv0DLAIMqca6/7zXpqen/nbHH4VfRRNbzcvWdQnM11odVrXuUfQY1vFDslXELqIsfdY4%0AfiEAGk+51zi9bS517Quw9G7rHD8iei0WNGyFryZeLXHtHle4rXP8VmlqE6W+muv2nGHbI7u72bRt%0AmXWtc/wgHmr6RCY6FTmVUxOt8SSc37YlXbnIdMfZHX8xvD3nZC/8A9TMnTabx8qL5PthHb8rvkez%0AZutYRSarza5iuvb75LbO8SPy84TYU0bCWcqpYXF6Uyx8HXJPnAAG35no+BH5flZnTZ+AMHz126md%0AaocTBwWBtb546m93/FH49xi2Pts26qheNRnvuyp8Xef4rSL632P+Xfy/1znuuOOOO+64447/Lfgf%0AFuoX02DU2vMAAAAASUVORK5CYII=

try this

String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[@class='qr-code']")).getAttribute("src");
String base64Image = base64Source.split(";")[1].split(",")[1]; //Try this
byte[] decoded = Base64.getMimeDecoder().decode(base64Image);

try (OutputStream stream = new FileOutputStream("QR_CODE.png")){
                stream.write(decoded);
            }

Okay, so i figured it out, so now it works. The thing is when I run: String base64Source = LocalDriverManager.get().findElement(By.xpath("//img[@class='qr-code']")).getAttribute("src"); it adds a neweline ( %0A ) characters to the string so before decoding it to byte array I need to run qrCodeImage = qrCodeImage.replaceAll("%0A", ""); in order to remove them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM