Android SDK - camera2 - Draw rectangle over TextureView -


im new android development, , i'm finding hard find examples on camera2 api.

im working way through issues, on 1 stuck. in default camera, when touch screen focus, shows rectangle of focus area moment. want similar (or in case, exact same thing start off can figure out).

i read somewhere (i think textureview page in sdk docs) cant draw on textureview while being used camera preview - , calling lock method return null rather canvas.

i found online: https://github.com/commonsguy/vidtry/ cant work. either errors saying my main view cant cast drawable view, or vice versa - or drawable view on top , makes screen black - or on bottom , wont respond touch events (and trying force performclick view above casues crashes.)

im stuck! can give me explanation or example of how can draw rectangle on event position few sconds?

thanks!

  1. first example of camera2 api android there open source google sample code. https://github.com/googlesamples/android-camera2basic
  2. second part want draw rectangle(touch focus) follow these steps -
    • create custom class extending surfaceview.
    • call ontouchlistener method in custom class detect finger coordinates , draw rectangle using paint class in android. -add custom class above textureview displaying preview camera2.
    • turn custom view transparent.
    • finally when touch @ point on device screen ontouchlistener called draw on canvas of custom view added above camera preview.
    • finally clear canvas dont keep adding rectangles customview since need 1 rect @ 1 point of time.
    • also if not touched time rectangle should disappear. using handler custom view.

i giving here tested code this. hope helps you. best of luck.

 private class customview extends surfaceview {      private final paint paint;     private final surfaceholder mholder;     private final context context;      public customview(camera2basicfragment context) {         super(context.getactivity().getbasecontext());         mholder = getholder();         mholder.setformat(pixelformat.transparent);         this.context = context.getactivity().getbasecontext();         paint = new paint(paint.anti_alias_flag);         paint.setcolor(color.white);         paint.setstyle(paint.style.stroke);     }      @override     protected void ondraw(canvas canvas) {         super.ondraw(canvas);     }      @override     public boolean ontouchevent(motionevent event) {         if (event.getaction() == motionevent.action_down) {             invalidate();             if (mholder.getsurface().isvalid()) {                 final canvas canvas = mholder.lockcanvas();                 log.d("touch", "touchrecieved camera");                 if (canvas != null) {                     log.d("touch", "touchrecieved canvas still not null");                     canvas.drawcolor(color.transparent, porterduff.mode.clear);                     canvas.drawcolor(color.transparent);                     canvas.drawcircle(event.getx(), event.gety(), 100, paint);                     mholder.unlockcanvasandpost(canvas);                     new handler().postdelayed(new runnable() {                         @override                         public void run() {                             canvas canvas1 = mholder.lockcanvas();                             if(canvas1 !=null){                                 canvas1.drawcolor(0, porterduff.mode.clear);                                 mholder.unlockcanvasandpost(canvas1);                             }                          }                     }, 1000);                  }                 mholder.unlockcanvasandpost(canvas);               }         }           return false;     } } 

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -