Note that there are some explanatory texts on larger screens.

plurals
  1. POBetter option then ImageMagick or Canvas for resizing images in Android
    primarykey
    data
    text
    <p>I trying to achieve good quality image resize in Android. I'm trying all methods listed here on SO and that I could find on Google, but I still can't find a good solution.</p> <p>To exemplify what I'm trying to achieve and what problems I'm having, I'm posting 3 images with the different results. Basically I'm just getting a big image from SD card, resizing and cropping it.</p> <p>Here is the desired result, achieved on Photoshop: <img src="https://i.stack.imgur.com/GXdFu.jpg" alt="resize with photoshop"></p> <p>And this is when I use the tradicional method of drawing on canvas <img src="https://i.stack.imgur.com/uCiXp.jpg" alt="draw on canvas"></p> <p>And this result is when I use ImageMagick. It's better, but in some devices it takes minutes to resize (not cool for a mobile app) <img src="https://i.stack.imgur.com/HzO0Z.jpg" alt="resize with imagemagick"></p> <p>So, here is my code using the canvas method:</p> <pre><code>BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeFile(params[0].path_source, o); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; options.inDither = false; options.inPurgeable = true; options.inScaled = false; options.inPreferQualityOverSpeed = true; options.inSampleSize = Utils.calculateInSampleSize(o, 640, 640); Bitmap image = BitmapFactory.decodeFile(params[0].path_source, options); ... calculate right x, y for cropping .. Matrix m = new Matrix(); m.postScale(new_width/(float)width, new_width/(float)width); m.postRotate(rotation); Bitmap result = Bitmap.createBitmap(640, 640, Config.ARGB_8888); Canvas canvas = new Canvas(result); canvas.setMatrix(m); Paint paint = new Paint(); paint.setAntiAlias(true); paint.setFilterBitmap(true); paint.setDither(true); canvas.drawBitmap(image, -x, -y, paint); FileOutputStream fos = activity.openFileOutput(Utils.NOME_ARQUIVO_FOTO, Context.MODE_PRIVATE); result.compress(CompressFormat.JPEG, 100, fos); fos.close(); </code></pre> <p>Now here is the code using ImageMagick:</p> <pre><code>ImageInfo info = new ImageInfo(params[0].path_source); MagickImage image = new MagickImage(info); //I can optionally use sampleImage for better performance, but worse quality (still better then android canvas) //image = image.sampleImage(new_sampled_width, new_sampled_height); image = image.scaleImage(new_width, new_height); image = image.cropImage(new Rectangle(x, y, 640, 640)); image = image.rotateImage(rotation); byte blob[] = image.imageToBlob(info); FileOutputStream fos = activity.openFileOutput(Utils.NOME_ARQUIVO_FOTO, Context.MODE_PRIVATE); fos.write(blob); fos.close(); </code></pre> <p>Edit: I'm using Android 2.3.4 on a Xperia Play for testing</p> <p>Edit 2: Saving with CompressFormat.PNG achieves near perfect result! Thanks FunkTheMonk for the tip! Only problem is that I use ImageMagick for blending this image with another later in the code, and I couldn't manage to build ImageMagick with PNG support</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload