Converting image to bitmap and reducing the size of it - android

I have look at many different methods of creating a reduced size bitmap of an image, but none of them work properly / I need something different.
This is a little difficult to explain :-)
What I need is a bitmap that keeps the ratio of the picture, but is less than a certain size - eg 1mb or the equivalent in pixel dimensions (As this bitmap needs to added as a putExtra() for an intent).
Problems I'm having so far:
Most of the methods that I've looked at create a scaled version of the bitmap. So: Image -> Bitmap1 (unscaled) -> Bitmap2 (scaled). But if the resolution of the image is very high, it is not scaled down enough. I think the solution would be to create a bitmap of an exact size so that any resolution can be reduced enough.
However, the side effect of this method would be that images already less than the required size will be resized up (or the resizing won't work?). So there needs to be an "if" to check if the image can be converted to a bitmap without resizing.
I have no idea how to go about doing this so any help is very much appreciated! :-)
This is what I'm using at the moment (It does NOT do I want it to do):
// This is called when an image is picked from the gallery
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 0:
if (resultCode == Activity.RESULT_OK) {
selectedImage = imageReturnedIntent.getData();
viewImage = imageReturnedIntent.getData();
try {
decodeUri(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
iv_preview.setImageBitmap(mImageBitmap);
}
break; // The rest is unnecessary
This is the part which is currently scaling the size:
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true; //
BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 260; // Is this kilobites? 306
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o2.inScaled = false; // Better quality?
mImageBitmap = BitmapFactory.decodeStream(getActivity()
.getContentResolver().openInputStream(selectedImage), null, o2);
return BitmapFactory.decodeStream(getActivity().getContentResolver()
.openInputStream(selectedImage), null, o2);
}
If anything needs to be explained more please say.
Thank you

How to call:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
pho1.setImageBitmap(decodeSampledBitmapFromResource(picturePath,
80, 60));
Methods:
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
Log.d("path", path);
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}

Related

Bitmap null when attempting to load scaled down version

I'm attempting to scale down a bitmap to load a smaller version into memory. I'm pretty much following Google's example (search Loading Large Bitmaps Efficiently), except that I'm loading from the image gallery instead of a resource. But I seem to be getting back a null bitmap after calculating dimensions. Here's my code:
/** OnActivityResult Method **/
final Uri imageUri = data.getData();
final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri);
Bitmap bitmapToLoad = Util.decodeSampledBitmapFromResource(imageStream, 500, 500); // bitmapToLoad is null.
mIvScreenshot.setImageBitmap(bitmapToLoad);
/**Helper Methods **/
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(InputStream is,
int reqWidth, int reqHeight) {
Rect rect = new Rect();
// First decode with inJustDecodeBounds = true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, rect, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(is, rect, options);
}
Can anyone catch what I'm doing wrong?
I managed to get it working. Thanks to Biraj Zalavadia (How to reduce an Image file size before uploading to a server) for the scaling logic, and the cursor code here (How to return workable filepath?). Here is my onActivityResult():
try {
final Uri imageUri = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(imageUri, filePath, null, null, null);
cursor.moveToFirst();
String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
Uri newUri = Uri.parse(ScalingUtilities.scaleFileAndSaveToTmp(imagePath, 500, 500));
final Bitmap selectedImage = BitmapFactory.decodeFile(newUri.getEncodedPath());
mIvScreenshot.setImageBitmap(selectedImage);
} catch (Exception e) {
// Handle
}

Resize a Image Picked from Gallery to Image View

