Base64 encoded image is different from what was saved - java

I use ImageIO to save Base64 image string to a file:
public static String decodeBase64AndSaveImage(String data) throws IOException, BadRequestException {
try {
if (data == null) {
return null;
}
String base64Image = data.split(",")[1];
byte[] imageBytes = DatatypeConverter.parseBase64Binary(base64Image);
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
String imageId = generateRandomKey(15);
ImageIO.write(img, "png", getImageFile(imageId));
return imageId;
} catch (IllegalArgumentException e) {
throw new BadRequestException("Bad image data");
}
}
I pass the following string to this method:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAADklEQVQIW2NgQAXGZHAAGioAza6+Hk0AAAAASUVORK5CYII=
In the following test I check that the if the saved content to the file is the same as what was passed to the method:
byte[] base64ImageData = Base64.encodeBase64(FileUtils.readFileToByteArray(imageFile));
Assert.assertEquals("wrong image data stored", DUMMY_IMAGE
.split(",")[1], new String(base64ImageData,
StandardCharsets.UTF_8));
But it returns a different string:
iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAADklEQVR42mNgQAXGZHAAGioAzTktUZoAAAAASUVORK5CYII=
Why does this happen ?
Note 1
It doesn't compare data:image/png;base64, so it's not the reason that it fails.

You are base64 decoding the string, using ImageIO to create a BufferedImage, converting it back to a PNG, and re-base64 encoding this result.
byte[] imageBytes = DatatypeConverter.parseBase64Binary(base64Image);
BufferedImage img = ImageIO.read(new
ByteArrayInputStream(imageBytes));
String imageId = generateRandomKey(15);
// re-encode
ImageIO.write(img, "png", getImageFile(imageId));
This all works fine. But the image encoder is encoding the image in a slightly different way to the original. They are both though valid PNG files and both valid base64 strings.
I decoded both strings and found they produced valid PNG files which look identical, but were encoded in different ways.

Related

Trying to display image retrived from server with base64 encoding but cant display the correct image

