I am trying to scale a Bitmap, doubling its size.
But after the scaling the bitmap shows empty, all plain gray...
here is the code:
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(2, 2);
// recreate the new Bitmap and set it back
Bitmap bm2=Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
//bm.recycle();
EDIT EDIT EDIT EDIT EDIT
I figured it out it's memory issue, if I do it with small images it works fine.
Still the problem remains with large pictures!!!
Thanks for any suggestion!!!
As from the Bitmap Doc it is clearly saying that Bitmap.createBitmap() returns the same bitmap or part of the source bitmap.So may be here it returns the same bitmap object.But here you are recycling it
bm.recycle();
thats why you are getting null
Use this method
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
and pass width as 1920 and height as 2560
Related
I want to resize an image to a smaller image like thumbnail. I have used following options
Bitmap imageBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 700, false);
and
ThumbnailUtils.extractThumbnail()
Both of these options cut parts of images. So they don't look good. I just want to reduce image dimensions and don't want to loose image content.
you have to try like this
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filepath,bmOptions);
//Part 1 :withcompression Sacle image 200 pixels x 700 pixels this size you can customize as per your requirement
Bitmap withCompressed = Bitmap.createScaledBitmap(bitmap,200,700,true);
If you want to resize an image without losing quality and images are from drawable/mipmap then you should try pngquant.
https://pngquant.org/
I have been searching but i did not find a question about how to pixelize a bitmap in android
Example
Note that i dont mean blur
You could just try and resize that image to a smaller version (and then back up if you need it at the same size as the original for some reason)
{
Bitmap original = ...;
//Calculate proportional size, or make the method accept just a factor of scale.
Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
//Recycle small, recycle original if no longer needed.
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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(
bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
The code is from here
The simplest way to pixelate the image would be to scale image down using "nearest neighbour" algorithm, and then scale up, using the same algorithm.
Filtering over the image trying to find an average takes much more time, but does not actually give any improvements in result quality, after all you do intentionally want your image distorted.
I want to resize the bitmap height & width into same as the user device width & height.So please help me if u know the code.Thank You !
This is my code:
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.nature);
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
width = metrics.widthPixels;
height=metrics.heightPixels;
But it was not working
Try to use this method:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
// GET CURRENT SIZE
int width = bm.getWidth();
int height = bm.getHeight();
// GET SCALE SIZE
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Bitmap.createScaledBitmap works great. As the document:
Creates a new bitmap, scaled from an existing bitmap, when possible. If the specified width and height are the same as the current width and height of the source bitmap, the source bitmap is returned and no new bitmap is created.
I found this bit of code on a similar question, but I can't figure out how to use it.
Resizing a Bitmap:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth)
{
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
I'm also confused as to where to place this. With my initial variables? Inside the constructor, or runnable? If the bitmap I wanted to change the size of is called Background, how do I add that to this method?
Thanks in advance.
I am loading an image scale it to screen size and after that the user can draw some text on it using his hand.
After that user can save it to SD card. I need to save it to same width and height as original image has. For others sample image it is working fine. But when i am loading the image captured by camera and once i draw something on draw. I try to scale image back to original height and width but it is giving Exception "Out of Memory Exception" for all the images captured by camera. The code is as follows:
private void saveView(View view) {
Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
String filePath="/sdcard/";
Canvas c = new Canvas(b);
view.draw(c);
float scaleHeight=((float)getHeight()/b.getHeight());
float scaleWidth=((float)getWidth()/b.getWidth());
Matrix matrix=new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Log.e("getWidth()", ":"+getWidth());
Log.e("getHeight()", ":"+getHeight());
**//Giving Exception at below Line as i am trying to scat it to origenal size.
b=Bitmap.createBitmap(b,0,0,b.getWidth(),b.getHeight(),matrix,true);**
You need to resize your bitmap as below.+
Bitmap resizedBitmap = getResizedBitmap(myBitmap, 150, 150);
and the method
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 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(bm, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}