1

I am trying to convert an image to string using Base64 Android class in an Android app and I succeeded in getting the string.

But when I checked the length of string for an image of size 86.13 kB (using String.length) is almost 85660. After Googling I came to know that these 2 sizes are almost same. That means though I convert image to string no data reduction happens.

My doubts are: 1) Is the both size same? 2)Is there has any another way to convert image to string that so I will data reduced.

The code I used for converting Image to String is

ByteArrayOutputStream baos=new  ByteArrayOutputStream();
imageSelected.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);

Guys please help. Thanks a ton in advance

AR5
  • 41
  • 7
  • You are using `(Bitmap.CompressFormat.PNG,100, baos)` 100% compress if you reduce that then it will also reduce the `String` size. – Harshad Pansuriya Dec 02 '16 at 08:23
  • Ohk... will try reducing compression %. Thanks for your comments – AR5 Dec 02 '16 at 08:24
  • It is pretty unclear what you consider to be the size of an imags. You did nof show where you got the 86.13 KB from. – greenapps Dec 02 '16 at 08:34
  • @greenapps using this code `File file = new File("/sdcard/imageName.jpeg"); long length = file.length(); length = length/1024;` – Harshad Pansuriya Dec 02 '16 at 08:36
  • So you compare a jpg file length with the length of a base64 encoded png file. That is a strange comparison. By the way: why are you converting jpg to png? – greenapps Dec 02 '16 at 08:50
  • `imageSelected` . That is a bitmap. You are not showing what it has to do with that jpeg. You have pretty incomplede code. – greenapps Dec 02 '16 at 08:53
  • `After Googling I came to know that these 2 sizes are almost same.` ??? What do you mean by that? You need Google to see that 86.13 kB and 85660 are about the same? Your figures do not make sense. As a jpeg file length would be much less then a base64 encoded png file length – greenapps Dec 02 '16 at 08:56
  • @greenapps as Ironman mentioned I got size of image by File file = new File("/sdcard/imageName.jpeg"); long length = file.length(); length = length/1024; My ultimate aim is ' If I have an image I need to convert it into string expecting that the size of converted string will be less than the size of the image'. – AR5 Dec 02 '16 at 08:58
  • But it seems that you should have said "If I have a file I need to encode it into a string expecting that the size of encoded string will be less than the size of the file'. Well base64 encoding increases the amount of bytes with 30%. – greenapps Dec 02 '16 at 09:00
  • @greenapps String.length will return integer length and not as kB/MB. But file size we usually specify as kB/MB. I wanted Googles help to convert size in integer to size in kB/MB – AR5 Dec 02 '16 at 09:04
  • Shame on you that you do not see that 86.13 kB and 85660 are the same. My god have you learned counting? – greenapps Dec 02 '16 at 09:05
  • @greenapps As you said 'base64 encoding increases the amount of bytes with 30%' thats what I am asking is there has any other way to convert so that the size will get reduced – AR5 Dec 02 '16 at 09:06
  • You are even trying to convert a jpg to png. That will also increase the amount of bytes. – greenapps Dec 02 '16 at 09:07
  • If you start with a jpg then there is little chanche to get less bytes by compressing/zipping as jpg is already maximally compresed. – greenapps Dec 02 '16 at 09:08
  • @greenapps is Stackoverflow a site to measure my knowledge or to get help on my doubts? If u r not interested to help then leave it... Many others will be here who are willing to help – AR5 Dec 02 '16 at 09:09
  • ???? How much help did i give you already? A lot! Strange that you think i'm not willing to help. Where does that come from? What bothers you? – greenapps Dec 02 '16 at 09:10

2 Answers2

2

First off, converting an image to a base64 string will INCREASE the size, not decrease it. This is because a base 64 string will use multiple characters per byte, which means multiple bytes in the string per byte in the image.

Secondly, you're comparing the wrong things. YOu have a JPEG file on the device. You open it, then compress it to PNG- which will likely increase the size as PNG is lossless compression not lossy. You're then base64 encoding that png, and comparing the size to a jpeg on disk? That makes no sense at all. If you want to compare, compare the size of the base64 string in bytes to the length of the output stream- which will show it about 2x as big, see point 1.

This is why Base64 should not normally be used. Its a hack to turn binary data into text data for non-binary storage mechanisms and should only be used if absolutely necessary- such as for non-multipart HTTP requests with binary data. It shouldn't normally be used for storage and is NOT a compression mechanism.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I understood ur points. But is there has any alternative that can be used instead of Base64? I will try comparing the size as u mentioned. Thanks – AR5 Dec 02 '16 at 09:36
  • To concert to a string and shrink it? No. A binary format will always be smaller than a text format for the same data – Gabe Sechan Dec 02 '16 at 14:20
0

You are confusing data conversion and data compression. Converting 'image' to a String is just a conversion (translation of data to another form) and not compression. If you want to cut the size of an image, you have to compress it first. You can find out more about it here: How to compress image size? or you can have fun with this imageSelected.compress method (change CompressFormat to JPEG, change second integer to lower value than 100 etc. until you get the desired effect)

Community
  • 1
  • 1
  • Thanks @prezmyslaw for ur comments. Will try it out – AR5 Dec 02 '16 at 09:23
  • And I don't want to compress an image where the result will also be an image rather I want to convert the image to string format with less size – AR5 Dec 02 '16 at 09:24