My App have an activity with a ImageView where i pick a picture from phone gallery and set in this ImageView as user's profile picture.
My problem is that some pictures when picked make app stop cause is too big, i want to know if someone can look my code and help me how can i resize this picked picture, so after set in this image view, how can user cut this picture before set, here below is my code where i pic the picture. i will be so greatful if someone do the needed changes and give me the code cause i dont know so much about developing. thank you.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.User);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Try this is image compression like whtsapp I have used in my application https://www.built.io/blog/2013/03/improving-image-compression-what-weve-learned-from-whatsapp/
You can use Picasso library. Get it from here.
The syntax looks like this:
Picasso.with(getContext()).load(imagePath).into(imageView);
Not gonna rewrite your code but this could be useful
Bitmap bitmap = BitmapFactory.decodeFile(foto.getFotoOrderFilePath());
Double height = (double)bitmap.getHeight();
Double scalingFactor = (960.0/height);
int tempWidht = bitmap.getWidth();
Double Dwidth = (tempWidht*scalingFactor);
int width = Dwidth.intValue();
Log.v("bitmap dimensions: ", String.valueOf(height) + " + " +String.valueOf(width) + " + " + String.valueOf(scalingFactor));
bitmap = Utilities.scaleBitmap(bitmap, width, 960);
An excerpt from code I use to scale a bitmap down. It sets the hight to 960 and get the scaling to change the width accordingly.
edit:
the ScaleBitmap method.
public static Bitmap scaleBitmap(Bitmap bitmap, int wantedWidth, int wantedHeight) {
Bitmap output = Bitmap.createBitmap(wantedWidth, wantedHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
Matrix m = new Matrix();
m.setScale((float) wantedWidth / bitmap.getWidth(), (float) wantedHeight / bitmap.getHeight());
canvas.drawBitmap(bitmap, m, new Paint());
return output;
}
sorry for the late response
You can try this code to resize image as per your requirement
public Bitmap decodeSampledBitmapFromResource(String Filepath,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(Filepath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(Filepath, options);
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
int inSampleSize = 1;
final int height = options.outHeight;
final int width = options.outWidth;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
check here
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

How to change the size of the image without changing resoultion?

I am developing an app in which i want to reduce the size of my image.For example if size is 1MB then i want it to get reduce into kb.
But resolution should not get changed.
Can anyone help in this?
I tried this code but its not working
public static Bitmap resizeBitMapImage1(String filePath, int targetWidth, int targetHeight) {
Bitmap bitMapImage = null;
try {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
double sampleSize = 0;
Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth
- targetWidth);
if (options.outHeight * options.outWidth * 2 >= 1638) {
sampleSize = scaleByHeight ? options.outHeight / targetHeight : options.outWidth / targetWidth;
sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
}
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[128];
while (true) {
try {
options.inSampleSize = (int) sampleSize;
bitMapImage = BitmapFactory.decodeFile(filePath, options);
break;
} catch (Exception ex) {
try {
sampleSize = sampleSize * 2;
} catch (Exception ex1) {
}
}
}
} catch (Exception ex) {
}
return bitMapImage;
}
Using this code it reduces the resolution but not much size of the image.
I also tried
public static Bitmap reduceImgSizeToHundredKB(Bitmap bitmap) {
Bitmap scaled = bitmap;
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return scaled;
}
If you don't want to resize your image then your only option is to compress it.
Here is an example on how to do that:
byte[] data = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
In my thought, In most cases, we can't do that. Because it related to resolution of the image and the color range of the image.
so, If we have a large image with a lot of colors, then reduce the size of the image will induce to reduce resolution of it.
There are two aproches: lossy (where you'll lose quality of the image) and lossless.
The answer provided by rickman is lossy so it will work well but reduce the quality of the image - it works particularly well for pictures taken with a camera.
The lossless approach is using PNG, which consists of using Bitmap.CompressFormat.PNG as argument to the compress method.
A not as well known but very efficient format is WebP. It's also available as an argument to the compress method (Bitmap.CompressFormat.PNG). I'd suggest testing with WebP, specially considering it supports both lossy and lossless compression.
Use this method if you are decoding from path
public Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp = BitmapFactory.decodeFile(path, options);
return bmp;
}
}
public int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
Use this if you are decoding from resources
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
If you want to reduce the file size of the image without losing a great ammount of quality you can do this:
First you need to flatten the image, then save it for web (ctrl+alt+shift+s)
Then select the preset PNG-8 128 Dithered.
You can now customize this preset by setting the colors to "256", then select "selective" under the PNG-8, under "selective" change dither to "diffusion", deselect the "interlaced" option and make shure that the web snap option is set to 0%.

Google Map custom Marker Out of Memory Error (API V2)

I'm using the following code to set marker with user's own image in his/her gallery. But I get out of memory error all the time so I guess my implementation is wrong. Another interesting behavior I found is that if the marker isn't in the view, the error doesn't occur immediately. But once I move the camera to where that marker is the error appears again. (In short, I never get a chance to see my image)
Codes I use:
//on button click, send user to gallery to choose image he/she wants to use
changeAvatarButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
//use the selected image for marker icon
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// BitmapDescriptorFactory
myIcon.setIcon(BitmapDescriptorFactory
.fromPath(picturePath));
}
}
logcat error: E/dalvikvm-heap(5809): Out of memory on a 16777232-byte allocation.
When debugging I change picturePath to a known path such as "/mnt/sdcard/DCIM/Camera/IMG_20121214.jpg" but the error is the same.
Thanks in advance :)
decode and scale image before loaded into memory,just change landscape and portrait to the size you actually want
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;
if(imageWidth > imageHeight) {
options.inSampleSize = calculateInSampleSize(options,512,256);//if landscape
} else{
options.inSampleSize = calculateInSampleSize(options,256,512);//if portrait
}
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path,options);
method for calculating size
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and width
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
You are trying to put 4 Mpix image as a marker icon. That doesn't seem like a good idea.
Load it as a Bitmap, scaling it down to reasonable size.

How to get an Image of specific dimensions from native camera

This is the code I am using to get a full sized image, which is >2mb in size and 3560 X 1876 in dimension.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView) findViewById(R.id.image);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
filePath = Environment.getExternalStorageDirectory() + "/Full_"
+ System.currentTimeMillis() + ".jpeg";
File file = new File(filePath);
Uri output = Uri.fromFile(file);
Intent i = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, output);
startActivityForResult(i, CAPTURE_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap photo = BitmapFactory.decodeFile(filePath, options);
image.setImageBitmap(photo);
}
Is there a way to get the image of specific size, in my case a <100kb sized image of dimensions 480 x 320.
You can take a look at this blog post I wrote on how to get Images from device's camera Activity:
Guide: Android: Use Camera Activity for Thumbnail and Full Size Image
If you look at the end of the guide, you have this method:
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight)
{ // BEST QUALITY MATCH
//First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.RGB_565;
int inSampleSize = 1;
if (height > reqHeight)
{
inSampleSize = Math.round((float)height / (float)reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth)
{
//if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
inSampleSize = Math.round((float)width / (float)reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
you can provider there the required dimensions for your image output.
you have to use the inSampleSize from BitmapFactory.Options. Through it the decoder will downsample your image of the inSampleSize. For instance inSampleSize = 2 will create a bitmap of width/2 and heigth/2
int destWidth = 480;
int destHeight = 320;
while ( (bitmapWidth/2 > destWidth)
|| (bitmapHeight/2 > destHeight)) {
++inSampleSize;
bitmapWidth /= 2;
bitmapHeight /=2;
}
What if using a simplest way to do it. Copy this method, pass your bitmap and required height and width, get a scaled image back :)
public static Bitmap resizeBitmap(Bitmap originalBitmap, int width, int height) {
float scaleWidth = ((float) width) / originalBitmap.getWidth();
float scaleHeight = ((float) height) / originalBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(originalBitmap, 0, 0, original.getWidth(),original.getHeight(), matrix, true);
}

Categories

Resources