Black Screenshot android OpenCv -
currently using opencv want happen when user presses screen of course screen/object displayed camera highlight necessary/similar colors see:color-blob-detector. video without screenshot.
below there small camera icon triggering screenshot (imagebutton), when screenshots outcome of picture black. wanted when necessary colors being highlighted , press screenshot progmatically (which imagebutton) take picture of screen (including highlighted objects) , save image in file.
i have tried using camera control opencv taking picture, hard me merge these two.
public class camera extends activity implements ontouchlistener, cvcameraviewlistener2 { private static final string tag = "ocvsample::activity"; private boolean miscolorselected = false; private mat mrgba; private scalar mblobcolorrgba; private scalar mblobcolorhsv; private colorblobdetector mdetector; private mat mspectrum; private size spectrum_size; private scalar contour_color; private camerabridgeviewbase mopencvcameraview; private baseloadercallback mloadercallback = new baseloadercallback(this) { @override public void onmanagerconnected(int status) { switch (status) { case loadercallbackinterface.success: { log.i(tag, "opencv loaded successfully"); mopencvcameraview.enableview(); mopencvcameraview.setontouchlistener(camera.this); } break; default: { super.onmanagerconnected(status); } break; } } }; public camera() { log.i(tag, "instantiated new " + this.getclass()); } /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { log.i(tag, "called oncreate"); super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.colorblobdetector); mopencvcameraview = (camerabridgeviewbase) findviewbyid(r.id.activity_java_surface_view); mopencvcameraview.setvisibility(surfaceview.visible); mopencvcameraview.setcvcameraviewlistener(this); if (!opencvloader.initdebug()) { log.e(this.getclass().getsimplename(), " opencvloader.initdebug(), not working."); } else { log.d(this.getclass().getsimplename(), " opencvloader.initdebug(), working."); } } public void camera_b(view v) { string path = environment.getexternalstoragedirectory().tostring() + "/" + "hellp.jpg"; v = getwindow().getdecorview().getrootview(); v.setdrawingcacheenabled(true); bitmap bitmap = bitmap.createbitmap(v.getdrawingcache()); v.setdrawingcacheenabled(false); outputstream out = null; file imagefile = new file(path); try { out = new fileoutputstream(imagefile); // choose jpeg format bitmap.compress(bitmap.compressformat.jpeg, 90, out); out.flush(); } catch (filenotfoundexception e) { // manage exception } catch (ioexception e) { // manage exception } { try { if (out != null) { out.close(); } } catch (exception exc) { } } } @override public void onpause() { super.onpause(); if (mopencvcameraview != null) mopencvcameraview.disableview(); } @override public void onresume() { super.onresume(); if (!opencvloader.initdebug()) { log.d(tag, "internal opencv library not found. using opencv manager initialization"); opencvloader.initasync(opencvloader.opencv_version_3_0_0, this, mloadercallback); } else { log.d(tag, "opencv library found inside package. using it!"); mloadercallback.onmanagerconnected(loadercallbackinterface.success); } } public void ondestroy() { super.ondestroy(); if (mopencvcameraview != null) mopencvcameraview.disableview(); } public void oncameraviewstarted(int width, int height) { mrgba = new mat(height, width, cvtype.cv_8uc4); mdetector = new colorblobdetector(); mspectrum = new mat(); mblobcolorrgba = new scalar(255); mblobcolorhsv = new scalar(255); spectrum_size = new size(200, 64); contour_color = new scalar(255,0,0,255); } public void oncameraviewstopped() { mrgba.release(); } public boolean ontouch(view v, motionevent event) { int cols = mrgba.cols(); int rows = mrgba.rows(); int xoffset = (mopencvcameraview.getwidth() - cols) / 2; int yoffset = (mopencvcameraview.getheight() - rows) / 2; int x = (int)event.getx() - xoffset; int y = (int)event.gety() - yoffset; log.i(tag, "touch image coordinates: (" + x + ", " + y + ")"); if ((x < 0) || (y < 0) || (x > cols) || (y > rows)) return false; rect touchedrect = new rect(); touchedrect.x = (x>4) ? x-4 : 0; touchedrect.y = (y>4) ? y-4 : 0; touchedrect.width = (x+4 < cols) ? x + 4 - touchedrect.x : cols - touchedrect.x; touchedrect.height = (y+4 < rows) ? y + 4 - touchedrect.y : rows - touchedrect.y; mat touchedregionrgba = mrgba.submat(touchedrect); mat touchedregionhsv = new mat(); imgproc.cvtcolor(touchedregionrgba, touchedregionhsv, imgproc.color_rgb2hsv_full); // calculate average color of touched region mblobcolorhsv = core.sumelems(touchedregionhsv); int pointcount = touchedrect.width*touchedrect.height; (int = 0; < mblobcolorhsv.val.length; i++) mblobcolorhsv.val[i] /= pointcount; mblobcolorrgba = converscalarhsv2rgba(mblobcolorhsv); log.i(tag, "touched rgba color: (" + mblobcolorrgba.val[0] + ", " + mblobcolorrgba.val[1] + ", " + mblobcolorrgba.val[2] + ", " + mblobcolorrgba.val[3] + ")"); mdetector.sethsvcolor(mblobcolorhsv); imgproc.resize(mdetector.getspectrum(), mspectrum, spectrum_size); miscolorselected = true; touchedregionrgba.release(); touchedregionhsv.release(); return false; // don't need subsequent touch events } public mat oncameraframe(cvcameraviewframe inputframe) { mrgba = inputframe.rgba(); if (miscolorselected) { mdetector.process(mrgba); list<matofpoint> contours = mdetector.getcontours(); log.e(tag, "contours count: " + contours.size()); imgproc.drawcontours(mrgba, contours, -1, contour_color); mat colorlabel = mrgba.submat(4, 68, 4, 68); colorlabel.setto(mblobcolorrgba); mat spectrumlabel = mrgba.submat(4, 4 + mspectrum.rows(), 70, 70 + mspectrum.cols()); mspectrum.copyto(spectrumlabel); } return mrgba; } private scalar converscalarhsv2rgba(scalar hsvcolor) { mat pointmatrgba = new mat(); mat pointmathsv = new mat(1, 1, cvtype.cv_8uc3, hsvcolor); imgproc.cvtcolor(pointmathsv, pointmatrgba, imgproc.color_hsv2rgb_full, 4); return new scalar(pointmatrgba.get(0, 0)); } }
as can see camera_b screenshot happens.the xml color blob detector: colorblobxml (i cannot paste xml code here)
image of output. can see, can take screenshot of button, not expected image. (this image made using power +vol down)
put in androidmanifest.xml
<uses-permission android:name="android.permission.camera"/>
and in this
@override public void oncreate(bundle savedinstancestate) { log.i(tag, "called oncreate"); super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.colorblobdetector); mopencvcameraview = (camerabridgeviewbase) findviewbyid(r.id.activity_java_surface_view); mopencvcameraview.setvisibility(surfaceview.visible); mopencvcameraview.setcvcameraviewlistener(this); mopencvcameraview.setcameraindex(0); //0 = , 1 = frontal mopencvcameraview.enableview(); if (!opencvloader.initdebug()) { log.e(this.getclass().getsimplename(), " opencvloader.initdebug(), not working."); } else { log.d(this.getclass().getsimplename(), " opencvloader.initdebug(), working."); }
Comments
Post a Comment