Resize image without aspect ratio - android

i want to resize my bitmap image to 512px,512px. i use below code ,but when my image resized its very bad and aspect ratio affected. how i can resize my image without aspect ratio?2nd image resized with pc to 512px.
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;
}

you can resize your bitmap using this.
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
or:
resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

Related

android resize bitmap auto height

I try to resize bitmap from gallery.I wrote some code and i can resize bitmap only static width and height.this is source
public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {
float aspectRatio = (float)bm.getWidth() / (float) bm.getHeight();
int height = Math.round(newWidth / aspectRatio); //based on width it gives height
Matrix matrix = new Matrix();
matrix.postScale(newWidth, height);
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, newWidth, height, matrix, false);
return resizedBitmap;
}
but i want to resize bitmap for example width 800px and auto height.is it a possible to solve this problem in android ?
thanks
float aspectRatio = (float)Image.getWidth() / (float) Image.getHeight();
int width = 200; //your width
int height = Math.round(width / aspectRatio); //based on width it gives height
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap resizedBitmap;
try {
resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight());
}catch (Exception e){
e.printStackTrace();
boolean_image = true;
return "";
}
return resizedBitmap;
}

Bitmap image compress from url

Hello i have facebook image that i have to compress and put it in my imageview. I used below code to resize my image and compress it so that i can show it in my imageview but it gives file not found exception error
i do not find any way to compress file/image that located on server.
you can take bitmap from URL and the you suppose to re size.
For Getting Bitmap From URL.
URL url = new URL("http://....");
Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
for resize you can use below code
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;
}
How to use :-
place this code:-
private void showImage(final String URL) {
new Thread(new Runnable() {
#Override
public void run() {
URL url = new URL(URL);
Bitmap bm = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) YOUR_WIDTH) / width;
float scaleHeight = ((float) YOUR_HEIGHT) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
final Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width,
height, matrix, false);
runOnUiThread(new Runnable() {
#Override
public void run() {
your_imageView.setImageBitmap(resizedBitmap);
}
})
}
}).start();
}
thanks.

out of memory by resizing bitmap

I am trying to resize a bitmap to custom X and Y and the code I am using runs sometimes into OutOfMemory exception. I looked here around and found some solutions how to use InputStream to resize bitmaps, but I was not able to find any approach how to resize it to custom X and Y dimensions . Could somebody give me an advice?
here is my code:
try {
Point scr = Sys.screenSize(getActivity());
// MY CUSTOM X and Y
int newWidth = (int) Sys.convertDpToPixel(linheight, getActivity())
* lines;
int newHeight = scr.x;
Bitmap bitmap = BitmapFactory.decodeResource(getActivity()
.getResources(), R.drawable.field);
int width = bitmap.getWidth();
int height = bitmap.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
bm = Bitmap
.createBitmap(bitmap, 0, 0, width, height, matrix, false);
bitmap.recycle();
matrix = null;
iv.setImageBitmap(bm);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
I think what you want is createScaledBitmap:
Bitmap resizedBitmap =
Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, false);

resize image in android drawable

I have full resolution images in drawable folder need to resize according to phone screen size how to do that ?
Do it like this
/************************ Calculations for Image Sizing *********************************/
public Drawable ResizeImage (int imageID) {
//Get device dimensions
Display display = getWindowManager().getDefaultDisplay();
double deviceWidth = display.getWidth();
BitmapDrawable bd=(BitmapDrawable) this.getResources().getDrawable(imageID);
double imageHeight = bd.getBitmap().getHeight();
double imageWidth = bd.getBitmap().getWidth();
double ratio = deviceWidth / imageWidth;
int newImageHeight = (int) (imageHeight * ratio);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), imageID);
Drawable drawable = new BitmapDrawable(this.getResources(),getResizedBitmap(bMap,newImageHeight,(int) deviceWidth));
return drawable;
}
/************************ Resize 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;
}
to get the width you should do this :
Display display = getWindowManager().getDefaultDisplay();
double deviceWidth;
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB_MR1)
deviceWidth= display.getWidth();
else {
display.getSize(size);
deviceWidth=size.x;
}

Android - Fitting bitmap to screen

I have this project in which I have a bitmap bigger than the screen size. I want to resize it to fit the screen exactly. I have no titlebar, and I am in fullscreen mode. This is my non-working code:
public class ScopView extends View
{
private Scop thescop;
public ScopView(Context context, Scop newscop)
{
super(context);
this.thescop = newscop;
}
#Override
public void onDraw(Canvas canvas)
{
Bitmap scopeBitmap;
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false;
bfOptions.inPurgeable = true;
bfOptions.inInputShareable = true;
bfOptions.inTempStorage = new byte[32 * 1024];
scopeBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.scope, bfOptions);
scopeBitmap.createScaledBitmap(scopeBitmap, SniperActivity.Width, SniperActivity.Height, false);
canvas.drawBitmap(scopeBitmap, SniperActivity.scopx, SniperActivity.scopy, null);
}
}
While in here the createScaledBitmap method, I am using itself as the source, and some variables from an activity used to retrieve the window height and width from screen preferences.
You can use the below code to resize the bitmap.
int h = 320; // Height in pixels
int w = 480; // Width in pixels
Bitmap scaled = Bitmap.createScaledBitmap(largeBitmap, h, w, true);
Also, you can use the below code snippet.
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;
}
This code is help to you try this
int REQ_WIDTH = 0;
int REQ_HEIGHT = 0;
REQ_WIDTH =imageView.getWidth();
vREQ_HEIGHT =imageView.getHeight();
mImageView.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imageURI, options), REQ_HEIGHT, REQ_WIDTH, true));

Categories

Resources