开发者

android encode image with max size limit

I nee开发者_StackOverflow社区d to encode an image to be JPEG and maximum file zise to be 300 K and send it as byte array. I make encoding :

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
image = baos.toByteArray();

do you know how to limit the file size to 300K? maybe the solution is to make image quality lower, but sometimes(when the image is smaller) I may not need this. Thanks


By repeatedly compressing with descending 'quality' values until the resulting byte[] length <= 300KB. There is no shortcut through this (AFAIK) but you can choose the quality values carefully and limit yourself to, say, a maximum of 4 compressions.


Quick answer is to use a trial and test solution to find a compression value which gives you a value close to less than 300k.

File sizes for JPEG are highly dependent on the detail of the image so unless you're pictures are going to be of the same detail (no all black images etc) then the best compression rating will be different.

Maybe use a binary search style algorithm to find a good solution? Start at 50, then if too big, go down to 25 otherwise go up to 75. Then whenever you're within say 270-300 just stop?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