how to crop face from image in android -
i want crop face image in android . after searching lot in internet , i have come know tutorial . have imageview .
iv1 = (imageview) mainactivity.this.findviewbyid(r.id.img1);
when tap imageview pick image gallery . code follows :
iv1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub intent photopickerintent = new intent(intent.action_pick); photopickerintent.settype("image/*"); startactivityforresult(photopickerintent, select_photo); imgno = 1; } });
in onactivityresult method , trying crop face image according following code :
if (resultcode == result_ok && imgno == 1 ) { selectedimage = imagereturnedintent.getdata(); try { imagestream = getcontentresolver().openinputstream( selectedimage); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } yourselectedimage = bitmapfactory.decodestream(imagestream); // iv1.setimagebitmap(yourselectedimage); path1 = selectedimage.getpath(); viewheight = part2.getmeasuredheight(); viewwidth = part2.getmeasuredwidth(); try { paint paint = new paint(); paint.setfilterbitmap(true); bitmap bitmaporg = yourselectedimage; int targetwidth = bitmaporg.getwidth(); int targetheight = bitmaporg.getheight(); bitmap targetbitmap = bitmap.createbitmap(targetwidth, targetheight, bitmap.config.argb_8888); rectf rectf = new rectf(0, 0, viewwidth, viewheight); canvas canvas = new canvas(targetbitmap); path path = new path(); path.addrect(rectf, path.direction.cw); canvas.clippath(path); canvas.drawbitmap( bitmaporg, new rect(0, 0, bitmaporg.getwidth(), bitmaporg .getheight()), new rect(0, 0, targetwidth, targetheight), paint); matrix matrix = new matrix(); matrix.postscale(1f, 1f); bitmapfactory.options bitmapfatoryoptions = new bitmapfactory.options(); bitmapfatoryoptions.inpreferredconfig = bitmap.config.rgb_565; bitmaporg = yourselectedimage; myface = new facedetector.face[5]; myfacedetect = new facedetector(targetwidth, targetheight, 5); int numberoffacedetected = myfacedetect.findfaces( bitmaporg, myface); bitmap resizedbitmap = null; if (numberoffacedetected > 0) { pointf mymidpoint = null; face face = myface[0]; mymidpoint = new pointf(); face.getmidpoint(mymidpoint); myeyesdistance = face.eyesdistance() + 20; if (mymidpoint.x + viewwidth > targetwidth) { while (mymidpoint.x + viewwidth > targetwidth) { mymidpoint.x--; } } if (mymidpoint.y + viewheight > targetheight) { while (mymidpoint.y + viewheight > targetheight) { mymidpoint.y--; } } resizedbitmap = bitmap.createbitmap(bitmaporg, (int) (mymidpoint.x - myeyesdistance), (int) (mymidpoint.y - myeyesdistance), viewwidth, viewheight, matrix, true); } else { resizedbitmap = bitmap.createbitmap(bitmaporg, 0, 0, viewwidth, viewheight, matrix, true); } /* convert bitmap resource */ // bitmap resizedbitmap = bitmap.createbitmap(targetbitmap, // 0, // 0, viewwidth, viewheight, matrix, true); bitmapdrawable bd = new bitmapdrawable(resizedbitmap); iv1.setimagebitmap(getcroppedbitmap(bd.getbitmap())); } catch (exception e) { system.out.println("error1 : " + e.getmessage() + e.tostring()); } iv1.invalidate(); }
but code not able crop face image picked gallery . how can crop face image ? advice of great .
you can try facedetector built in class
facedetector.face[] face=new facedetector.face[20]; facedetector fd=new facedetector(200,200,2); imageview v=(imageview)findviewbyid(r.id.imageview); bitmapdrawable draw=(bitmapdrawable)v.getdrawable(); fd.findfaces(draw.getbitmap(), face); //now face hold array face image
Comments
Post a Comment