I have a Data-URL from an image file and have to pass it through to another function. Along this path from Data-URL to the BufferedImage it needs to be a byteArray.
my approach was the following:
String dataUrl;
byte[] imageData = dataUrl.getBytes();
// pass the byteArray along the path
// create BufferedImage from byteArray
BufferedImage inputImage = ImageIO.read(new ByteArrayInputStream(imageData));
// If the picture is null, then throw an unsupported image exception.
if (inputImage == null) {
throw new UnknownImageFormatException();
}
The problem is, it always throws the UnknownImageFormatException Exception, which means inputImage is null, which means, the ImageIO.read did not recognize the imagetype.
I've used ImageIO.getReaderFormatNames() to get the supported Filenames and got the following list:
Supported Formats:
jpg, BMP, bmp, JPG, jpeg, wbmp, png, JPEG, PNG, WBMP, GIF, gif
The dataURLs I try to pass are like: data:image/png;base64,... or data:image/jpg;base64,...
As far as I understand, those are in the supported filelist and therefor should be recognized.
What else could cause the inputImage to be null in this case? And more interesting, how do I solve it?
As the comments already said the image data is Base64 encoded. To retrieve the binary data you have to strip the type/encoding headers, then decode the Base64 content to binary data.
String encodingPrefix = "base64,";
int contentStartIndex = dataUrl.indexOf(encodingPrefix) + encodingPrefix.length();
byte[] imageData = Base64.decodeBase64(dataUrl.substring(contentStartIndex));
I use org.apache.commons.codec.binary.Base64 from apaches common-codec, other Base64 decoders should work as well.
The only one problem with RFC2397 string is its specification with everything before data but data: and , optional:
data:[<mediatype>][;base64],<data>
So pure Java 8 solution accounting this would be:
final int dataStartIndex = dataUrl.indexOf(",") + 1;
final String data = dataUrl.substring(dataStartIndex);
byte[] decoded = java.util.Base64.getDecoder().decode(data);
Of course dataStartIndex should be checked.
I think, a simple regex replace would be better and more conform to the RFC2397:
java.util.Base64.getDecoder().decode(b64DataString.replaceFirst("data:.+,", ""))
The RFC states that the data: and the , are the required prefixes for a data url, therefore it is wise to match for them.
Related
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.
I am very new to image encoding and would rather not learn a whole lot about it. Basically I'm taking greyscale byte array where each byte equals one pixel. I'm getting this data from mnist where I get 28x28 byte images. Anyway, bellow is my code, so you understand what I'm trying to accomplish.
private def getImages = {
val filePath = getClass.getResource("/mnist/train-images.idx3-ubyte").getPath
val fis = new FileInputStream(filePath)
var bytes = new Array[Byte](4)
fis.read(bytes)
println((ByteBuffer.wrap(bytes).getInt()))
fis.read(bytes)
println((ByteBuffer.wrap(bytes).getInt()))
fis.read(bytes)
var rows = ByteBuffer.wrap(bytes).getInt()
println("Number of rows: " + rows)
fis.read(bytes)
var cols = ByteBuffer.wrap(bytes).getInt()
println("Number of cols: " + cols)
var imageBytes = new Array[Byte](rows * cols)
fis.read(imageBytes)
imageBytes.foreach(println(_))
// I created a byte array input stream to feed into ImageIO
// which should create my image
val b = new ByteArrayInputStream(imageBytes)
// This is where your helpful answer would be placed
// What is the code to encode this into jpeg, gif, or whatever?
// This returns null because I have not encoded the bytes
// in the proper format
val img = ImageIO.read(b)
// Errors out because img is null
ImageIO.write(img, "gif", new File("/home/dev/woot.gif"))
}
The format is just consecutive pixel bytes laid next to each other. My question is what Java library or function is available to convert these raw bytes into jpeg, gif, or whatever format I need?
Before you write it out with ImageIO, create a BufferedImage first. It can be as simple as using the setRGB methods, and has the added benefit of allowing you to observe the image before writing it out.
how to convert gif to base64?
here's what i try so far
public static String convertImageToBase64(String urlString, String type ){
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
URL url = new URL(urlString);
BufferedImage img = ImageIO.read( url );
ImageIO.write(img, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
code above is working, but it lost the animation
Most likely the class BufferedImage does not support images with animation. So the code opens and parses the image only to the first frame.
Instead try directly getting the bytes with URL.openStream and then just convert the downloaded bytes to base64.
Notice that this way you can't be sure that the downloaded file is actually an image since you are not opening it at all. This may or may not be needed.
You have to use
public String encodeToString(byte[] src)
of class BASE64.Encoder (from java 8)
I'm assuming what you want is a Base64 representation of the GIF file itself, not the ImageIO representation. If you have an older version of Java without built-in Base64 support, Apache Commons Codec will do the job. Read the file into a byte array using URL.openStream(), not with ImageIO, then call Base64.encodeBase64String. If you want the result URL encoded, call Base64.encodeBase64URLSafe instead.
If you actually want the ImageIO representation, then you're stuck with losing the animation.
I have inputStream Object when i try to convert in bitmap it works but for some images it will return me null still, image is present.
InputStream responseInputStream = apiResponseModel
.getResponseInputStream();
if (responseInputStream != null) {
Bitmap bmp = BitmapFactory.decodeStream(responseInputStream);
imgProductProfile.setImageBitmap(bmp);
}
The image you are trying to decode may not be supported for decoding. Android supports only few image types for decoding. The supported format is listed here
http://developer.android.com/guide/appendix/media-formats.html. Try converting your image to other format like PNG , JPG and try decoding
Basically, what I'm doing is converting an image into a byte array, processing it, then after obtaining the byte array, converting it back to an image. Here is how I convert it the byte array to an image.
InputStream in = new ByteArrayInputStream(result); //result is the byte array
BufferedImage bImageFromConvert;
try {
bImageFromConvert = ImageIO.read(in);
ImageIO.write(
bImageFromConvert, watermark_ext, new File(extracted_name_path));
} catch (Exception e) {
return e.getMessage();
}
Now this code works perfectly for PNG or JPG images, however when I use it for BMP images, it returns an exception that says bImageFromConvert is null. Can anyone please help me know why it does that? Thanks everyone.
The answer is in the Javadoc:
Returns a BufferedImage as the result of decoding a supplied
ImageInputStream with an ImageReader chosen automatically from among
those currently registered. If no registered ImageReader claims to be
able to read the stream, null is returned.
This previous post on SO is more detailed.
The method javax.imageio.ImageIO.getImageReadersBySuffix() may be useful for you.