Reputation: 1906
I have an java application allows user to take a photo from his camera and send it to me using a web service, But my problem is while sending the image. The send progress takes long time because image is big so i want to compress image. I have tried to:
1- Use this code:
Bitmap img = BitmapFactory.decodeFile("C:\\test.jpg");
ByteArrayOutputStream streem = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 75, streem);
byte[] b = streem.toByteArray();
But this code is useless in my case because it make image very bad and dosent affect on image size a lot.
2- Search a lot about ways to resize but all results is using BufferedImage. I cant use this type(Class) because it takes a lot of memory size:
private static BufferedImage resizeImage(BufferedImage originalImage, int type)
{
BufferedImage resizedImage = new BufferedImage(new_w, new_h, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, new_w, new_h, null);
g.dispose();
return resizedImage;
}
I want to use Bitmap instead, Any body can help me in my application???
Upvotes: 1
Views: 1438
Reputation: 1906
I have found these tow method:
private static int CalculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
float height = (float) options.outHeight;
float width = (float) options.outWidth;
float inSampleSize = 0;
if (height > reqHeight || width > reqWidth) {
inSampleSize = width > height ? height / reqHeight : width
/ reqWidth;
}
return (int) Math.round(inSampleSize);
}
public static byte[] ResizeImage(int reqWidth, int reqHeight, byte[] buffer) {
BitmapFactory.Options op = new Options();
op.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(buffer, 0, buffer.length, op);
op.inSampleSize = CalculateInSampleSize(op, reqWidth, reqHeight);
op.inJustDecodeBounds = false;
try {
return ToByte(BitmapFactory.decodeByteArray(buffer, 0,
buffer.length, op));
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Upvotes: 1
Reputation: 347334
Use ImageIO
to write the BufferedImage
to the format you need
You can supply a output stream to ImageIO
so you should be able to write to just about anywhere.
Check out Writing/Saving an Image for more details
Upvotes: 1