using zxing barcode reader through surfaceview

这一生的挚爱 提交于 2019-12-06 07:41:48

问题


I am creating a Barcode Scanner app and I want to use Zxing to read barcodes, my app has a surfaceview and showing camera into that, but now I want to scan barcode from my SurfaceView camera , the reason that I use it, is that I have two Edittexts under the surface view in my layout to show content of barcode.

How should I create an app like, the main problem is that how can I set zxing into surface.
If there's any way I would appreciate.

Layout screenshot:


回答1:


Customize your zxing layout with below codes.

I took here a RelativeLayout for zxing Scanner.

Or, Go with this references :

  • https://github.com/dm77/barcodescanner/issues/67

My design looks like below image, if need you can take FrameLayout.

Xml Code :

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">




<android.support.v7.widget.Toolbar
    android:id="@+id/toolbarAdjustScan"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    android:elevation="6dp"
    android:minHeight="56dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/titleToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:layout_weight="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textSize="18dp" />

         <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dp"
            android:id="@+id/searchAdjustBtn"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="SEARCH   "
            android:textColor="@color/white"
            android:textSize="13dp" />
    </LinearLayout>

</android.support.v7.widget.Toolbar>


 <TextView
    android:id="@+id/textv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:gravity="center_horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    android:text="Point at any barcode to scan"
    android:textColor="@color/white"
    android:textSize="14sp"
    android:textStyle="normal" />

<RelativeLayout
    android:layout_marginBottom="120dp"
    android:layout_below="@+id/textv"
    android:id="@+id/relative_scan_take_single"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</RelativeLayout>
</LinearLayout>

<LinearLayout
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:background="@color/colorPrimaryDark"
    android:layout_height="120dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/doneBtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="bottom"
        android:text="Done"
        android:textAllCaps="true"
        android:textColor="@color/white"
        android:textSize="22dp" />


</LinearLayout>

Activity code :(Java)

   public class TakeSingleScanActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
    private ZXingScannerView mScannerView;



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_single_scan);

 //Scanner
        mScannerView = new ZXingScannerView(this);
        RelativeLayout rl = (RelativeLayout) findViewById(R.id.relative_scan_take_single);
        rl.addView(mScannerView);
        mScannerView.setResultHandler(this);
        mScannerView.startCamera();
        mScannerView.setSoundEffectsEnabled(true);
        mScannerView.setAutoFocus(true);

    }


来源:https://stackoverflow.com/questions/49104965/using-zxing-barcode-reader-through-surfaceview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!