Can you explain what this code does? - android

This code is used to compress and resize image in my app Image is showing blurry and size is reducing to much. Can yo Explain how this code works and how can i improve quality of my image with this existing code.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f),
null, o2);

Your code is just initializing the bitmap options to work with a decodedStream ( the input image or file you will work with) after that is setting a rule thats say the required size should be >= 200 width and height and after that is just creating a final bitmap with the required output of the stream
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bit1 = BitmapFactory.decodeStream(new FileInputStream(f),
null, o2);
bit1.compress(Bitmap.CompressFormat.PNG, 100, out); //you can use this line and play with the value 100 in order to set quality of the image when its compresed
Since your image is scaled to a certain size ( which is >= 200 width and height) your image output dimension will depend on the input bitmap at first
if you want to check out the Dimensions of the bitmap you can do this
Log.e("Dimensions", bit1.getWidth()+" "+bit1.getHeight());
Bitmap.compress
compress(Bitmap.CompressFormat format, int quality, OutputStream
stream) Write a compressed version of the bitmap to the specified
outputstream.
EDIT: As Vladyslav Matviienko suggests you could increase your REQUIRED_SIZE to a bigger value, since like i wrote above, you are setting a fixed size at first

Related

android reduce image size without original image get changed

I have wrote a function that do reduce image size then upload it
everything works very well except original image will lost it quality
it should only reduce uploaded image size not the original one
this is my function
public void ReduceAndUpload(File file)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inSampleSize = 6;
BitmapFactory.decodeFile(file.getPath(), o);
int Scale = 1;
while (o.outWidth / Scale / 2 >= 75 && o.outHeight / Scale / 2 >= 75)
{
Scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = Scale;
Bitmap SelectedBitmap = BitmapFactory.decodeFile(file.getPath(), o2);
ByteArrayOutputStream OutputStream = new ByteArrayOutputStream();
SelectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, OutputStream);
String ImageEncode = Base64.encodeToString(OutputStream.toByteArray(), 0);
DoUpload(ImageEncode);
}
whats wrong ?
Full Code: http://paste.ubuntu.com/20529512/

Setting an image from filepath(sdcard) to imageview

I am trying to set the background for an ImageView using a saved path. However the following code crashes.
selectSavedImagefromGallery(imagePath);
Here imagePath = "/storage/sdcard0/Downloads/image.jpg" which is a valid path. It works when I set the image choosing it from the gallery. But I would like to save the path so it can be used at anytime.
private void selectSavedImagefromGallery(String savedImagePath) {
Bitmap bm;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(savedImagePath, options);
final int REQUIRED_SIZE = 200;
int scale = 1;
while (options.outWidth / scale / 2 >= REQUIRED_SIZE
&& options.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
options.inSampleSize = scale;
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(savedImagePath, options);
profile_pic.setImageBitmap(bm);
}
The error is being caused by this the last line:
profile_pic.setImageBitmap(bm);
What can I do to make it work?
Thanks.

reduce image size in Android programmatically

I am developing a application where , there is a profile page where user can set its image via camera or from gallery.
Now when user sets large image then my app stops unfortunate , i found a reason for that is , we can't set image on image View exceeds some size.
So i decided to reduce image size before setting it on image view.
Here i found many solutions but in all that it also reduces its width and height, but i don't want to reduce its width and height.
I only want to reduce its size in percentage. E.x from 1 mb to 100 kb.
How can I achieve this in Android?
Try this way,hope this will help you to solve your problem.
public Bitmap decodeFile(String path) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}

What does BitmapFactory.Options do?

What does BitmapFactory.Options in android.graphics.BitmapFactory.Options do?
There is no theoretical explanation in the android sdk reference manual about this class, it only contains the explanation about the methods of the class.
Bitmapfactory is mainly used for Scaling
Bitmap lBmp = BitmapFactory.decodeResource(getResources(), R.Drawable.ic_dolphin);
It gets the "dolpin" image and it will reduce the image size, if we dnt use bitmapfactory then it leads to insufficient memory allocations
It's used to pass options to the BitmapFactory - as you might expect :)
For example, you can use it to explicitly scale the Bitmap up or down from the source.
See this example
This method is used to create bitmap of given specific size which is stored in sdcard.
public Bitmap decodeFile(String path,int size) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = size;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}

android : bitmap size exceeds VM budget

I am using android Camera. Also using auto focusing feature. After capturing image more then 10 times getting the following exception:
Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
my source code is following:
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
// Bitmap bmp = BitmapFactory.decodeStream(new
// ByteArrayInputStream(data));
Utility.gc();
Bitmap bmp = decodeFile(data);
Utility.gc();
// BitmapFactory.decodeByteArray(data, 0,
// data.length, o);
// Bitmap bmpCompressed = rotateBitmap(bmp, 90, 320, 430);
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bmp, 430, 320,
true);
bmp.recycle();
Utility.gc();
writeBmp(bmpCompressed);
bmpCompressed.recycle();
Utility.gc();
} catch (Exception e) {
Log.e(Constants.TAG, e.getMessage(), e);
} finally {
isImageCapture = true;
}
}
};
private Bitmap decodeFile(byte[] buffer) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new ByteArrayInputStream(buffer), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 70;
// 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;
return BitmapFactory.decodeStream(new ByteArrayInputStream(buffer),
null, o2);
}
how can i get rid from the above mention exception?
Use
Options.inSampleSize
// Decode image size
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inJustDecodeBounds = true;
options.inSampleSize = 8;
BitmapFactory.decodeStream(new ByteArrayInputStream(buffer), null, options);
Take a look at this bug report:
http://code.google.com/p/android/issues/detail?id=8488
There's a known bug in Android relating to this error message, which doesn't seem to be accepted by Google. There are workarounds in the bug report if this is the problem you're having. I've used them to clear up similar issues.
You can try to do the scaling yourself using BitmapFactory.Options and BitmapFactory.decodeByteArray():
BitmapFactory.Options options = new BitmapFactory.Options;
options.inSampleSize = 4; // might try 8 also
BitmapFactory.decoderByteArray(data, 0, data.length, options);
There are also other options in BitmapFactory.Options to play around with.
You are probably running out of memory because you create a full decoded bitmap before you do the scaling.
Use less memory.
It definitely sounds like you have a memory leak somewhere if it works until the 10th picture is taken. Use the DDMS tools in Eclipse to track memory to try to see where it's happening.

Categories

Resources