Zbar Android Costum Camera Preview Stretched -
i customize camera view of zbar api in zbarscanneractivity class. code added.
setcontentview(r.layout.scanner); mautofocushandler = new handler(); // create , configure imagescanner; setupscanner(); // create relativelayout container hold surfaceview, // , set content of our activity. mpreview = new camerapreview(this, this, autofocuscb); linearlayout zbarlayout = (linearlayout) findviewbyid(r.id.zbar_layout_area); mpreview.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); zbarlayout.addview(mpreview); then when run app camera preview wrong rotate using setdisplayorientation 90. rotate camera view stretched. camerapreview code.
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); 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); } } } log.d("edgar", optimalsize.width + " " + optimalsize.height); 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.setdisplayorientation(90); mcamera.setparameters(parameters); mcamera.setpreviewcallback(mpreviewcallback); mcamera.startpreview(); mcamera.autofocus(mautofocuscallback); } } please me fix camera view. thank helping.
if trying view in portrait view, show landscape view portal not 'squished' suggest doing layout preview behind other layouts.
<relativelayout android:layout_height="fill_parent" android:layout_width="fill_parent" > <framelayout android:id="@+id/camera_preview" android:layout_width="fill_parent" android:layout_height="fill_parent"> <linearlayout android:id="@+id/ticket_overlay" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <... stuff here ... w/ background> <... spacer here ... w/ transparent show view portal> <... stuff here ... w/ background> </linearlayout> </framelayout> </relativelayout>
Comments
Post a Comment