How to resize a base64 image to occupy less? - android

I have an Bitmap image hat i convert to base64, but I want that the image to occupy less because I want to upload that image to a database and it needs to be less than a mb.
Convert from Bitmap to base 64:
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
imagen.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
final String imagenPlanta = Base64.encodeToString(byteArray, Base64.DEFAULT);
Any help would be appreciated.

To make it smaller you have to get rid of information. Do something like JPEG, it will focus on removing information that is less relevant, many times a JPEG image looks just fine but is like 10 times smaller.

Related

Checking if an image is high quality image in android before compression

Currently am compressing my camera images via
public static String getStringImage(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] imageBytes = baos.toByteArray();
return Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
The above compresses images to quality 30 and generates a base64 encoded image for upload to a php server. This works well for high resolution images, but i have an issue with already low resolution images where i would like to check if an image is a low resolution image. How can i check the bitmap passed (before compression) is a high resolution or not so as to adjust the value of quality before compression.
You could just set a threshold for the image size. If the size is above certain threshold, you can compress and upload.

Android - Reduce image file size before upload to server with Base64

I'm trying to reduce the file size from gallery before upload to the server with base64. I've tried ALL the suggestions from stackoverflow & elsewhere I've found on the internet without success.
For images > 2MB, they seem to be reduced in size once written on the server (down to ~500KB). However, for images < 500KB they seem to be bigger than original file size once decoded and written on the server (again ~500KB). It seems like there's threshold that base64 can't get lower beyond). Is it true? Any other way I can reduce the image file size and upload to server programmatically?
private String string(Bitmap bitmap){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
return Base64.encodeToString(stream.toByteArray(), Base64.DEFAULT);
}
Pass bitmap into string method, to convert into String. compressFormat is in PNG which automatically lower the size and quality.

Can I share BitMap as an image (PNG, JPG...) on Facebook, Twitter etc

I need to give the user possibility to share his high score. I took an image (PNG) and wrote the high score on it on it. Can I convert BitMap to PNG and share it without saving the file?
You can use the boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) of the Bitmap class.
ByteArrayOutputStram baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] png = baos.toByteArray();
You will have the byte[] contained the image in png format. You can then use any API you want to share that image.

Compress a 1080p video pngs into small size

I am developing an app which generate .png images from a 1080p video. But the pngs are in MBs, and even my app is crashing due to large size of pngs. I want to compress or something like that to reduce the size of each png. I have done a lot of methods like createScaledBitmap, or compress(CompressFormat.PNG, 20, stream); or Bitmap.createBitmap(source, 0, 0, source.getWidth(),source.getHeight(), m, true); also searched a lot of methods.
But it's not reducing the size as much as I want. it still remains in 2.2+ MB per png.
Any idea other than these. Thanks.
I am also building an app which deals with high resolution images but on server side. I am compressing and re-sizing the image on server side by using thumbnailator-0.4.8.jar. Example
InputStream in = new ByteArrayInputStream(bytes);
BufferedImage bImageFromConvert = ImageIO.read( in );
BufferedImage newImage = Thumbnails.of(bImageFromConvert).size(213, 316).asBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(newImage, "jpg", baos);
baos.flush();
retVal = baos.toByteArray();
baos.close();

Why my Image Quality is not good while sending it to server in My Android App?

This is my code to send Image on Server . my image is going to server but not in good quality.
photo = (Bitmap) data.getExtras().get("data");
image_from_camera.setImageBitmap(photo);
if(photo != null)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
//Here if I increase the size of quality like up to
//photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);
//then i am getting BAD REQUEST ERROR i.e request exceeds size limit.
byte[] byte_arr = stream.toByteArray();
image_string_chore_detail = Base64.encodeToString(byte_arr,Base64.DEFAULT);
System.out.println(
"IMAGE STRING IN CHORE DETAILS:: "+image_string_chore_detail);
}
So please help me what to do....
Use png-32 format for good quailty.
change the format in photoshop and by this you will improve the quailty.
Its because you are reducing the image quality to half the original in this line while compressing,
photo.compress(Bitmap.CompressFormat.JPEG, 50, stream);
The 50 in this line means half. Maybe u have to try it with 100 which will provide you full quality without any loss.
The string is too large for your server to handle. You need to change the settings in the server to allow a larger file.

Categories

Resources