I am working on a project in android studio where I am uploading images. Some of the images are very large in size and I want to compress before upload them in the app. Can anyone please suggest me soemthing?
You can use this code to compress images
Bitmap imageBitmap;
int newHeight = 20 // define your height here
int newWidth = 30 //define new height
Bitmap resizedBitmap = Bitmap.createScaledBitmap(imageBitmap,newWidth,
newHeight,
true);
https://developer.android.com/reference/android/graphics/Bitmap.html see documentation for details.
use this library to compress any file :
https://github.com/zetbaitsu/Compressor
try this function
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {
float ratio = Math.min(
maxImageSize / realImage.getWidth(),
maxImageSize / realImage.getHeight());
int width = Math.round(ratio * realImage.getWidth());
int height = Math.round(ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}
Related
I need to display images which are stored in database and I'm having issues with image (Bitmap) width/height and ImageView ...
Just for test - when I add same image in project's drawables I can use this:
Works:
Bitmap b = BitmapFactory.decodeResource(context.getResources(), R.drawable.menu_image);
BitmapDrawable bd = new BitmapDrawable(context.getResources(), b);
imageView.setImageDrawable(bd);
same as
imageView.setImageResource(R.drawable.menu_image);
This following isn't working because image isn't resized:
imageView.setImageBitmap(image);
same as
imageView.setImageDrawable(new BitmapDrawable(context.getResources(), image));
Where image is constructed using this:
public static Bitmap base64ToBitmap(String b64) {
byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDensity = context.getResources().getDisplayMetrics().densityDpi;
options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length, options);
return bitmap;
}
Image original size is 338x94.
676x188, this is image size when I'm using image from project's drawables directory. This is size that I'm looking for, in this case. I suppose a quick fix would be to use Bitmap.createScaledBitmap(), but I have couple of different image formats and I would like to use imageView.setImageBitmap or imageView.setImageDrawable to behave like I loaded Bitmap from project's drawables directory.
Use the following helper class from github
public class BitmapScaler
{
// scale and keep aspect ratio
public static Bitmap scaleToFitWidth(Bitmap b, int width)
{
float factor = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);
}
// scale and keep aspect ratio
public static Bitmap scaleToFitHeight(Bitmap b, int height)
{
float factor = height / (float) b.getHeight();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
}
// scale and keep aspect ratio
public static Bitmap scaleToFill(Bitmap b, int width, int height)
{
float factorH = height / (float) b.getWidth();
float factorW = width / (float) b.getWidth();
float factorToUse = (factorH > factorW) ? factorW : factorH;
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorToUse),
(int) (b.getHeight() * factorToUse), true);
}
// scale and don't keep aspect ratio
public static Bitmap strechToFill(Bitmap b, int width, int height)
{
float factorH = height / (float) b.getHeight();
float factorW = width / (float) b.getWidth();
return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factorW),
(int) (b.getHeight() * factorH), true);
}
}
I am new to android. I want to reduce the size of a bitmap to 100kb exactly. I get an image from the sdcard, compress it and save it to the sdcard again with a different name into a different directory. Compression works fine (3 mb like image is compressed to around 100 kb). I trying to compress 1mb image to 100kb, but i am getting size of that image is 20kb. But i need that 1mb(or 2mb (or) 3mb (or) 4mb (or) 5mb.....) image also get resize to 100kb. and my code is,,
private Bitmap getResizedBitmap(Bitmap bm, int maxSize)
{
int width = bm.getWidth();
int height = bm.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(bm, width, height, true);
}
public static Bitmap getResizedBitmap(Bitmap bm, float newHeight, float newWidth) {
float width = bm.getWidth();
float height = bm.getHeight();
float scaleWidth = (newWidth) / width;
float scaleHeight = (newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
return Bitmap.createBitmap(bm, 0, 0, (int) width, (int) height, matrix, false);
}
Using matrix
Resizing bitmap by pixel resolution is possible but resizing by its disk size is not as far as I know
I want to scale an existing bitmap. So I use Bitmap.createBitmap(Bitmap b, int x, int y, int width, int height, Matrix m, boolean filter) to create a scaled bitmap. If the scale ratio of the Matrix object is less than 1, this method works well. However, if the ratio is equal to or greater than 1, the method returns a bitmap with the width and height i desire but without an image (is transparent). I wonder why and how to solve this.
Bitmap imageBitmap;
imageBitmap = weiboView.getDrawingCache();
Log.d("cosmo", "bitmap generated: "+imageBitmap.getWidth()+" * "+imageBitmap.getHeight());
//Init and configure and load the image view
ImageView imageView = new ImageView(this);
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
imageView.setScaleType(ScaleType.FIT_CENTER);
containerLayout.addView(imageView);
//create a scaled bitmap to assign to the image view
Bitmap scaledImageBitmap = MyBitmapUtils.getBitmapScaledToFitWidth(getWindowManager().getDefaultDisplay().getWidth(), imageBitmap);
Log.d("cosmo", "scaled: "+scaledImageBitmap.getWidth()+" * "+scaledImageBitmap.getHeight());
//Here if I set imageBitmap as the image of imageView it works well
imageView.setImageBitmap(scaledImageBitmap);
Here is MyBitmapUtils.getBitmapScaledToFitWidth:
public static Bitmap getBitmapScaledToFitWidth(int targetWidth, Bitmap bitmap) {
float ratio = (float)targetWidth/(float)bitmap.getWidth();
Log.d("cosmo", "ratio is "+ratio);
//ratio = 0.5f;
Matrix matrix = new Matrix();
matrix.postScale(ratio, ratio);
Bitmap target = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,false);
return target;
}
I have known how did this happen. It's because that the height of the Image is greater than 2048, and in android's system creating a bitmap bigger than 2048*2048 will cause OOM (My logcat didn't report this error, oddly)
Why don't you just use createScaledBitmap?
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
You can supply the targetWidth and targetHeight directly and it should be more reliable than scaling the bitmap yourself with a Matrix.
If you just supply the targetWidth to your method then you have to calculate the targetHeight which would look something like this:
float ratio = (float)targetWidth/(float)bitmap.getWidth();
int targetHeight = (int)((float)bitmap.getHeight * ratio);
And if you put that in your method it would look like this:
public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) {
double bitmapWidth = bitmap.getWidth();
double bitmapHeight = bitmap.getHeight();
double widthRatio = targetWidth / bitmapWidth;
double targetHeight = widthRatio * bitmapHeight;
Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true);
return target;
}
Also don't forget to recycle() not needed bitmaps as often and as soon as possible. That can prevent you from running into memory problems, for example if you don't need the not scaled bitmap anymore after scaling it you can recycle it directly in your helper method:
public static Bitmap getBitmapScaledToFitWidth(double targetWidth, Bitmap bitmap) {
double bitmapWidth = bitmap.getWidth();
double bitmapHeight = bitmap.getHeight();
double widthRatio = targetWidth / bitmapWidth;
double targetHeight = widthRatio * bitmapHeight;
Bitmap target = Bitmap.createScaledBitmap(bitmap, (int)targetWidth, (int)targetHeight, true);
// Recycle old bitmap to free up memory.
bitmap.recycle();
return target;
}
In my Application when i take the picture from camera, i needs to get the size of that picture and compress it,if it is more than the specified size. how should i know the size of image and compress it according to my application..
Please help me.
To get the height and width, you call:
Uri imagePath = Uri.fromFile(tempFile);//Uri from camera intent
//Bitmap representation of camera result
Bitmap realImage = BitmapFactory.decodeFile(tempFile.getAbsolutePath());
realImage.getHeight();
realImage.getWidth();
To resize the image, I just supply the resulting Bitmap into this method:
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min((float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height,
filter);
return newBitmap;
}
The basic actually is just Bitmap.createScaledBitmap(). However, I wrap it to another method to scale it down proportionally.
I like to resize an bitmap,if it is big make it small and place it on an specific location in the surface view, I need to get device width and height and then bitmap size and place them in an surface view and then take another image resize it and place it in any location has i like.
How to know the location co ordinates first(to place second image - this should be device independent if larger/smaller screen layout should not change)
and how to scale an big bitmap and set as background so i can draw an image over it.
my code :
final int canvasWidth = getWidth();
final int canvasHeight = getHeight();
int imageWidth = img.getWidth();
int imageHeight = img.getHeight();
float scaleFactor = Math.min( (float)canvasWidth / imageWidth,
(float)canvasHeight / imageHeight );
Bitmap scaled = Bitmap.createScaledBitmap( img,
(int)(scaleFactor * imageWidth),
(int)(scaleFactor * imageHeight),
true );
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(scaled, 10, 10, null);
this scale the big image but it doesn't fit the whole screen "img - bitmap is like an background image "
Someone can help me to understand the resize basics (I'm new so finding hard to understand resizing) to resize image to fit screen and resize any image to smaller one and place it in any location i like .
Store your bitmap as a source for manipulation and display it using ImageView:
Bitmap realImage = BitmapFactory.decodeFile(filePathFromActivity.toString());
Bitmap newBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);
imageView.setImageBitmap(newBitmap);
//scale down method
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min(
(float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());
Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}
And set your ImageView's width and height to "fill_parent".