Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I scale an image without blurring?

I'm trying to scale a tiny image (something like 20x40 pixels) to an arbitrarily large image. I'm doing the scaling by setting the ImageView.scaleType property, but while it does scale the image, it fades from one original pixel color to the next, creating a large blurry version of the image.

Here's some context for the question. I'm trying to create an old school 16-bit style game for Android. I want to store the sprites/etc as tiny files, and then scale them larger depending on the user's screen size. That way, I don't have to create a ton of different images at different sizes for each sprite. Right now I'm just working on a proof of concept, so I'm trying to scale a tiny 20x40 image file to the size of the entire screen.

*edit: I figured out how to use the method Reflog recommended. However, it still does some fading, just far less than the default algorithm.

*edit2: Got it! I had to turn anti-aliasing off

*edit3: This mysteriously stopped working for me, and I found another place where dithering/auto scaling needed to be disabled. Added the options lines.

@Override
protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setDither(false);
    paint.setAntiAlias(false);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inScaled = false;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tinyimg, options);
    canvas.drawBitmap(bitmap, null, new RectF(getLeft(), getTop(), getRight()/2, getBottom()/2), paint);
}
like image 693
mnemy Avatar asked Apr 05 '11 05:04

mnemy


People also ask

Can you scale up an image without losing quality?

Open your image in Photoshop. Go to the Image Size dialog, check resample, and select "Preserve Details" in the corresponding dropdown menu. Make sure the Resolution is set to 300 Pixels/Inch.

Why is my image blurry when I resize it?

Your image may appear blurry due to a compression issue. Whenever you resize an image, text or graphic, you are also shrinking and enlarging the pixels of that image/text. While you shouldn't expect a loss in quality with minor resizing, any substantial resizing of JPG images will result in a visibly poorer image.


1 Answers

Use:

paint.setDither(false);
g.drawBitmap(img, src, dst, paint);

to use NN scaling, this will reduce blurring, but also will reduce the quality of the scaling.

like image 182
reflog Avatar answered Oct 19 '22 17:10

reflog