android - Rectangular Window For Face Detection -
am particularly new android. making application in want program launches camera takes image , rectangular window painted around face use detect face. here code:
import android.app.activity; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.pointf; import android.media.facedetector; import android.media.facedetector.face; import android.os.bundle; import android.view.menu; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; public class mainactivity extends activity { final int camera_pic_request = 0; private int imagewidth, imageheight; private int numberofface = 5; private facedetector myfacedetect; private facedetector.face[] myface; float myeyesdistance; int numberoffacedetected; bitmap thumbnail = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); button strt = (button) findviewbyid(r.id.start_camera); strt.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent camera_intent = new intent(android.provider.mediastore.action_image_capture); startactivityforresult(camera_intent, camera_pic_request); bitmapfactory.options bitmapfactoryoptionsbfo = new bitmapfactory.options(); bitmapfactoryoptionsbfo.inpreferredconfig = bitmap.config.rgb_565; imagewidth = thumbnail.getwidth(); imageheight = thumbnail.getheight(); myface = new facedetector.face[numberofface]; myfacedetect = new facedetector(imagewidth, imageheight, numberofface); numberoffacedetected = myfacedetect.findfaces(thumbnail, myface); canvas canvas = null; canvas.drawbitmap(thumbnail, 0, 0, null); paint mypaint = new paint(); mypaint.setcolor(color.green); mypaint.setstyle(paint.style.stroke); mypaint.setstrokewidth(3); for(int i=0; < numberoffacedetected; i++) { face face = myface[i]; pointf mymidpoint = new pointf(); face.getmidpoint(mymidpoint); myeyesdistance = face.eyesdistance(); canvas.drawrect( (int)(mymidpoint.x - myeyesdistance), (int)(mymidpoint.y - myeyesdistance), (int)(mymidpoint.x + myeyesdistance), (int)(mymidpoint.y + myeyesdistance), mypaint); } } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch(requestcode){ case 0: if(resultcode==result_ok){ thumbnail = (bitmap) data.getextras().get("data"); } } } @override protected void onsaveinstancestate(bundle outstate) { } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
the logcat shows:
08-20 12:13:16.218: e/androidruntime(26801): fatal exception: main 08-20 12:13:16.218: e/androidruntime(26801): java.lang.runtimeexception: unable resume activity {com.mh9.makeover/com.mh9.makeover.mainactivity}: java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=0, result=-1, data=intent { act=inline-data (has extras) }} activity {com.mh9.makeover/com.mh9.makeover.mainactivity}: java.lang.nullpointerexception 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.performresumeactivity(activitythread.java:2124) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.handleresumeactivity(activitythread.java:2139) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1672) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.access$1500(activitythread.java:117) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread$h.handlemessage(activitythread.java:935) 08-20 12:13:16.218: e/androidruntime(26801): @ android.os.handler.dispatchmessage(handler.java:99) 08-20 12:13:16.218: e/androidruntime(26801): @ android.os.looper.loop(looper.java:130) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.main(activitythread.java:3687) 08-20 12:13:16.218: e/androidruntime(26801): @ java.lang.reflect.method.invokenative(native method) 08-20 12:13:16.218: e/androidruntime(26801): @ java.lang.reflect.method.invoke(method.java:507) 08-20 12:13:16.218: e/androidruntime(26801): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:867) 08-20 12:13:16.218: e/androidruntime(26801): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:625) 08-20 12:13:16.218: e/androidruntime(26801): @ dalvik.system.nativestart.main(native method) 08-20 12:13:16.218: e/androidruntime(26801): caused by: java.lang.runtimeexception: failure delivering result resultinfo{who=null, request=0, result=-1, data=intent { act=inline-data (has extras) }} activity {com.mh9.makeover/com.mh9.makeover.mainactivity}: java.lang.nullpointerexception 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.deliverresults(activitythread.java:2536) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.performresumeactivity(activitythread.java:2111) 08-20 12:13:16.218: e/androidruntime(26801): ... 12 more 08-20 12:13:16.218: e/androidruntime(26801): caused by: java.lang.nullpointerexception 08-20 12:13:16.218: e/androidruntime(26801): @ com.mh9.makeover.mainactivity.onactivityresult(mainactivity.java:78) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activity.dispatchactivityresult(activity.java:3908) 08-20 12:13:16.218: e/androidruntime(26801): @ android.app.activitythread.deliverresults(activitythread.java:2532) 08-20 12:13:16.218: e/androidruntime(26801): ... 13 more
now have 2 questions here.first why getting exception mean? correction explanation appreciated. 2nd question: accomplish trying do?? mean after image taken , saved want pass drawing rectugular window around face!
the nullpointerexception encountered due having no coming camera intent in onactivityresult() method @ line :
thumbnail = (bitmap) data.getextras().get("data");
this occurs in (high resolution) devices because devices need have camera intent specified file captured image should stored.
instead of :
intent camera_intent = new intent(android.provider.mediastore.action_image_capture); startactivityforresult(camera_intent, camera_pic_request);
try :
// create intent take picture , return control calling application intent intent = new intent(mediastore.action_image_capture); fileuri = getoutputmediafileuri(media_type_image); // create file save image intent.putextra(mediastore.extra_output, fileuri); // set image file name // start image capture intent startactivityforresult(intent, capture_image_activity_request_code);
and instead of onactivityresult method :
if(resultcode==result_ok){ thumbnail = (bitmap) data.getextras().get("data"); }
try :
if (requestcode == capture_image_activity_request_code) { if (resultcode == result_ok) { // image captured , saved fileuri specified in intent toast.maketext(this, "image saved to:\n" + data.getdata(), toast.length_long).show(); } else if (resultcode == result_canceled) { // user cancelled image capture } else { // image capture failed, advise user } }
for detailed info visit here documentation
bestof luck !
Comments
Post a Comment