Getting a Bitmap from resources using the code below and then drawing it to canvas gives me no error.
mLetterA = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.lettera), mScaledCardW, mScaledCardH, false);
But if I set the Bitmap in a class called Letter for example, then try get the Bitmap using the getter, say, getLetterGetBMP(), then I always get an error when I try to draw it on canvas. Why is that?
Related
I am trying to draw an image from a file, a png file.
I need to draw it as a bitmap on a canvas but i cant get the file into the bitmap object without getting an error, after trying for a lot of time i need some help.
example of what i tried:
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.myimage);
thanks!
How can I check if a Bitmap object is completely blank, i.e. all its pixels are transparent, without a x-y loop on every pixel?
You can check your Bitmap instance (in the example myBitmap) against an empty one with:
Bitmap emptyBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), myBitmap.getConfig());
if (myBitmap.sameAs(emptyBitmap)) {
// myBitmap is empty/blank
}
You can do this very easy but it depends on the application.
If you have an application that prompts the user for a drawing input, like signature or anything similar, you will usually have an ArrayList of Paths which are drawn to the Canvas of that View. You can do a check when you want to return the BitMap look to see if the ArrayList of Paths is bigger than 0 and return the BitMap if so, or else return null.
I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:
eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:
Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...
This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).
I'm tryimng to see if I can creat dynamic images by creating a bitmap and using setPixel. The program crashes when I call setPixel,
Bitmap bm= createBitmap (50,50, Bitmap.Config.RGB_565);
// program crashes here
bm.setPixel(25,25,0xffffff);
// add a test viue
ImageView mImage= new ImageView(this);
mImage.setImageBitmap(bm);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
layout.addView(mImage);
I figure it is something simple, but cannot figure it out.
Ted
Can you paste the error message for more information ?
You're using Bitmap.Config.RGB_565, did you have tried with Bitmap.Config.ARGB_8888 instead ?
I am able to draw a signature on my canvas. Now after that I want to save this signature as an image which can be used later.
I am unable to implement an onClicklistener on this canvas. Also I am unclear as to how this signature can be stored as an image. Please suggest?
Try this:
Bitmap bmp = Bitmap.createBitmap(...);
Canvas can = new Canvas(bmp);
When you change your canvas, bitmap bmp will change too. Canvas is only raference to Bitmap canvas, and you have no need to save canvas. Save only Bitmap (bmp).
To save the canvas drawing as an image, you need to convert it to a data url using the toDataURL method. Once you have the data url, you can use it to set the source of an image element so the user can right click and download the image.:
// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();
// set canvasImg image src to dataURL
// so it can be saved as an image
document.getElementById("canvasImg").src = dataURL;
Reference: http://www.html5canvastutorials.com/advanced/html5-canvas-save-drawing-as-an-image/