android怎么实现手势划定区域裁剪图片-mile米乐体育

android怎么实现手势划定区域裁剪图片

这篇文章主要介绍“android怎么实现手势划定区域裁剪图片”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“android怎么实现手势划定区域裁剪图片”文章能帮助大家解决问题。

需求:

拍照,然后对图片进行处理,划定矩形区域,将矩形区域裁剪下来

思路:

1、使用系统相机拍照,拍完返回,对图片进行压缩和存储。

2、新建一个activity处理图片裁剪,利用自定义view在画布上画出矩形区域。

3、根据坐标信息生成裁剪图片并存储。

部分核心代码:

1、调用系统相机拍照

stringimage_path=environment.getexternalstoragedirectory().getpath() "/com.kwmax.demo/image/";stringfilename="xxxxxx.jpeg";filepicfile=newfile(image_path filename);if(!picfile.exists()){picfile.createnewfile();}...if(getcontext().getpackagemanager().getlaunchintentforpackage("com.sec.android.app.camera")!=null){cameraintent.setpackage("com.sec.android.app.camera");}if(getcontext().getpackagemanager().getlaunchintentforpackage("com.android.hwcamera")!=null){cameraintent.setpackage("com.android.hwcamera");}if(getcontext().getpackagemanager().getlaunchintentforpackage("com.zte.camera")!=null){cameraintent.setpackage("com.zte.camera");}cameraintent.setaction(mediastore.action_image_capture);//默认系统相机cameraintent.addcategory("android.intent.category.default");uripictureuri=uri.fromfile(picfile);cameraintent.putextra(mediastore.extra_output,pictureuri);startactivityforresult(intent,camera_request_code);

2、自定义手势矩形view

