Android Image to Base64 - java

I want to convert my app images to base64 so it won't show on the gallery. I have tried various techniques. The image is from a zip file so it is a byte array at that point. The length of the base64 change when I change the byte array size. What is the proper byte array size? And the base64 encoded image doesn't work too.
The primary code is
String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT);
buffer is the byte array(102400)
and it contains the image too.
The image is a 7KB file and the output is 400KB

You may try following function to convert image into Base64:
public void toStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
Above function takes Bitmap image and converts it into Base64 encoded string. This is working in my project and I hope this will help you too.

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;
}

Why can't I decode Android encoding image using base64String with an other decoder?

I have an image that I encode using this code :
Bitmap bm = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
It encodes well. After that I'm getting the String in a file in my computer and I try to decode it with this website :
http://www.askapache.com/online-tools/base64-image-converter/
And I'm getting a null picture. However, when I decode it directly in my app and display it like :
public static Bitmap decodeBase64(String input) {
byte[] decodedByte = Base64.decode(input, 0);
return BitmapFactory
.decodeByteArray(decodedByte, 0, decodedByte.length);
}
private ImageView img;
img.setImageBitmap(decodeBase64(encodedImage));
I'm getting the right picture. So I was wondering if i actually can't decode an android encoded image with an other decoder than android's one or my file is corrupted meaning that I have missing characters. However even if I don't have the whole encoded file, the decoded picture i'm getting with the website should look like to the good one right ?
Thank you in advance

encoded image in base64 back to image

so I am trying to take a string for an image I encoded using base64 and turn it back into an image I can use in an ImageView. The code to encode it is:
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), options);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
image_str = Base64.encodeToString(b, Base64.DEFAULT);
I'm assuming it would be convert image_str back to a byteArray then back to bitmap?
I am not very familiar with base64 functions so I figured I would ask here while I search, to get more done in the same amount of time.
Thank you in advance,
Tyler
EDIT: I did find this bit of code but the image does not show up and logcat says decode returned false:
byte[] imageBytes=Base64.decode(imageString,Base64.NO_WRAP);
InputStream in = new ByteArrayInputStream(imageBytes);
Bitmap b = BitmapFactory.decodeStream(in);
You'd first decode the Base64 encoded string to bytes:
byte[] decodedBytes = Base64.decode(image_str, Base64.DEFAULT);
Then convert the bytes back to a JPG:
Bitmap bm = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);

Java Convert String to byte[]

I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
I guess this is a pretty common issue so i guess someone could give any suggestions.
If it is better to convert an JSONArray into an byte array i would like to know why.
I wonder how you should do if you are going to convert an String that contains an array of bytes, actually bytes from an image into an byte array.
It depends on how you've constructed the string in the first place. If something somewhere has used
new String(imageData)
then you've already lost information, potentially. Image data isn't text.
To propagate opaque binary data in text, you should use base64 (or potentially hex). Android already has a Base64 class that makes this easy:
String text = Base64.encodeToString(originalBytes, Base64.DEFAULT);
...
byte[] roundTrip = Base64.decode(text, Base64.DEFAULT);
EDIT: If you can't use that class, this public domain base64 encoder should work fine.
Byte[] to String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Your_Bitmap.compress(CompressFormat.PNG, 0 , baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String bytetostring = Base64.encodeToString(b, Base64.DEFAULT);
return bytetostring ;
String to Byte[]
byte[] data = Base64.decode("Your string", Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

How to convert image to byte array and convert back byte array to image

I am trying to convert an image to byte array and converting back byte array to image in Android Emulator.
First part is working fine but the second part is not creating the image file in Android emulator.
Please suggest me if there is any correction in my second part of the code.
Following is my code.
public String GetQRCode() throws FileNotFoundException, IOException {
/*
* In this function the first part shows how to convert an image file to
* byte array. The second part of the code shows how to change byte array
* back to an image.
*/
Bitmap bitmap = BitmapFactory.decodeFile("sdcard/Download/QR.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] byte_img_data = baos.toByteArray();
byte[] buf = new byte[200];
// Second Part: Convert byte array back to an image
Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, 200);
ByteArrayOutputStream img= new ByteArrayOutputStream();
Bitmap imageFile= BitmapFactory.decodeFile("sdcard/Download/QR3.jpg");
String abc = buf.toString();
return abc;
}
your call to BitmapFactory.decodeByteArray(..) - this method returns a Bitmap object, but you're not assigning it to anything. You also need to change the call to pass in the actual length of byte_img_data, rather then 200.
Bitmap bitmap2 = BitmapFactory.decodeByteArray(byte_img_data, 0, byte_img_data.length);
However, whether decodeByteArray(..) can handle compressed streams, i don't know

Categories

Resources