having trouble keeping an images quality while compressing > sending to sever (in BLOB format) > retrieving image > decoding BLOB > then setting bitmap.
When i take the picture on the device or choose to upload one it looks alright on the camera but once I send it to the server and get it back later the quality is garbage.
here is the code I am using while compressing the image:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
and decoding:
String pict = getIntent().getExtras().getString("pic");
byte[] encodeByte = Base64.decode(pict, Base64.DEFAULT);
thanks in advanced!
Related
I want to upload my gallery image to localhost database in the form of string from android but i did not found any relevant topic. Please guide me
you can convert you Image bitmap to base64 String by this code :`
`ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
I am creating vCard files from my android app.
I am storing data manually(without using any libray)
I am able to write the data,read and parse it in my app.But when I save Image .I have gor two issues.
1)I am not able to save the image captured using camera..which throws an Out of memoery exception while writing the base 64 encoded string into vcard.
2)I am able to save the base 64 encoded string of some image which I took from gallery,but while reading it doesn't get me image.I am reding all the data from vCard line by line and base64 encoded string is not coming as a single line.(Please note that I stored each value to the file using \r\n)
Please let me know the proper way of doing this.
Code Snippets
Encoding
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
encodedProfileImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
Writing
fw = new FileWriter(vcfFile);
...
fw.write("PHOTO;ENCODING=BASE64;TYPE=PNG:"+encodedProfileImage + "\r\n");
Reading and decoding
else if(strline[0].equals("PHOTO;ENCODING=BASE64;TYPE=PNG")){
String imagestr=strline[1];
byte[] byteArray = Base64.decode(imagestr, Base64.DEFAULT);
card.profileImage = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}
When reading it in you can use BitmapFactory.Options
http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
Use the decodeByteArray call that takes BitmapFactory.Options as the last argument.
if you set inSampleSize = 2
It will reduce the size of the incoming image to something managable.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //Scale it down
options.inPreferredConfig = Bitmap.Config.RBG_565; // Less memory
In my android application, i want to convert a jpeg image to base64 binary encoded value.
How to acheive this in android ?
Edit
I have encoded my bitmap image into base64 string by following code:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
byte[] val = stream.toByteArray();
String ba = Base64.encodeToString(val, Base64.DEFAULT);
The output is in the form of string. BUt I need the output as binary value.
if using KSOAP2 for connectivity use this
new MarshalBase64().register(envelope); this is do the magic.
I want to convert JPEG image to Byte Array in android. I am using the below code:
if (PhotoScreen.st_bitPicture != null)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean b = PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Log.w("test2", "BOOLEAN BOOLEAN BOOLEAN BOOLEAN :"+b);
m_base64EncodedImage = Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
}
But it is compressing the image. How can i do without compressing the image?
When you say "compressing", do you mean "convert it to base 64?". You already have a byte array, and then you encode it in base 64. What is your desired output?
PhotoScreen.st_bitPicture.compress(CompressFormat.JPEG, 100, bos);
Creates an in-memory JPEG image (JPEG compressed).
bos.toByteArray()
Creates a byte array with the JPEG data. That's what you want.
Base64.encodeToString(bos.toByteArray(), Base64.DEFAULT);
Encodes this data to base 64. Just omit that step if you don't want base 64. If you want any other output (like a string with the byte array converted to hexadecimal), then do that instead.
The Base64.encode doesn't want to take the argument "image", and I don't know how to figure out. I've never used Base64 before.
Bitmap bm = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] image = baos.toByteArray();
String encodedImage = Base64.encode(image);
Edit: I use an external package of Base64 http://iharder.sourceforge.net/current/java/base64/
Base64 encode takes at least two arguments. Perhaps try Base64.encode(image, Base64.DEFAULT)