Search code examples
javaandroidsurfaceviewzxingbarcode-scanner

using zxing barcode reader through surfaceview


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:

enter image description here


Solution

  • Customize your zxing layout with below codes.

    I took here a RelativeLayout for zxing Scanner.

    Or, Go with this references :

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

    enter image description here

    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);
    
        }