I am trying to make a square larger by using createScaledBitmap. However, the square becomes a rectangle with width longer than height. What is the proper way to do this in order to keep the aspect ratio constant?
public Bitmap getScaledBitmap(Bitmap bitmap){
dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
scaledBitmapW = (int)(dm.widthPixels/(10)); *//make the square just 1/10 of the screen width*
scaledBitmapH = (int)(scaledBitmapW*(bitmap.getHeight()/bitmap.getWidth()));
scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledBitmapW, scaledBitmapH, true);
return scaledBitmap;
}
Related
I am taking an image and drawing it to the screen using canvas. I want to scale it based on the screen size.
This is what I tried, but it cuts off a large chunk of the image:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.myimage);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
canvas.drawBitmap(bitmap,frameToDraw,whereToDraw, paint);
I know I am doing a few things wrong, but I'm not exactly sure what. This code causes the image to exceed the size of the screen. I want it to be scaled to the size of the screen. I'll be drawing smaller images in too that I also will need to scale according to the size of the screen, though not to the full screen.
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
Take a look at the above code. Your are not scaling the picture. You are simply taking a part of the picture (in this case the whole picture) and pasting it somewhere (in this case the size of the original picture).
The width and heigth of the frameToDraw rectangle should be the width and height of your picture.
dont need creat scale bitmap, instead:
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,frameBufferHeight, Config.RGB_565);
frameBufferWidth and frameBufferHeight are the sizes that you want to make your app base on those(for example 480*800)
then simply add this code:
Rect dstRect = new Rect();
canvas.getClipBounds(dstRect); // get canvas size base screen size
canvas.drawBitmap(framebuffer, null, dstRect, null);
it draw your final bitmap according to screen size
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you get your width and height like this, you aren't going to take the size of any toolbar/status bar/navigation bar into account.
If you are coding an app that has a View, inflate a layout with an ImageView that is full size (i.e. match_parent), then use the scaleType to scale/fit/crop the image how you want.
I am taking an image and drawing it to the screen using canvas. I want to scale it based on the screen size.
This is what I tried, but it cuts off a large chunk of the image:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.myimage);
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
canvas.drawBitmap(bitmap,frameToDraw,whereToDraw, paint);
I know I am doing a few things wrong, but I'm not exactly sure what. This code causes the image to exceed the size of the screen. I want it to be scaled to the size of the screen. I'll be drawing smaller images in too that I also will need to scale according to the size of the screen, though not to the full screen.
Rect frameToDraw = new Rect(0, 0, width, height);
RectF whereToDraw = new RectF(0, 0, width, height);
Take a look at the above code. Your are not scaling the picture. You are simply taking a part of the picture (in this case the whole picture) and pasting it somewhere (in this case the size of the original picture).
The width and heigth of the frameToDraw rectangle should be the width and height of your picture.
dont need creat scale bitmap, instead:
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,frameBufferHeight, Config.RGB_565);
frameBufferWidth and frameBufferHeight are the sizes that you want to make your app base on those(for example 480*800)
then simply add this code:
Rect dstRect = new Rect();
canvas.getClipBounds(dstRect); // get canvas size base screen size
canvas.drawBitmap(framebuffer, null, dstRect, null);
it draw your final bitmap according to screen size
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you get your width and height like this, you aren't going to take the size of any toolbar/status bar/navigation bar into account.
If you are coding an app that has a View, inflate a layout with an ImageView that is full size (i.e. match_parent), then use the scaleType to scale/fit/crop the image how you want.
I am using a simplest code to set wallpaper:
Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.a));
getApplicationContext().setWallpaper(bmap2);
And the problem occur when image size is bigger than screen size.
I can see only the part of input picture.
I tried resizing methods like createScaledBitmap and it works, but not like I want.
createScaledBitmap is resizing bitmap, but not size of picture, just resolution (just mess up the quality of picture, not picture size loaded to phone as wallpaper).
Does anyone know how to scale down the size of image, not a resolution?
EDIT:
Few screens:
Images from menu, before scale and after scale:
http://zapodaj.net/14097596e4251.png.html
So as you can see there is only scaled resolution, not size of picture.
Any ideas??
Answer from the author is in the comments,
but as nobody see comments, I copy it here:
Bitmap bmap2 = BitmapFactory.decodeStream(getResources().openRawResource(R.drawable.paper));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
Bitmap bitmap = Bitmap.createScaledBitmap(bmap2, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(MainActivity.this);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
I have an application with an embedded drawable 48x48 pixel at 71,12 pixels/inch
I load the same image via a stream to a webserver, then load that stream
return new BitmapDrawable(getActivity().getResources(), new ByteArrayInputStream(imageThumbnail));
the displayed result is:
How can i get the BitmapDrawable to scale the same as the rest of the drawables?
you can trigger android bitmapfactory to scale bitmap automatically, codes for this:
BitmapFactory.Options options = new BitmapFactory.Options();
DisplayMetrics metrics = context.getApplicationContext().getResources().getDisplayMetrics();
options.inScreenDensity = metrics.densityDpi;
options.inTargetDensity = metrics.densityDpi;
options.inDensity = DisplayMetrics.DENSITY_DEFAULT;
Bitmap bm = BitmapFactory.decodeStream(in, null, options);
in.close();
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bm);
Do something like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Bitmap bitmapOrg = new BitmapDrawable(getResources(), new ByteArrayInputStream(imageThumbnail)).getBitmap();
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
float scaleWidth = metrics.scaledDensity;
float scaleHeight = metrics.scaledDensity;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
Or
Maybe try a differnet approach... try setting the height and width of images in the XML layout in dips, I am guessing you have the ImageView with wrap_content height and width at the moment, try setting the height and width to 48dip
Get the current density of the screen and set the drawable to that density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi
myDrawable.setDensity(densityDpi);
I've created a function that scales a bitmap directly to a specific surface area. The function first gets the width and height of the bitmap and then finds the sample size closest to the required size. Lastly the image is scaled to the exact size. This is the only way I could find to decode a scaled bitmap. The problem is that the bitmap returned from BitmapFactory.createScaledBitmap(src,width,height,filter) always comes back with a width and height of -1. I've already implemented other functions that use the createScaledBitmap() method with out this error and I can not find any reason why creating a scaled bitmap would produce invalid output. I've also found that if I create a copy of the image bitmap that is mutable causes the same error. Thanks
public static Bitmap load_scaled_image( String file_name, int area) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(file_name, options);
double ratio = (float)options.outWidth / (float)options.outHeight;
int width, height;
if( options.outWidth > options.outHeight ) {
width = (int)Math.sqrt(area/ratio);
height = (int)(width*ratio);
}
else {
height = (int)Math.sqrt(area/ratio);
width = (int)(height*ratio);
}
BitmapFactory.Options new_options = new BitmapFactory.Options();
new_options.inSampleSize = Math.max( (options.outWidth/width), (options.outHeight/height) );
Bitmap image = BitmapFactory.decodeFile(file_name, new_options);
return Bitmap.createScaledBitmap(image, width, height, true);
}
I added this function to scale large camera images to a specific number of mega pixels. So a typical area passed in would be 1000000 for 1 megapixel. The camera image after being decoded yields a outWidth of 1952 and a outHieght of 3264. I then calculate the ratio this way I can keep the same height to width ratio with the scaled image, in this case the ratio is 0.598... Using the ratio and the new surface area I can find the new width which is 773 and a height of 1293. 773x1293=999489 which is just about 1 megapixel. Next I calculate the sample size for which to decode the new image, in this case the sample size is 4 and the image is decoded to 976x1632. So I'm passing in a width of 773 a height of 1293.
I was having a similar problem (getting -1 for height and width of the scaled bitmap).
Following this stackOverflow thread:
Android how to create runtime thumbnail
I've tried to use the same bitmap twice while calling the function:
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE,
THUMBNAIL_SIZE, false);
For some reason, this solved my problem, perhaps it would solve yours too.