I have an app that needs to resize an image and then save it to jpg. My test image is a photo with very smooth gradients in the sky. I'm trying to save it to jpeg after a resize using this code:
dstBmp = Bitmap.createBitmap(srcBmp, cropX, 0, tWidth, srcBmp.getHeight());
if (android.os.Build.VERSION.SDK_INT > 12) {
Log.w("General", "Calling setHasAlpha");
dstBmp.setHasAlpha(true);
}
dstBmp = Bitmap.createScaledBitmap(dstBmp, scaledSmaller, scaledLarger, true);
OutputStream out = null;
File f = new File(directory + "/f"+x+".jpg");
try {
f.createNewFile();
out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
dstBmp.compress(CompressFormat.JPEG, jpegQuality, out);
The problem is that excessive banding occurs in the gradients of the image unless I bump the quality up to around 95. However, at quality level 95, the resulting file is over 150kb. When I perform these same functions in photoshop and do a "Save for web", I can avoid banding all the way down to quality level 40 with an image size of 50kb. Using ImageCR on my web server I can accomplish the same at 30kb.
Is there any way in Java to compress an image into jpeg more efficiently, or is there a separate library or api I can use to do so? I'm loading a lot of images into memory and at this rate the app is threatening OOM errors on older devices. I'd be happy to allocate more time to image manipulation if that will help the final result.