android - How to add textVIew to camera Previw -


im developing application using zbar qr code scanner. everthing works fine, want add text camera preview , im unable it. when add text camera preview disappear. there not enought information , id appriciate if me. !

this code of camerapreview.java looks make magic

package com.dm.zbar.android.scanner;  import android.content.context; import android.hardware.camera; import android.hardware.camera.autofocuscallback; import android.hardware.camera.previewcallback; import android.hardware.camera.size; import android.util.log; import android.view.surfaceholder; import android.view.surfaceview; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.textview;  import java.io.ioexception; import java.util.list;  class camerapreview extends viewgroup implements surfaceholder.callback {     private final string tag = "camerapreview";      surfaceview msurfaceview;     surfaceholder mholder;     size mpreviewsize;     list<size> msupportedpreviewsizes;     camera mcamera;     previewcallback mpreviewcallback;     autofocuscallback mautofocuscallback;      camerapreview(context context, previewcallback previewcallback, autofocuscallback autofocuscb) {         super(context);          mpreviewcallback = previewcallback;         mautofocuscallback = autofocuscb;         msurfaceview = new surfaceview(context);           textview tvtest = new textview(context);         linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(0,0);         tvtest.setlayoutparams(layoutparams);         tvtest.settext("Сканирайте qr !");         addview(tvtest);             addview(msurfaceview);          // install surfaceholder.callback notified when         // underlying surface created , destroyed.         mholder = msurfaceview.getholder();         mholder.addcallback(this);         mholder.settype(surfaceholder.surface_type_push_buffers);     }      public void setcamera(camera camera) {         mcamera = camera;         if (mcamera != null) {             msupportedpreviewsizes = mcamera.getparameters().getsupportedpreviewsizes();             requestlayout();         }     }      @override     protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {         // purposely disregard child measurements because act         // wrapper surfaceview centers camera preview instead         // of stretching it.         final int width = resolvesize(getsuggestedminimumwidth(), widthmeasurespec);         final int height = resolvesize(getsuggestedminimumheight(), heightmeasurespec);         setmeasureddimension(width, height);          if (msupportedpreviewsizes != null) {             mpreviewsize = getoptimalpreviewsize(msupportedpreviewsizes, width, height);         }     }      @override     protected void onlayout(boolean changed, int l, int t, int r, int b) {          if (changed && getchildcount() > 0) {             final view child = getchildat(0);              final int width = r - l;             final int height = b - t;              int previewwidth = width;             int previewheight = height;             if (mpreviewsize != null) {                 previewwidth = mpreviewsize.width;                 previewheight = mpreviewsize.height;             }              // center child surfaceview within parent.             if (width * previewheight > height * previewwidth) {                 final int scaledchildwidth = previewwidth * height / previewheight;                 child.layout((width - scaledchildwidth) / 2, 0,                         (width + scaledchildwidth) / 2, height);             } else {                 final int scaledchildheight = previewheight * width / previewwidth;                 child.layout(0, (height - scaledchildheight) / 2,                         width, (height + scaledchildheight) / 2);             }         }     }      public void hidesurfaceview() {         msurfaceview.setvisibility(view.invisible);     }      public void showsurfaceview() {         msurfaceview.setvisibility(view.visible);     }      public void surfacecreated(surfaceholder holder) {         // surface has been created, acquire camera , tell         // draw.         try {             if (mcamera != null) {                 mcamera.setpreviewdisplay(holder);             }         } catch (ioexception exception) {             log.e(tag, "ioexception caused setpreviewdisplay()", exception);         }     }      public void surfacedestroyed(surfaceholder holder) {         // surface destroyed when return, stop preview.         if (mcamera != null) {             mcamera.cancelautofocus();             mcamera.stoppreview();         }     }       private size getoptimalpreviewsize(list<size> sizes, int w, int h) {         final double aspect_tolerance = 0.1;         double targetratio = (double) w / h;         if (sizes == null) return null;          size optimalsize = null;         double mindiff = double.max_value;          int targetheight = h;          // try find size match aspect ratio , size         (size size : sizes) {             double ratio = (double) size.width / size.height;             if (math.abs(ratio - targetratio) > aspect_tolerance) continue;             if (math.abs(size.height - targetheight) < mindiff) {                 optimalsize = size;                 mindiff = math.abs(size.height - targetheight);             }         }          // cannot find 1 match aspect ratio, ignore requirement         if (optimalsize == null) {             mindiff = double.max_value;             (size size : sizes) {                 if (math.abs(size.height - targetheight) < mindiff) {                     optimalsize = size;                     mindiff = math.abs(size.height - targetheight);                 }             }         }         return optimalsize;     }      public void surfacechanged(surfaceholder holder, int format, int w, int h) {         if (holder.getsurface() == null){           // preview surface not exist           return;         }          if (mcamera != null) {             // size known, set camera parameters , begin             // preview.             camera.parameters parameters = mcamera.getparameters();             parameters.setpreviewsize(mpreviewsize.width, mpreviewsize.height);             requestlayout();              mcamera.setparameters(parameters);             mcamera.setpreviewcallback(mpreviewcallback);             mcamera.startpreview();             mcamera.autofocus(mautofocuscallback);         }     }  } 

this part have additionly added code camerapreviwe disappear long add it.

        textview tvtest = new textview(context);         linearlayout.layoutparams layoutparams = new linearlayout.layoutparams(0,0);         tvtest.setlayoutparams(layoutparams);         tvtest.settext("Сканирайте qr !");         addview(tvtest); 

why not add textview in main.xml file. when programmatically add surfaceview think overlap layout of .xml file that's why camerapreview disappear.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -