getDrawingCache distorted quality of bitmap - android

I am trying to take screenshot of a view using getDrawingCache() method. When it gives bitmap that bitmap have not the same quality which i can see on the application. Below is the code which i am using :
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
ByteArrayOutputStream streamOut = new ByteArrayOutputStream();
cacheBitmap.compress(Bitmap.CompressFormat.PNG, 100, streamOut);
byte[] bitmapDataArry = streamOut.toByteArray();
I already tried few stuff which i got from google and SO. Like this improve quality of getdrawingcache and improve quality of image. But still not getting the pure image quality. Can anyone have any suggestion about this or any else method for taking screenshot without loosing quality.

I guess your problem is that you taka a screenshot on a small screen device and there the drawing cache is smaller and therefore the screenshot itself is smaller but looks ok because of the phone screen which is also small. If you then upload this image that you've got from getDrawingCache to your server and then try to show it on another device with higher resolution screen the quality will be grainy and pixelated. If you are in this case then showing the original image or cropped version of it will help you maintain quality.
There is another case when your view might be getting the old cache.
Here you can destroyDrawingCache() and then buildDrawingCache().

Related

Saving Bitmap Image without shrinking width and height

What im trying to do is to save an edited bitmap that is composed by 2 bitmaps overlayed. My application allows the user to draw on top of a picture and save it.
My problem is: when i save the result image, it gets smaller, even setting the quality to 100. So, if the user saves and edit the image multiple times, the image will get smaller and smaller.
I save the bitmap with this:
result.compress(Bitmap.CompressFormat.JPEG, 100, fos);
I debugged the code, and at this point the width and height are fine, but after saving, the image shrinks.
I've researched for questions about this, but the ones i've found had no answers that could help me.
What i need is a way to save a bitmap without shrinking it, but i need it to be in image format, like JPG, PNG, etc.
Thanks in advance.
I guess is not at the time when you are saving the image, the issue is when you are editing the image (creating layers). Check that mechanism, maybe you are setting the image size there.

Save Bitmap of Full Size Image After Scaling

My application uses a SurfaceView to show an image to the user, and have them manipulate stickers on top of the image that has been taken. To reduce memory usage, I have scaled all of these bitmaps to fit within the screen. Now I want to save the image that the user just put all of the stickers on, and I would like to save it with the resolution of the original image. How would a go about doing this without loading the full-size images into memory and risking an OutOfMemoryError? I do not know where to start with this, it seems like an impossible task with the given tools.
In case if you want to save the bitmap without downsizing it. There are two options.
Use largeHeap=true in your Application tag (Manifest).
If you can afford a bit of loss in quality! Compress the bitmap using.
Sample Code
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
Below is the link to the documentation of compress method of Bitmap class.
http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)
I would do the following: You have to decouple the editing and the composition. In editing you save what the user wants in some POJOs ( e.g. sticker number A at position X,Y with rotation R )
Then in the composition-step you work with the Hi-Res images - but only one by one to save memory.
In the worst case you have to work with tiles if you still hit memory constraints.

Resource to bitmap conversion results in poor quality

I never really noticed it before but when I change the image of an imageview using setImageBitmap, using a bitmap which is decoded from resources using BitmapFactory, then the quality of image deteriorates and I don't know why.
I even played around with BitmapFactoryOptions like options.inPreferredConfig, options.inJustDecodeBounds, options.inDither, but the results were pretty much the same; a poor quality image.
On the other if I just use setImageResource, the image doesn't deteriorates and is in best quality possible.
So basically these two codes
Bitmap b=BitmapFactory.decodeResource(getResources(), R.drawable.keypad,options); iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.keypad))
iv.setImageResource(R.drawable.messages);
results in different image quality.
Can anybody explain why? And how to solve this quality issue using code 1.
If you have blurry images using the BitmapFactory.decodeResource method, you can use this code:
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap b = BitmapFactory.decodeResource(getResources(), path, options);
iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.keypad));
Setting the inScaled parameter of BitmapFactory.Options class to false, will prevent blurry images on low screen resolutions since it will prevent scalling as mentioned in the answer of this previous SO question. Maybe you have already tried this but i thought worth mentioning.

Large bitmap for background giving OOM error

I have a bitmap that I'm loading into the surfaceView that is around 75kb (give or take)
The code I'm using to call it or decode it is
bmLargeImage = BitmapFactory.decodeResource(getResources(),R.drawable.campplan);
nothing fancy really. The size of the image is 1000x500 pix. Which load fine when I want to. Problem is I want it to be 2000x1000 which is double that size.
I tried the resize with this code
bmLargeImage = Bitmap.createScaledBitmap(bmLargeImage, 2000, 1000, true);
but it gives me a OOM error. I tried saving the png file as a 2000x1000 pix image and again it gives the OOM exception. This is tested in an emulator so far. But I think the size should be enough to load, what am I missing?
Note: I want it this big cause it's a background to a game (think like farmvile or smurf's village game background) so it should be big and scrollable, yet I only manage the 1000x500 size.
check this link http://developer.sonyericsson.com/wp/2011/06/27/how-to-scale-images-for-your-android%E2%84%A2-application/ might be helpful to you as
as there is some problem with createScaledBitmap http://code.google.com/p/android/issues/detail?id=13038

OutOfMemoryError when opening lot of little pictures [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
OutOfMemoryError: bitmap size exceeds VM budget :- Android
In my application I load some little Bitmap(between 2k and 300k) that I affect to ImageViews, during the first launch of my App it works fine but when I quit it and relaunch it, I always have the OutOfMemoryError during a Bitmap creation.
Could somebody tell me why?
The problem is because your bitmap's size is too large than the VM can handle. For example from your code I can see that you are trying to paste an Image into imageView which you are capturing using Camera. So normally the camera images will be too large in size which will rise this error obviously. So as others have suggested, you have to compress your image either by sampling it or convert your image into smaller resolution. For example if your imageView is 100x100 in width and height, you can create a scaled bitmap so that your imageView gets filled exactly. You can do this for that,
Bitmap newImage = Bitmap.createScaledBitmap(bm, 350, 300,true);
And you have to recycle your bitmap and null your bitmap like this,
Bitmap bmap.recycle();
Bitmap bmap=null;
You should use bitmap.recycle() manually when you leave your activity.
Please refer to http://developer.android.com/reference/android/graphics/Bitmap.html#recycle()
I really doubt you have to use bitmap.recycle() if you null your references, allowing GC (but please prove me wrong).
I'd say it's more likely your bitmaps are too large, possibly aggravated by long lived Context references or somesuch.

Categories

Resources