publicclasscapturerectviewextendsview{privateintx;privateinty;privateintm;privateintn;privatebooleansign;//绘画标记位privatepaintpaint;//画笔publiccapturerectview(contextcontext){super(context);paint=newpaint(paint.filter_bitmap_flag);}@overrideprotectedvoidondraw(canvascanvas){if(sign){paint.setcolor(color.transparent);}else{paint.setcolor(color.red);paint.setalpha(80);paint.setstyle(paint.style.stroke);paint.setstrokewidth(15f);canvas.drawrect(newrect(x,y,m,n),paint);}super.ondraw(canvas);}publicvoidsetseat(intx,inty,intm,intn){this.x=x;this.y=y;this.m=m;this.n=n;}publicbooleanissign(){returnsign;}publicvoidsetsign(booleansign){this.sign=sign;}}

3、裁剪页面布局

4、裁剪activity

publicclassdrawrectactivityextendsbasicactivityimplementsonclicklistener,view.ontouchlistener{privatestringtag="drawrectactivity";privatestringimagestring;privatestringimagepath;privatearraylistimagelist=null;privateintposition=0;privateintwidth,height;privatelinearlayoutlayerviewlayout=null;privateimageviewaipreview;privatecapturerectviewcaptureview;//绘画选择区域privateintcapx;//绘画开始的横坐标privateintcapy;//绘画开始的纵坐标privateintcapm;//绘画结束的横坐标privateintcapn;//绘画结束的纵坐标privatebitmapcapturebitmap;privatebuttoncancel;privatebuttonaicapture;privateframelayoutframelayout;privaterelativelayoutrelativelayout;@overrideprotectedvoidoncreate(bundlesavedinstancestate){super.oncreate(savedinstancestate);init();initui();}privatevoidinit(){width=imageutils.getscreenwidth(this);height=imageutils.getscreenheight(this);intentintent=this.getintent();bundlebundle=intent.getextras();imagestring=bundle.getstring("imagestring");imagepath=bundle.getstring("imagepath");position=bundle.getint("position");imagelist=parseimagestring(imagepath,imagestring);}@targetapi(build.version_codes.jelly_bean)privatevoidinitui(){setcontentview(r.layout.draw_image_rect_view);framelayout=(framelayout)findviewbyid(r.id.drawrect_framelayout);relativelayout=(relativelayout)findviewbyid(r.id.drawrect_relativelayout);layerviewlayout=(linearlayout)this.findviewbyid(r.id.image_zoom_view_layout);btncancel=(button)findviewbyid(r.id.btn_cancel);btncapture=(button)findviewbyid(r.id.btn_capture);btnpreview=(imageview)findviewbyid(r.id.capture_preview);imagevieworiginimage=newimageview(this);bitmapimage=imageutils.getbitmapfromfile(imagepath imagelist.get(position),1);originimage.setimagebitmap(image);originimage.setlayertype(view.layer_type_software,null);layerviewlayout.addview(originimage,newlinearlayout.layoutparams(viewgroup.layoutparams.match_parent,viewgroup.layoutparams.match_parent));captureview=newcapturerectview(this);originimage.setontouchlistener(this);this.addcontentview(captureview,newviewgroup.layoutparams(viewgroup.layoutparams.wrap_content,viewgroup.layoutparams.wrap_content));if(framelayout.isclickable()){framelayout.setonclicklistener(newonclicklistener(){@overridepublicvoidonclick(viewv){}});}btncancel.setonclicklistener(this);btncapture.setonclicklistener(this);}privatearraylistparseimagestring(stringimagepath,stringimagestring){arraylistlist=newarraylist();stringallfiles=imagestring.substring(imagestring.indexof("img://") "img://".length());stringfilename=null;while(allfiles.indexof(";")>0){filename=allfiles.substring(0,allfiles.indexof(";"));allfiles=allfiles.substring(allfiles.indexof(";") 1);if(checkisimagefile(filename)&&newfile(imagepath filename).exists()){list.add(filename);log.v("parseimagestring()","imagename=" filename);}else{log.v("parseimagestring()","badimagename=" filename);}}log.v("parseimagestring()","imagelist.size=" list.size());returnlist;}/***判断是否相应的图片格式*/privatebooleancheckisimagefile(stringfname){booleanisimageformat;if(fname.endswith(".jpg")||fname.endswith(".gif")||fname.endswith(".png")||fname.endswith(".jpeg")||fname.endswith(".bmp")){isimageformat=true;}else{isimageformat=false;}returnisimageformat;}@overridepublicbooleanontouch(viewview,motioneventevent){switch(event.getaction()){casemotionevent.action_down:capx=0;capy=0;width=0;height=0;capx=(int)event.getx();capy=(int)event.gety();break;casemotionevent.action_move:capm=(int)event.getx();capn=(int)event.gety();captureview.setseat(capx,capy,capm,capn);captureview.postinvalidate();break;casemotionevent.action_up:if(event.getx()>capx){width=(int)event.getx()-capx;}else{width=(int)(capx-event.getx());capx=(int)event.getx();}if(event.gety()>capy){height=(int)event.gety()-capy;}else{height=(int)(capy-event.gety());capy=(int)event.gety();}capturebitmap=getcapturepreview(this);if(null!=capturebitmap){btnpreview.setimagebitmap(capturebitmap);}break;}if(captureview.issign()){returnfalse;}else{returntrue;}}privatebitmapgetcapturepreview(activityactivity){viewview=activity.getwindow().getdecorview();view.setdrawingcacheenabled(true);view.builddrawingcache();bitmapbitmap=view.getdrawingcache();rectframe=newrect();activity.getwindow().getdecorview().getwindowvisibledisplayframe(frame);inttoheight=frame.top;//todo:这里需要针对部分机型做适配if(width>0&&height>0){bitmap=bitmap.createbitmap(bitmap,capx,capy 240,width,height);view.setdrawingcacheenabled(false);returnbitmap;}else{returnnull;}}@overridepublicvoidonclick(viewv){switch(v.getid()){caser.id.btn_cancel:intentcancelintent=getintent();creatependingresult(600,cancelintent,pendingintent.flag_update_current);setresult(result_ok,cancelintent);finish();break;caser.id.btn_capture:intentsureintent=getintent();creatependingresult(cpaiphotoattributes.photo_capture,sureintent,pendingintent.flag_update_current);if(capturebitmap!=null){try{stringfile=image_path;stringrandomid=uuid.randomuuid().tostring();stringfilename=randomid ".jpeg";fileoutputstreamfout=newfileoutputstream(file filename);capturebitmap.compress(bitmap.compressformat.jpeg,100,fout);sureintent.putextra("capturepath",file filename);sureintent.putextra("capturefilename",filename);sureintent.putextra("capturefileid",randomid);}catch(filenotfoundexceptione){e.printstacktrace();}}setresult(result_ok,sureintent);finish();break;default:break;}}@overridepublicbooleanonkeydown(intkeycode,keyeventevent){switch(keycode){casekeyevent.keycode_back:intentcancelintent=getintent();creatependingresult(600,cancelintent,pendingintent.flag_update_current);cancelintent.putextra("imagepath",imagepath);cancelintent.putextra("position",position);cancelintent.putextra("todowhat","cancel");setresult(result_ok,cancelintent);finish();break;default:break;}returnfalse;}@overridepublicvoidfinish(){super.finish();}@overrideprotectedvoidonactivityresult(intrequestcode,intresultcode,intentdata){if(resultcode==result_ok){switch(requestcode){case400:stringtext=null;log.v("drawrectactivity","onactivityreaultimagepath=" imagepath imagelist.get(position));if(stringutil.isnotblank(text)){log.v("drawrectactivity","onactivityreaultimagepath=" imagepath imagelist.get(position) ";text=" text);}else{}break;default:break;}}super.onactivityresult(requestcode,resultcode,data);}@overrideprotectedvoidondestroy(){super.ondestroy();}}

关于“android怎么实现手势划定区域裁剪图片”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注恰卡编程网行业资讯频道,小编每天都会为大家更新不同的知识点。

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图