How to do custom image cropping in android -
in application, need take images gallery/camera, crop images, save cropped images else. below code of that, cannot crop images liking. using below code, can crop images using 4 coordinates, top, bottom, left , right side of image middle coordinates; need crop using 8 coordinates. this image shows mean.
public class mainactivity extends activity { private static final int pick_from_camera = 1; private static final int pick_from_gallery = 2; private static final int press_ok = 3; imageview imgview; string m_path; bitmap m_thepic; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imgview = (imageview) findviewbyid(r.id.imageview1); button buttoncamera = (button) findviewbyid(r.id.btn_take_camera); button buttongallery = (button) findviewbyid(r.id.btn_select_gallery); button buttonok = (button) findviewbyid(r.id.btn_ok); file folder = new file(environment.getexternalstoragedirectory().tostring() + "/images/"); folder.mkdirs(); buttoncamera.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // call android default camera intent intent = new intent(mediastore.action_image_capture); intent.putextra(mediastore.extra_output, mediastore.images.media.external_content_uri.tostring()); // ******** code crop image intent.putextra("crop", "true"); intent.putextra("aspectx", 0); intent.putextra("aspecty", 0); intent.putextra("outputx", 100); intent.putextra("outputy", 150); try { intent.putextra("return-data", true); startactivityforresult(intent, pick_from_camera); } catch (activitynotfoundexception e) { // nothing } } }); buttongallery.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(); // call android default gallery intent.settype("image/*"); intent.setaction(intent.action_get_content); // ******** code crop image intent.putextra("crop", "true"); intent.putextra("aspectx", 0); intent.putextra("aspecty", 0); intent.putextra("outputx", 200); intent.putextra("outputy", 150); try { intent.putextra("return-data", true); startactivityforresult(intent.createchooser(intent, "complete action using"), 1); } catch (activitynotfoundexception e) { // nothing } } }); buttonok.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { string m_path = environment.getexternalstoragedirectory().tostring(); file m_imgdirectory = new file(m_path + "/images/"); file m_file = new file(m_path); string m_fileid = "nm_tech" + system.currenttimemillis() + ""; m_file = new file(m_path, "/images/" + m_fileid + ".jpg"); uri outputfileuri = uri.fromfile(m_file); intent intent = new intent(mainactivity.this, imagegallerydemoactivity.class); intent.putextra("image", m_fileid); startactivity(intent); // startactivityforresult(intent,press_ok); // call android default camera // toast.maketext(getapplicationcontext(), ,1234).show(); } }); } protected void onactivityresult(int requestcode, int resultcode, intent data) { bundle extras = data.getextras(); bitmap m_thepic = extras.getparcelable("data"); string m_path = environment.getexternalstoragedirectory().tostring(); file m_imgdirectory = new file(m_path + "/images/"); if (!m_imgdirectory.exists()) { m_imgdirectory.mkdir(); } outputstream m_fout = null; file m_file = new file(m_path); m_file.delete(); string m_fileid = "nm_tech" + system.currenttimemillis() + ""; m_file = new file(m_path, "/images/" + m_fileid + ".jpg"); try { if (!m_file.exists()) { m_file.createnewfile(); } m_fout = new fileoutputstream(m_file); bitmap m_bitmap = m_thepic.copy(bitmap.config.argb_8888, true); m_bitmap.compress(bitmap.compressformat.png, 100, m_fout); m_fout.flush(); m_fout.close(); mediastore.images.media.insertimage(getcontentresolver(), m_file.getabsolutepath(), m_file.getname(), m_file.getname()); } catch (exception p_e) { } if (requestcode == pick_from_camera) { if (extras != null) { // bitmap photo = extras.getparcelable("data"); imgview.setimagebitmap(m_thepic); } } if (requestcode == pick_from_gallery) { // bundle extras2 = data.getextras(); if (extras != null) { imgview.setimagebitmap(m_thepic); } } if (requestcode == press_ok) { bundle extras11 = data.getextras(); bitmap bmp = (bitmap) extras.get("data"); /* * bitmap photo = extras.getparcelable("data"); * imgview.setimagebitmap(photo); intent n=new * intent(getapplicationcontext(),imagegallerydemoactivity.class); * n.putextra("data",photo); startactivity(n); */ } } }
here simple way create minimum needed rect array of coordinates
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); rect r = getrect(new int[]{10, 10, 20, 20, 30, 30}, new int[]{20, 100, 10, 110, 20, 100}); system.out.println(r.left + " " + r.top + " " + r.bottom + " " + r.right); } public rect getrect(int[] x, int y[]){ rect r = new rect(); // set first coordinate, in order not include 0, 0 r.set(x[0], y[0], x[0], y[0]); for(int = 1; < x.length; i++){ r.union(x[i], y[i]); } return r; }
edit: have 8 points, call getrect this
rect recttodraw = getrect(new int{yourx1, yourx2, yourx3, yourx4, yourx5, yourx6, yourx7, yourx8,}, new int{youry1, youry2, youry3, youry4, youry5, youry6, youry7, youry8});
you can pass function ass many points want, not 8
hope helps , enjoy work
Comments
Post a Comment