Reputation: 6403
I have a list of bitmaps. What I want is to take these bitmaps and, with the help of canvas, create a new bitmap with scaled down images (meaning, make them quite tiny) from the list I have of bitmaps.
I've manage to do this but, the image looks quite horrible due to the down-scaling. I've tried many things, settings, creating new canvases etc.
The simple first solution looks like this (code below), but, as I said, the images looks awful.
public static Bitmap folderBitmap(Bitmap bitmap[]) {
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.RGB_565);
Canvas c = new Canvas(b);
c.drawARGB(255, 255, 255, 255);
Paint paint = new Paint();
paint.setAntiAlias(false);
paint.setFilterBitmap(false);
paint.setDither(true);
c.drawBitmap(getBit(bitmap), 4, 4, paint);
c.drawBitmap(getBit(bitmap), 35, 4, paint);
c.drawBitmap(getBit(bitmap), 67, 4, paint);
c.drawBitmap(getBit(bitmap), 4, 35, null);
c.drawBitmap(getBit(bitmap), 35, 35, null);
c.drawBitmap(getBit(bitmap), 67, 35, null);
c.drawBitmap(getBit(bitmap), 4, 67, null);
c.drawBitmap(getBit(bitmap), 35, 67, null);
c.drawBitmap(getBit(bitmap), 67, 67, null);
return b;
}
private static Bitmap getBit(Bitmap[] b) {
Bitmap newBitmap = Bitmap.createScaledBitmap(b[getR()], 28, 28, false);
return newBitmap;
}
private static int getR() {
Random r = new Random();
int rint = r.nextInt(8);
return rint;
}
By awful I mean, they look pixelated and un-sharp.
Upvotes: 2
Views: 5100
Reputation: 3309
Using your code, if you want to increase quality according to documentation for each of these settings you should:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
: Each pixel is stored on 4 bytes. Each channel (RGB and alpha for translucency) is stored with 8 bits of precision (256 possible values.) This configuration is very flexible and offers the best
quality. It should be used whenever possible.
paint.setAntiAlias(true);
: AntiAliasing smooths out the edges of what is being drawn
paint.setFilterBitmap(true);
: Filtering affects the sampling of bitmaps when they are transformed.
Upvotes: 1
Reputation: 157437
Use inSampleSize
to scale a Bitmap
. From the documentation
If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
For instance:
BitmapFactory.Options opts = new BitmapFactory.Options();
opt.inSampleSize = 4;
Bitmap newBitmap = BitmapFactory.decodeFile(filePath, opts);
Upvotes: 5
Reputation: 11194
You can make image thumbnail :
Bitmap thumbBitmap = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath), thumbWidth, thumbHeight);
Upvotes: 3