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/
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've created a bitmap from canvas. Save it to my "/sdcard/folder/subfolder/file.png"
I want to get this png file into imageview after saving it. I tried this by using BitmapFactory.decodeFile("/sdcard/folder/subfolder/file.png"); method. But it returned nothing. There is no image on imageview.
Do you have any idea?
Try using
String filePath = Environment.getExternalStorageDirectory()+"/folder/subfolder/file.png";//or "folder/subfolder.."
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 want to retrieve an image from my data/data/com.apps.myapp/images folder and display it in an ImageView. Any clue?
Try this :
Bitmap bitmap = BitmapFactory.decodeFile("data/data/com.apps.myapp/images/img.png");
ImageView imgView = (ImageView) this.findViewById(R.id.imgViewId);
imgView.setImageBitmap(bitmap);
There are several components involved in this.
To get the path of your data folder you can use the method getDir in the Context.
Now you have to know the file name and open an stream here again the Context class is your friend. Now the stream can be decoded into a Bitmap via a Bitmap Factory.
After you got a Bitmap create a BitmapDrawable from it and pass it to your ImageView