These are my encoding and decoding methods.
public String encodeBase64(Bitmap bit){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bit.compress(Bitmap.CompressFormat.PNG, 100, baos); // Could be Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP
byte[] bai = baos.toByteArray();
return Base64.encodeToString(bai, Base64.DEFAULT);
}
public Bitmap decodeBase64(String base64Image){
byte[] data = Base64.decode(base64Image, Base64.DEFAULT);
Bitmap bm;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
bm = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
return bm;
}
This is encoded data from a bitmap
iVBORw0KGgoAAAANSUhEUgAABLAAAAdcCAYAAAC7T3KrAAAABHNCSVQICAgIfAhkiAAAIABJREFU
eJzswQEBAAAAgJD+r+4ICgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.... etc
But the data i get from the server looks like this
{"type":"Buffer","data":[105,86,66,79,82,119,48,75,71,103,111,65,65,65,... etc
I can get pure data or the type and data json object. Ive tried to decode both. When i try to decode with the type and data it gives me a null bitmap, when i decode the raw data only the image is not displayed, but nothing seems to crash.
Do i need to retrive all those array numbers and convert them to chars before decoding? Ive read other post and it sounded very straigtforward, just encode and then decode.
decode base_64 string Imgae in codigneter
$
aadharcard_front = image name(database)
if(!empty($aadharcard_front))
{
$aadharcard_front = base64_decode($aadharcard_front);
$aadharcard_front_time = time().'.png';
file_put_contents(APPPATH.'../media/uploads/'.$aadharcard_front_time,$aadharcard_front);
$user_data['aadharcard_front'] = $aadharcard_front_time;
}

Show image from Data URI scheme on Java panel

I am having trouble interpreting this question.
I receive a img in JSON formated in Base64, for web developers its only do:
"".
How I can make it in Java?
You can use apache commons-codec for converting byte array. to base64 and from base64 to byte array.
Actually, you can use guava. This artifact has base64 libraries and json.
Just add com.google.guava in your maven's project.
For creating image, you can use:
InputStream in = new ByteArrayInputStream(yourbytearray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "jpg", new File("c:/yourimage.jpg"));
//JSON funtion
String cmd_getPhoto = so.cmd_getPhoto();
//for remove ":"data:image\/png;base64,"
String imageDataBytes = cmd_getPhoto.substring(cmd_getPhoto.indexOf(",") + 1);
//for decode
Base64 b = new Base64();
byte[] decode = b.decode(imageDataBytes.getBytes());
//create the stream
InputStream stream = new ByteArrayInputStream(decode);
try {
//set the stream for a bufferedImage and do what your will with it
BufferedImage bitmap = ImageIO.read(stream);
jLabel6.setIcon(new ImageIcon(bitmap));
} catch (IOException ex) { }

Java - GIF to base64 string

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.

Null returned from ImageIO.read(new ByteArrayInputStream(bs));

I am trying to convert Base64 encoded string (from image) [link to convertor]. Here is how I do this: First I convert the string, which is:
String str =
/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxQHBhMTEhQWFBUUGBUZGRYXFxgXGBQWFRQYGBQVFxgaISogGBomGxUXITEiJSkrLi4uFx8zODMsNygtLisBCgoKDgwMFw8PFywcFB4sLCwsNyssLDcrLCwsKzcsNzc3NzcsKywsKysrLCsrKywsNysrLCsrKyssKysrKysrK//AABEIALEBHQMBIgACEQEDEQH/xAAbAAEAAgMBAQAAAAAAAAAAAAAABAUCAwYBB//EADcQAAIBAwEFBQcEAgIDAQAAAAABAgMEERIFITFBURMiMnGhFBU0UmGRsXKBweHR8EJDM2KCBv/EABQBAQAAAAAAAAAAAAAAAAAAAAD/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwD7iAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA4ARb29jaR372+C/3gVU9rVJPdhft/ki3NV167k+b9OSLe22VGNPv736L6AQPelTqvsh70qdV9kWvuyn8vqx7sp/L6sDLZ1y7m3y+KePMlGNOmqUMRWEjIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHk/Az08ksoDlIf8AkXn/ACdYc5Cyn7So6Xx443ceOTowAAAAEDat27amlHxP0QE8HNe31PnY9vqfOwOlBza2hUT8T9DoaMtdJN7m0mBmARr68VpT6t8F/PkBJBQWt9Uldre3l8OW8vwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKC92jKtUai2o8sc/qyJ28vml92B1LeEcze1/aLhy5cvJcDB15NeJ/dkjZlv7Rcrot7/hAWVjs+MbZaopt79/LPIkew0/kX2JAA0RsqcXnQvsbwc9tCvNXck21h7lnG7kBb314rSn1b4L+X9CglKVzW6yZhKTk8t58xGbg8p4f0Av9n2KtY5e+T59PoiaVGyr9zq6JvOeD5+TLcAAAAAAAAAAAAAAAAAAAAAAAA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y/uTQBE2dZewU5RTzHU3FPLcU97WW9+/L/cxtdnRttoVaqe+pjdjw4Xex5vDfkTQBW3Wx4XtzOVRt6oqKSlKKit+c4fey3z6IlU7drZ6pylqejS5Y47samupIAEGrs/tNjdhq/wCtQ1Y6RxnGf5NtpCrBvtJwl00wccdc5k8kkAVFbYUaznJyl2kpalNOSUXHGjuasPGFxJm0bR3dvFKSjKMoSTaysweeGVu3dSWAIdjZyoVpznPXOelNqOmKUM6Ull/M+L5mm0sqtrWeKkNEpzm49m9Xfk5NatX144LIAQb2znVvIVKc4wcYyj3oOSak4vlJY8JKt4yjRSm1KW/LUdK47t2Xy+psAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH//2Q==
like this:
byte[] imageString = str.getBytes("UTF-8");
and then I want to save the file on machine like this:
public boolean uploadImage(byte[] imageString, String name, String extension)
{
BufferedImage image = null;
String path = EVENT_FOLDER + name + "." + extension;
try
{
image = ImageIO.read(new ByteArrayInputStream(imageString)); //null returned here
ImageIO.write(image, extension, new File(path));
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
But I am getting null on this line: image = ImageIO.read(new ByteArrayInputStream(imageString));
What did I do wrong??
What did I do wrong?
You ignored the fact that the data is Base64-encoded, not UTF-8.
To convert the string to bytes to get the original data, you need to reverse that Base64 encoding, for example using this library.
byte[] imageData = Base64.decode(str);

How do i convert a image as a string to PNG file

I'm using a jQuery plugin called wPaint to allow users to draw their own image. I send the resulting image as a string to the server as a string which begins with
data:image/png;base64,
I tried the two approaches below but with both the approaches i'm not able to store the image.
Approach 1
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
FileOutputStream fileOutputStream = new FileOutputStream("/home/arvind/Desktop/test.png");
fileOutputStream.write(imgByteArray);
fileOutputStream.close();
In this case the file is written but doesn't show the image. However, when i remove the file extension i get the string that was sent to the server (i.e. whatever is in imageData).
Approach 2
String imageData = parameterParser.getStringParameter("image", "");
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
InputStream in = new ByteArrayInputStream(imgByteArray);
BufferedImage bImageFromConvert = ImageIO.read(in);
ImageIO.write(bImageFromConvert, "png", new File("/home/arvind/Desktop/test.png"));
The BufferedImage bImageFromConvert is null so I get an exception (IllegalArgumentException) when the file is created.
The Base64 class is from the apache commons codec library and is version 1.2.
Is there something I'm doing wrong?
Initially I had sent the data to the server using the following code.
$.ajax({
url : '/campaign/holiImageUpload.action',
type : 'POST',
data : "image=" + $("#wPaint2").wPaint("image")
success :function(data){
}
});
Now i'm sending the data to the server using the following code
var imgData = $("#wPaint2").wPaint("image");
$.ajax({
url : '/campaign/holiImageUpload.action',
type : 'POST',
data : {image : imgData},
success :function(data){
}
});
In the server side this is the final code :
String imageData = parameterParser.getStringParameter("image", "");
try {
imageData = imageData.substring(22);
byte[] imgByteArray = Base64.decodeBase64(imageData.getBytes());
InputStream in = new ByteArrayInputStream(imgByteArray);
BufferedImage bufferedImage = ImageIO.read(in);
ImageIO.write(bufferedImage, "png", new File("/home/arvind/Desktop/test.png"));
catch(Exception ex){
ex.printStrackTrace();
}
It appears that you are trying to decode data:image/png'base64 along with your Base64 encoded data? You will want to remove that from the input string before you decode the Base64 data into image bytes.
Also, you don't want to decode the string as bytes... just as a string.

Categories

Resources