opencv - Select Image area on Image view in Android -
i want select rect area of image i.e. of bitmap in image view . rect area defined user touch. not crop. user can select rect area , sub-image created. giving code below nut not independent of devices , have incorporated opencv camculating region of interest , please me
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final dragrectview view = (dragrectview) findviewbyid(r.id.dragrect); imview1 = (imageview) findviewbyid(r.id.imageview1); bmp = bitmapfactory.decoderesource(getresources(), r.drawable.poll); imview1.setimagebitmap(bmp); final mat mat1=new mat(bmp.getheight(),bmp.getwidth(),cvtype.cv_8uc4); utils.bitmaptomat(bmp, mat1); if (null != view) { view.setonupcallback(new dragrectview.onupcallback() { @override public void onrectfinished(final rect rect) { org.opencv.core.rect rectmat = new org.opencv.core.rect(10,20,240,360); mat matroi = new mat(mat1,rectmat); bitmap bm = bitmap.createbitmap(matroi.rows(),matroi.cols(),config.argb_8888); utils.mattobitmap(matroi, bm); //mat1.copyto(rectmat); toast.maketext(getapplicationcontext(), "rect (" + rect.left + ", " + rect.top + ", " + rect.right + ", " + rect.bottom + ")", toast.length_long).show(); imview1.setimagebitmap(bm); } }); }
}
this code draw rectangle using android.graphics.rect.
public int imgyoffset; public int imgxoffset; private rectangle_box = null; final atomicreference<point> rectbox_1stcorner = new atomicreference<point>(); final paint rectpaint = new paint(); rectpaint.setcolor(color.rgb(0, 255, 0)); rectpaint.setstrokewidth(5); rectpaint.setstyle(style.stroke); setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { final point corner = new point(event.getx() - imgxoffset, event.gety() - imgyoffset); switch(event.getaction()){ case motionevent.action_down: rectbox_1stcorner.set(corner); break; case motionevent.action_up: rectangle_box= new rect(rectbox_1stcorner.get(), corner); break; case motionevent.action_move: final android.graphics.rect rect = new android.graphics.rect( (int)rectbox_1stcorner.get().x + imgxoffset, (int)rectbox_1stcorner.get().y + imgyoffset, (int)corner.x + imgxoffset, (int)corner.y + imgyoffset); final canvas canvas =_holder.lockcanvas(rect); canvas.drawcolor(color.transparent, porterduff.mode.clear); // remove old rectangle canvas.drawrect(rect, rectpaint); _holder.unlockcanvasandpost(canvas); break; } return true; } }); } // if rectangle box not null assign rectangle_box new mat or bitmap object create subimage want work with.
Comments
Post a Comment