I`m editing a bitmap to optimize it for an OCR scan. One of the things I need to do is to rotate the image 270 degrees. I'm using the following code:
Matrix matrix = new Matrix();
matrix.PostRotate (270);
canvas.DrawBitmap(alteredBitmap, matrix, paint);
Apparently, this does not work for me. Can someone point out where I am wrong?
The bitmap comes from a byte[].
This is the snippet of code I use to rotate a byte[] in my projects. It receives and returns a byte[], but by removing the last 5 lines of code it'll return a Bitmap instead. It works wonders:
public async Task<byte[]> RotateImage(byte[] source, int rotation)
{
//This is optional, use it to reduce your images a little
var options = new BitmapFactory.Options();
options.InJustDecodeBounds = false;
options.InSampleSize = 2;
var bitmap = await BitmapFactory.DecodeByteArrayAsync(source, 0, source.Length, options);
Matrix matrix = new Matrix();
matrix.PostRotate(rotation);
var rotated = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
var stream = new MemoryStream();
await rotated.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, stream);
var result = stream.ToArray();
await stream.FlushAsync();
return result;
}
All awaitable calls have non-async counterparts, so this can be converted to run in a blocking way without major issues. Just beware that removing the options variable might cause OutOfMemoryException, so make sure you know what you're doing before removing.
This method has always worked for me
Matrix matrix = new Matrix();
//myBitmap is the bitmap which is to be rotated
matrix.postRotate(rotateDegree);
Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true);//Rotated Bitmap
Well if you have this bitmap generated from a url , go ahead and use this function
public Bitmap decodeBitmap(File f) {
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
//The new size we want to scale to
final int REQUIRED_SIZE = 490;
//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.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
Related
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/
I am trying to send an image to a server. Before sending it, I am reducing its size and quality, and then fixing any rotation issue. My problem is that, after rotating the image, when I save it, the file is bigger than before. Before rotation size was 10092 and after rotation is 54226
// Scale image to reduce it
Bitmap reducedImage = reduceImage(tempPhotoPath);
// Decrease photo quality
FileOutputStream fos = new FileOutputStream(tempPhotoFile);
reducedImage.compress(CompressFormat.JPEG, 55, fos);
fos.flush();
fos.close();
// Check and fix rotation issues
Bitmap fixed = fixRotation(tempPhotoPath);
if(fixed!=null)
{
FileOutputStream fos2 = new FileOutputStream(tempPhotoFile);
fixed.compress(CompressFormat.JPEG, 100, fos2);
fos2.flush();
fos2.close();
}
public Bitmap reduceImage(String originalPath)
{
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
o.inPurgeable = true;
o.inInputShareable = true;
BitmapFactory.decodeFile(originalPath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 320;
// 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.inPurgeable = true;
o2.inInputShareable = true;
o2.inSampleSize = scale;
Bitmap bitmapScaled = null;
bitmapScaled = BitmapFactory.decodeFile(originalPath, o2);
return bitmapScaled;
}
public Bitmap fixRotation(String path)
{
Bitmap b = null;
try
{
//Find if the picture is rotated
ExifInterface exif = new ExifInterface(path);
int degrees = 0;
if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("6"))
degrees = 90;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("8"))
degrees = 270;
else if(exif.getAttribute(ExifInterface.TAG_ORIENTATION).equalsIgnoreCase("3"))
degrees = 180;
if(degrees > 0)
{
BitmapFactory.Options o = new BitmapFactory.Options();
o.inPurgeable = true;
o.inInputShareable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, o);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(degrees);
b = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
}
catch(Exception e){e.printStackTrace();}
return b;
}
You're compressing it with different quality measures. After rotation, you're using quality 100, so it's going to be a larger file than the previous one, with quality 55.
When you compress an image, it doesn't matter what the current file size/quality is. That has no real impact on the outcome. Compressing at 55 quality, followed by 100 quality, does not result in a file with the same size as a simple 55 quality compression. It results in a file with the size of 100 quality compression, because that's the last thing done to it.
For your specific code, I'm not sure I see the reason behind compressing it twice anyway. Compression(file size) isn't what was causing your OOM problems when rotating, the image dimensions were most likely the culprit. Shrinking the image before rotating should fix that, no need for saving a temp file.
All you need to do is run reduceImage(), then follow it up with fixRotation(). Fix your rotation method so that it accepts a Bitmap instead of a path, so you don't need to save the file in between. Finally, save/compress it at whatever quality you desire.
If you do need the temp file for some reason, use PNG for the first compression. This way it's lossless, so when you recompress the final image, you won't have used JPG(lossy) twice at a low quality.
First off, I have read many posts and articles about out of memory exceptions but none of them have helped with my situation. What I'm trying to do is load an image from the sd card but scale it to an exact pixel size.
I first get the width and height of the image and calculate the sample size:
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(backgroundPath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, getWidth(), getHeight());
Here's how I get the sample size (although its not really relevant):
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;
// NOTE: we could use Math.floor here for potential better image quality
// however, this also results in more out of memory issues
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;
}
Now that I have a sample size I load the image from disk to an approximate size (sample size):
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
options.inPurgeable = true;
Bitmap bmp = BitmapFactory.decodeFile(backgroundPath, options);
Now, I scale this bitmap that I have created to the exact size I need and clean up:
// scale the bitmap to the exact size we need
Bitmap editedBmp = Bitmap.createScaledBitmap(bmp, (int) (width * scaleFactor), (int) (height * scaleFactor), true);
// clean up first bitmap
bmp.recycle();
bmp = null;
System.gc(); // I know you shouldnt do this, but I'm desperate
The above step is usually get my out of memory exception. Does anyone know a way to load an exact size bitmap from disk to avoid having to create two separate bitmaps like above?
Also, it seems like more exceptions occur when the user runs this code for a second time (sets a new image). However, I make sure to unload the drawable that was created from the bitmap which allows it to be garbage collected before this code is run again.
Any suggestions?
Thanks,
Nick
In your case there's no need to create the intermediate bitmap after you've performed the first decode. Since you're drawing to to a Canvas, you can use either the following methods (whichever you find most convenient) to scale the image to the perfect size.
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint)
Maybe this method would be helpful, I think I pulled it off of stackoverflow myself. It solved my out of memory exception issue.
private Bitmap decodeFile(File f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=250;
//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.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
I'm getting the error: java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=12295KB, Allocated=3007KB, Bitmap Size=15621KB)
The bitmap size is larger than my heapSize.. So how can I make the bitmap smaller? Here is the code where it crashes:
private Bitmap getPicture(int position) {
Bitmap bmpOriginal = BitmapFactory.decodeFile(mFileLocations.get(position));
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(bmResult);
tempCanvas.rotate(90, bmpOriginal.getWidth()/2, bmpOriginal.getHeight()/2);
tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
bmpOriginal.recycle();
}
it crashes at line:
Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.RGB_565);
and my decodeFile:
private Bitmap decodeFile(String f){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),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 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.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
Log.d(LOG_TAG, "Failed to decode file");
}
return null;
}
Scale down your image to be in VM Budget...
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile( filename, options );
options.inJustDecodeBounds = false;
options.inSampleSize = 4;
bitmap = BitmapFactory.decodeFile( filename, options );
if ( bitmap != null ) {
bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
}
Change sample size to whatever you want like 2, 4, 6, 8 etc..
For full details refer to this from Android developers site which was posted rescently, it clearly states what you need to do to be in VM budget..
Two things:
It appears that you are using the BitmapFactory.decodeFile method instead of the method you have posted.
Also it looks like the only information you are keeping from bmpOriginal are its dimensions. So why not just use BitmapFactory.inJustDecodeBounds like you do in your decodeFile method. Or do just recycle the bitmap before you create the new one while retaining its dimensions, like this:
int width = bmpOriginal.getWidth();
int height = bmpOriginal.getHeight();
bmpOriginal.recycle();
Bitmap bmResult = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
This was recently covered in AndroidDevelopers+ :
https://plus.google.com/103125970510649691204/posts/1oSFSyv3pRj
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.