How to save the edited canvas to image as original image size in android -
i trying draw on image , save image. can succesfully draw on image. facing 2 problems. when draw on image allow draw out side of image also. want draw on image only. second issue when save image getting struched. want save image original size. please me resolve problems.
here post entire code.
public class drawingpaint extends view implements view.ontouchlistener { private canvas mcanvas; private path mpath; private paint mpaint, mbitmappaint; public arraylist<pathpoints> paths = new arraylist<pathpoints>(); private arraylist<pathpoints> undonepaths = new arraylist<pathpoints>(); private bitmap mbitmap; private int color; private int x, y; private string texttodraw = null; private boolean istextmodeon = false; int lastcolor = 0xffff0000; static final float stroke_width = 15f; public drawingpaint(context context/*, int color*/) { super(context); //this.color = color; setfocusable(true); setfocusableintouchmode(true); this.setontouchlistener(this); mbitmappaint = new paint(paint.dither_flag); /*mpaint = new paint(); mpaint.setantialias(true); mpaint.setdither(true); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(stroke_width); mpaint.settextsize(30); mpath = new path(); paths.add(new pathpoints(mpath, color, false)); mcanvas = new canvas();*/ } public void colorchanged(int color) { this.color = color; mpaint.setcolor(color); } public void setcolor(int color) { mpaint = new paint(); this.color = color; mpaint.setantialias(true); mpaint.setdither(true); mpaint.setcolor(color); mpaint.setstyle(paint.style.stroke); mpaint.setstrokejoin(paint.join.round); mpaint.setstrokecap(paint.cap.round); mpaint.setstrokewidth(stroke_width); mpaint.settextsize(30); mpath = new path(); paths.add(new pathpoints(mpath, color, false)); mcanvas = new canvas(); } @override protected void onsizechanged(int w, int h, int oldw, int oldh) { super.onsizechanged(w, h, oldw, oldh); // mbitmap = addreportitemactivity.mphoto; mbitmap = customgalleryhandler.getminstance().getbitmapsend(); float xscale = (float) w / (float) mbitmap.getwidth(); float yscale = (float) h / (float) mbitmap.getheight(); if (xscale > yscale) // make sure both dimensions fit (use // smaller scale) xscale = yscale; float newx = (float) w * xscale; float newy = (float) h * xscale; // use same scale both bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(new file(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()).getabsolutepath(), options); int imageheight = options.outheight; int imagewidth = options.outwidth; m = new matrix(); rectf drawablerect = new rectf(0, 0, imagewidth, imageheight); rectf viewrect = new rectf(0, 0, w, h); m.setrecttorect(drawablerect, viewrect, matrix.scaletofit.center); mbitmap = bitmap.createbitmap(mbitmap,0,0,imagewidth, imageheight, m,true); } @override protected void ondraw(canvas canvas) { float[] pts = {0, 0}; m.mappoints(pts); canvas.drawbitmap(mbitmap, pts[0], pts[1], mbitmappaint); //canvas.drawbitmap(mbitmap, 0, 0, mbitmappaint); (pathpoints p : paths) { mpaint.setcolor(p.getcolor()); log.v("", "color code : " + p.getcolor()); if (p.istexttodraw()) { canvas.drawtext(p.texttodraw, p.x, p.y, mpaint); } else { canvas.drawpath(p.getpath(), mpaint); } } } private float mx, my; private static final float touch_tolerance = 0; private void touch_start(float x, float y) { mpath.reset(); mpath.moveto(x, y); mx = x; = y; } private void touch_move(float x, float y) { float dx = math.abs(x - mx); float dy = math.abs(y - my); if (dx >= touch_tolerance || dy >= touch_tolerance) { mpath.quadto(mx, my, (x + mx) / 2, (y + my) / 2); mx = x; = y; } } private void touch_up() { mpath.lineto(mx, my); // commit path our offscreen mcanvas.drawpath(mpath, mpaint); // kill don't double draw mpath = new path(); paths.add(new pathpoints(mpath, color, false)); } private void drawtext(int x, int y) { this.x = x; this.y = y; paths.add(new pathpoints(color, texttodraw, true, x, y)); // mcanvas.drawtext(texttodraw, x, y, mpaint); } @override public boolean ontouch(view arg0, motionevent event) { float x = event.getx(); float y = event.gety(); switch (event.getaction()) { case motionevent.action_down: if (!istextmodeon) { touch_start(x, y); invalidate(); } break; case motionevent.action_move: if (!istextmodeon) { touch_move(x, y); invalidate(); } break; case motionevent.action_up: if (istextmodeon) { drawtext((int) x, (int) y); invalidate(); } else { touch_up(); invalidate(); } break; } return true; } public void onclickundo() { try { if (paths.size() > 0) { undonepaths.add(paths.remove(paths.size() - 1)); invalidate(); } else { } } catch (exception e) { e.tostring(); } } public void onclickredo() { try { if (undonepaths.size() > 0) { paths.add(undonepaths.remove(undonepaths.size() - 1)); invalidate(); } else { } } catch (exception e) { e.tostring(); } } }
my main activity is:
public class galleryimagefullscreen extends activity implements view.onclicklistener { private imageview mfullscreenimage; private imageview /*mwhitecolor,*//* mgreencolor, mskybluecolor, myellowcolor, mredcolor, mblackcolor,*/ mundoicon, mpaintimagesave, mpaintimagedelete, mredoicon; roundedimageview mwhitecolor, mgreencolor, mskybluecolor, myellowcolor, mredcolor, mblackcolor; // private signature msignature; private relativelayout mdrawlayout; private drawingpaint mdrawviewsignature; private alertdialog malertdialog = null; bitmap bitmapss; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); setcontentview(r.layout.gallery_fullscreen); super.oncreate(savedinstancestate); mundoicon =(imageview)findviewbyid(r.id.erase_icon); mfullscreenimage=(imageview)findviewbyid(r.id.img_fullscreen); mdrawlayout=(relativelayout)findviewbyid(r.id.img_fullscreen_layout); mpaintimagesave=(imageview)findviewbyid(r.id.paint_img_save); mpaintimagedelete=(imageview)findviewbyid(r.id.paint_img_delete); mredoicon=(imageview)findviewbyid(r.id.img_redo); mwhitecolor.setonclicklistener(this); mgreencolor.setonclicklistener(this); mskybluecolor.setonclicklistener(this); myellowcolor.setonclicklistener(this); mredcolor.setonclicklistener(this); mblackcolor.setonclicklistener(this); mundoicon.setonclicklistener(this); mpaintimagesave.setonclicklistener(this); mpaintimagedelete.setonclicklistener(this); mredoicon.setonclicklistener(this); // msignature = new signature(galleryimagefullscreen.this, null); try { intent i=getintent(); bitmap image = null; image = bitmapfactory.decodefile(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); customgalleryhandler.getminstance().setbitmapsend(image); bitmapfactory.options options = new bitmapfactory.options(); options.injustdecodebounds = true; bitmapfactory.decodefile(new file(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()).getabsolutepath(), options); int imageheight = options.outheight; int imagewidth = options.outwidth; mdrawlayout.getlayoutparams().width = imagewidth; mdrawlayout.getlayoutparams().height = imageheight; //relativelayout.layoutparams layout_description = new relativelayout.layoutparams(imagewidth,imageheight); // mdrawlayout.setlayoutparams(layout_description); bitmap msignaturebitmapimage = bitmap.createbitmap(imagewidth, imageheight, bitmap.config.argb_8888); mdrawviewsignature = new drawingpaint(galleryimagefullscreen.this/*, lastcolor*/); mdrawviewsignature.setdrawingcacheenabled(true); mdrawviewsignature.measure( view.measurespec.makemeasurespec(0, view.measurespec.unspecified), view.measurespec.makemeasurespec(0, view.measurespec.unspecified)); mdrawviewsignature.layout(0, 0, mdrawviewsignature.getmeasuredwidth(), mdrawviewsignature.getmeasuredheight()); mdrawviewsignature.builddrawingcache(true); canvas canvas = new canvas(msignaturebitmapimage); drawable bgdrawable = mdrawlayout.getbackground(); if (bgdrawable != null) { bgdrawable.draw(canvas); } else { canvas.drawcolor(color.white); } mdrawlayout.draw(canvas); bytearrayoutputstream bs = new bytearrayoutputstream(); msignaturebitmapimage.compress(bitmap.compressformat.png, 50, bs); } catch (exception e) { logger.d("", "" + e.tostring()); } mdrawviewsignature.setcolor(getresources().getcolor(r.color.black)); mdrawlayout.addview(mdrawviewsignature); } @override public void onclick(view v) { switch (v.getid()) { case r.id.paint_img_save: try { bitmap editedimage = bitmap.createbitmap(mdrawviewsignature .getdrawingcache()); editedimage = bitmap.createscaledbitmap(editedimage, mdrawviewsignature.getwidth(), mdrawviewsignature.getheight(), true); if(editedimage!=null) { final bitmap finaleditedimage = editedimage; malertdialog = alerts.getinstance().createconfirmationdialog(this, this.getresources().getstring(r.string.painting_image_save), new view.onclicklistener() { @override public void onclick(view v) { /*bitmap editedimage = bitmap.createbitmap(mdrawviewsignature .getdrawingcache()); editedimage = bitmap.createscaledbitmap(editedimage, mdrawviewsignature.getwidth(), mdrawviewsignature.getheight(), true);*/ //saveimagetointernalstorage(finaleditedimage); storeimage(finaleditedimage); preferenceforcustomcamera.getinstance().seteditedurl(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); calltogalleyactivity(); malertdialog.dismiss(); } }); malertdialog.show(); } } catch (exception e) { logger.d("paint_img_save",""+e.tostring()); } break; case r.id.paint_img_delete: try { if (!preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen().equals("") && !preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen().equals("null")) { malertdialog = alerts.getinstance().createconfirmationdialog(this, this.getresources().getstring(r.string.painting_image_path_delete), new view.onclicklistener() { @override public void onclick(view v) { // databasehelper.getinstance().deleteparticularvideopath(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); customgalleryhandler.getminstance().deleteparticularimages(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); customgalleryhandler.getminstance().deleteparticularimagefrominternalstorage(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); calltogalleyactivity(); malertdialog.dismiss(); } }); } malertdialog.show(); } catch (exception e) { logger.d("paint_img_delete",""+e.tostring()); } break; case r.id.img_redo: mdrawviewsignature.onclickredo(); break; } } private void saveimagetointernalstorage(bitmap finalbitmap) { try { file mydir = new file(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); mydir.mkdirs(); logger.d("listofphoto",""+mydir.getabsolutepath()); logger.d("listofphotorename",""+preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); for(file files: mydir.listfiles()) { logger.d("listofphoto",""+files.getabsolutepath()); logger.d("listofphotorename",""+preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); } picasso.with(getapplicationcontext()).load(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()).skipmemorycache(); /*random generator = new random(); int n = 10000; n = generator.nextint(n); string fname = "image-" + "3680" + ".png"; file file = new file(mydir, fname); if (file.exists()) { file.delete(); }*/ try { if(mydir.exists()) { mydir.delete(); } fileoutputstream out = new fileoutputstream(mydir); finalbitmap.compress(bitmap.compressformat.png, 90, out); out.flush(); out.close(); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { logger.d("saveimagetointernalstorage",""+e.tostring()); } } private void storeimage(bitmap image) { file picturefile = new file(preferenceforcustomcamera.getinstance().getimagepathforgalleryfullscreen()); if (picturefile.exists()) { picturefile.delete(); } //clearimagediskcache(); try { fileoutputstream fos = new fileoutputstream(picturefile); image.compress(bitmap.compressformat.png, 90, fos); fos.close(); } catch (filenotfoundexception e) { logger.d("galleryimage", "file not found: " + e.getmessage()); } catch (ioexception e) { logger.d("galleryimage", "error accessing file: " + e.getmessage()); } } public boolean clearimagediskcache() { file cache = new file(getapplicationcontext().getcachedir(), "picasso-cache"); if (cache.exists() && cache.isdirectory()) { return deletedir(cache); } return false; } public static boolean deletedir(file dir) { if (dir.isdirectory()) { string[] children = dir.list(); (int = 0; < children.length; i++) { boolean success = deletedir(new file(dir, children[i])); if (!success) { return false; } } } // directory empty delete return dir.delete(); } }
for drawing on bitmap, use src_in porterduffxfermode.
mpaint.setxfermode(new porterduffxfermode(porterduff.mode.src_in));
this write on area have written .
Comments
Post a Comment