Converting Bitmap in memory to Bitmap with Bitmap.Config.RGB_565 - android

I have a loaded Bitmap which I would like to convert to set the config to Bitmap.Config.RGB_565. Is there a simple way of converting a Bitmap to this configuration after the Bitmap is already loaded into memory? For example, below I have a bitmap being decoded from the application resources, however, how would I convert an already loaded Bitmap to RGB_565? I'm sure it's something simple, however, I'm fairly new to working with Bitmaps and after a few hours of looking online, unfortunately I couldn't find what I needed specifically.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig=Bitmap.Config.RGB_565
bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.myphoto ,options);

You can also try this:
Bitmap converted = original.copy(Config.RGB_565, false);
From the documentation of Bitmap.copy():
Tries to make a new bitmap based on the dimensions of this bitmap, setting the new bitmap's config to the one specified, and then copying this bitmap's pixels into the new bitmap. If the conversion is not supported, or the allocator fails, then this returns NULL.
Looking through the native source code, you should be fine converting between any values of Bitmap.Config.

I haven't tested this but it should work:
private Bitmap convert(Bitmap bitmap, Bitmap.Config config) {
Bitmap convertedBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), config);
Canvas canvas = new Canvas(convertedBitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, paint);
return convertedBitmap;
}
call the methods like this:
Bitmap convertedBitmap = convert(bitmap, Bitmap.Config.RGB_565);
You can do all kinds of additional transformations like rotating, stretching etc. if you use the drawBitmap with a Matrix.

Found the answer here https://stackoverflow.com/a/12148450/1364673, thanks to
siliconeagle.
The solution is to create a new bitmap with the required encoding as per link above example.

Related

Bitmap not large enough to support new configuration

I have a Bitmap and i want to configure it. I also set my bitmap to Mutable one that's not a problem but when i want to configure, it comes with a error that my bitmap is not large enough.
Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.ic_cm);
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888,true);
bitmap.setWidth(getWidth() / 2);

using the BitmapRegionDecoder when there is also a matrix

using the BitmapRegionDecoder to decode partial areas works fine in the general use.
however, i've come to a piece of code where i had to replace the creation of a bitmap by using a BitmapRegionDecoder , but here's the catch: the creation of the new bitmap used both a rectangle and a matrix, while i know for sure that the matrix was used only for translation and scaling.
how would i convert such a code to be used by BitmapRegionDecoder ?
for example, how do i convert this:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.android);
Matrix matrix = new Matrix();
//<=mess around with the matrix, but only translations and scaling.
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,width, height, matrix, true);
into something that uses the BitmapRegionDecoder.decodeRegion ?
i've tried to use Matrix.mapRect , but i don't think it makes sense, as i've got weird results on the output bitmap compared to the original code.

Merging image from camera with image from drawables

I´m trying to merge 2 images, one is bitmap from camera, second one is .png file stored in drawables. What I did was that I used both images as bitmaps and I tried to merge them by using canvas, something like this:
Bitmap topImage = BitmapFactory.decodeFile("gui.png");
Bitmap bottomImage = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Canvas canvas = new Canvas(bottomImage);
canvas.drawBitmap(topImage, 0, 0, null);
But I keep getting "Bitmap size exceeds VM budget" error all the time. I tried nearly everything, but still, it keeps throwing this error. Is there another way of merging 2 images? What i need to do is simple - I need to take photo and save it merged with that .PNG image stored in drawables. For example this app is very close to what i need - https://play.google.com/store/apps/details?id=com.hl2.hud&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5obDIuaHVkIl0.
Thanks :)
See the below code for combining two images.
This method returns combined bitmap
public Bitmap combineImages(Bitmap frame, Bitmap image) {
Bitmap cs = null;
Bitmap rs = null;
rs = Bitmap.createScaledBitmap(frame, image.getWidth() + 50,
image.getHeight() + 50, true);
cs = Bitmap.createBitmap(rs.getWidth(), rs.getHeight(),
Bitmap.Config.RGB_565);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(image, 25, 25, null);
comboImage.drawBitmap(rs, 0, 0, null);
if (rs != null) {
rs.recycle();
rs = null;
}
Runtime.getRuntime().gc();
return cs;
}
You can change height and width as per your requirements
Hope this will help...
How large are the images? I've only encountered this problem when trying to load large images into memory.
Is the byte array your decoding actually an image?
From a quick look at the android docs you can capture an image using the default camera app which may work in this situation.
http://developer.android.com/training/camera/photobasics.html
Also see this question: Capture Image from Camera and Display in Activity
Edit: You may also need to scale the image from the camera down if it is very large. See the end of the android page I linked to for details on that.

How do I made a deep copy a BitmapDrawable?

I'm having trouble cloning a BitmapDrawable. I tried the answer in this post but it creates a "shallow" copy, and I need a "deep" copy so I can alter the pixels in the clone without affecting the original.
I also tried this:
Bitmap bitmap = bdOriginal.getBitmap();
BitmapDrawable bdClone = new BitmapDrawable(getResources(), bitmap.copy(bitmap.getConfig(), true));
But it seems to create an immutable clone even though I set the mutable parameter in Bitmap.copy() to "true". That is, color filters don't appear to change the clone. Am I doing it wrong? (EDIT: I used the debugger to confirm bitmap.mIsMutable = true)
To summarize, I need a clone of a BitmapDrawable that can be altered with color filters without affecting the original. Any suggestions?
Thanks in advance...
Create new Bitmap of the same size.
Create canvas for this new Bitmap
Draw your first Bitmap into this canvas.
Example:
Bitmap copy = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());
Canvas copiedCanvas = new Canvas(copy);
copiedCanvas.drawBitmap(original, 0f, 0f, null);

Android bitmap quality issues

In my app, the bitmap is drawn as if the color is some lower quality type. If i load up the background image using the gallery app, it loads just fine and does not look like it's super low quality. The code i am using to load and draw my images is simple:
//Code for initializing the Bitmap
Bitmap bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.none), (int) (canvas.getWidth() * compression), (int) (canvas.getHeight() * compression), true);
//...
//Code for drawing this Bitmap
canvas.drawBitmap(bitmap, null, new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), null);
If nothing in the code tells you what is wrong, i made an image comparing what the image actually looks like on a computer or other image viewer, and what it looks like in the app.
question is somewhat similar to Bad image quality after resizing/scaling bitmap
try disabling scaling, resize in an offscreen bitmap and make sure that Bitmap is 32 bits (ARGB888):
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
another good and complete answer about image scaling/processing can be found at Quality problems when resizing an image at runtime

Categories

Resources