android - Implement zbar scanner in already existing layout -
im trting place camera preview of zbar scanner existing layout, place im trying put stays black , dosn't start camera read qrs. if don't use custom layout 1 thats in example(it starts scanner in new activity , takes screen size) fine. means have no problems permissions or this. codes i'm using now:
import net.sourceforge.zbar.config; import net.sourceforge.zbar.image; import net.sourceforge.zbar.imagescanner; import net.sourceforge.zbar.symbol; import net.sourceforge.zbar.symbolset; import com.dm.zbar.android.scanner.camerapreview; import com.dm.zbar.android.scanner.zbarconstants; import com.dm.zbar.android.scanner.zbarscanneractivity; import android.os.bundle; import android.os.handler; import android.app.activity; import android.content.intent; import android.content.pm.packagemanager; import android.hardware.camera; import android.text.textutils; import android.view.menu; import android.view.view; import android.view.window; import android.view.windowmanager; import android.widget.linearlayout; import android.widget.linearlayout.layoutparams; import android.widget.textview; import android.widget.toast; public class pointgivingactivity extends activity implements camera.previewcallback, zbarconstants{ private static final int zbar_scanner_request = 0; private static final int zbar_qr_scanner_request = 1; private static final string tag = "zbarscanneractivity"; private camerapreview mpreview; private camera mcamera; private imagescanner mscanner; private handler mautofocushandler; private boolean mpreviewing = true; static { system.loadlibrary("iconv"); } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (!iscameraavailable()) { cancelrequest(); return; } requestwindowfeature(window.feature_no_title); getwindow().addflags(windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.activity_point_giving2); mautofocushandler = new handler(); setupscanner(); linearlayout zbarlayout = (linearlayout) findviewbyid(r.id.zbar_layout_area); mpreview = new camerapreview(zbarlayout.getcontext(), this, autofocuscb); mpreview.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); zbarlayout.addview(mpreview); } public void launchscanner(view v) { if (iscameraavailable()) { intent intent = new intent(this, zbarscanneractivity.class); startactivityforresult(intent, zbar_scanner_request); } else { toast.maketext(this, "rear facing camera unavailable", toast.length_short).show(); } } public void launchscanner() { if (iscameraavailable()) { intent intent = new intent(this, zbarscanneractivity.class); startactivityforresult(intent, zbar_scanner_request); } else { toast.maketext(this, "rear facing camera unavailable", toast.length_short).show(); } } public void launchqrscanner(view v) { if (iscameraavailable()) { intent intent = new intent(this, zbarscanneractivity.class); intent.putextra(zbarconstants.scan_modes, new int[] { symbol.qrcode }); startactivityforresult(intent, zbar_scanner_request); } else { toast.maketext(this, "rear facing camera unavailable", toast.length_short).show(); } } public void mymethod(view v) { intent intent = new intent(this, zbarscanneractivity.class); startactivityforresult(intent, zbar_scanner_request); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (resultcode == result_ok) { textview sometext = (textview) findviewbyid(r.id.givepointtv1); sometext.settext(data.getstringextra(zbarconstants.scan_result)); // toast.maketext(this, "scan result = " + // data.getstringextra(zbarconstants.scan_result), // toast.length_short).show(); // toast.maketext(this, "scan result type = " + // data.getstringextra(zbarconstants.scan_result_type), // toast.length_short).show(); } else if (resultcode == result_canceled) { // toast.maketext(this, "camera unavailable", // toast.length_short).show(); } } public boolean iscameraavailable() { packagemanager pm = getpackagemanager(); return pm.hassystemfeature(packagemanager.feature_camera); } public void cancelrequest() { intent dataintent = new intent(); dataintent.putextra(error_info, "camera unavailable"); setresult(activity.result_canceled, dataintent); finish(); } public void onpreviewframe(byte[] data, camera camera) { camera.parameters parameters = camera.getparameters(); camera.size size = parameters.getpreviewsize(); image barcode = new image(size.width, size.height, "y800"); barcode.setdata(data); int result = mscanner.scanimage(barcode); if (result != 0) { mcamera.cancelautofocus(); mcamera.setpreviewcallback(null); mcamera.stoppreview(); mpreviewing = false; symbolset syms = mscanner.getresults(); (symbol sym : syms) { string symdata = sym.getdata(); if (!textutils.isempty(symdata)) { intent dataintent = new intent(); dataintent.putextra(scan_result, symdata); dataintent.putextra(scan_result_type, sym.gettype()); setresult(activity.result_ok, dataintent); finish(); break; } } } } private runnable doautofocus = new runnable() { public void run() { if(mcamera != null && mpreviewing) { mcamera.autofocus(autofocuscb); } } }; // mimic continuous auto-focusing camera.autofocuscallback autofocuscb = new camera.autofocuscallback() { public void onautofocus(boolean success, camera camera) { mautofocushandler.postdelayed(doautofocus, 1000); } }; public void setupscanner() { mscanner = new imagescanner(); mscanner.setconfig(0, config.x_density, 3); mscanner.setconfig(0, config.y_density, 3); int[] symbols = getintent().getintarrayextra(scan_modes); if (symbols != null) { mscanner.setconfig(symbol.none, config.enable, 0); (int symbol : symbols) { mscanner.setconfig(symbol, config.enable, 1); } } } }
im pretty sure problem here somewhere:
linearlayout zbarlayout = (linearlayout) findviewbyid(r.id.zbar_layout_area); mpreview = new camerapreview(zbarlayout.getcontext(), this, autofocuscb); mpreview.setlayoutparams(new linearlayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent)); zbarlayout.addview(mpreview);
but cant figure out how make work way want.
here , xml file if in need
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pointgivingll1" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f1f1" android:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:weightsum="10" > <linearlayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightsum="8" > <button android:id="@+id/givepointmenubut" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="invisible" /> <textview android:id="@+id/givepointtv1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="6" android:gravity="center" android:text="@string/pointgiving" android:textcolor="@android:color/black" android:textsize="22sp" /> <button android:id="@+id/givepointmenubut" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginbottom="10dp" android:layout_margintop="10dp" android:layout_weight="1" android:gravity="center" android:text="@string/ruchno" android:textsize="10sp" /> </linearlayout> <linearlayout android:gravity="center" android:layout_weight="9" android:id="@+id/zbar_layout_area" android:layout_width="match_parent" android:layout_height="0dp" android:orientation="vertical" > </linearlayout> </linearlayout>
i faced same issue , did trick adding/removing views framelayout programmatically. found solution in here:
customize camera view android using zbar
here activity_qr_code_reader.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/black" > <framelayout android:id="@+id/flytcamera" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" > <imageview android:id="@+id/ivscanner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/im_qr_scan"/> </framelayout> <textview android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:padding="10dp" android:textcolor="@android:color/white" android:textsize="16sp" android:background="@color/transparent_black" android:text="@string/qr_code_reading"/> </linearlayout>
so far have info text view @ bottom , qr code reading frame image (only 4 white corners). wanted put camera preview inside whole framelayout, ended placing @ center. here code. not perfect, @ least more user-friendly.
public class qrcodereaderactivity extends zbarscanneractivity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.layout_qr_code_reader); framelayout flytcamera = (framelayout) findviewbyid(r.id.flytcamera); imageview ivscanner = (imageview) findviewbyid(r.id.ivscanner); flytcamera.addview(mpreview); flytcamera.removeview(ivscanner); flytcamera.addview(ivscanner); } }
btw, wanted zbarscanner portrait, forced camera rotate 90 degrees. http://developer.android.com/reference/android/hardware/camera.html#setdisplayorientation(int)
@override protected void onresume() { super.onresume(); setcameradisplayorientation(this, camera.camerainfo.camera_facing_back, mcamera); } public static void setcameradisplayorientation(activity activity, int cameraid, android.hardware.camera camera) { android.hardware.camera.camerainfo info = new android.hardware.camera.camerainfo(); android.hardware.camera.getcamerainfo(cameraid, info); int rotation = activity.getwindowmanager().getdefaultdisplay() .getrotation(); int degrees = 0; switch (rotation) { case surface.rotation_0: degrees = 0; break; case surface.rotation_90: degrees = 90; break; case surface.rotation_180: degrees = 180; break; case surface.rotation_270: degrees = 270; break; } int result; if (info.facing == camera.camerainfo.camera_facing_front) { result = (info.orientation + degrees) % 360; result = (360 - result) % 360; // compensate mirror } else { // back-facing result = (info.orientation - degrees + 360) % 360; } camera.setdisplayorientation(result); }
Comments
Post a Comment