Monday, 27 August 2018

How to Create BarCode and QRCode Scanner inside Fragment

Need file :

1.BarcodeActivity.java
2.compile 'me.dm7.barcodescanner:zxing:1.9'
3.BarcodeFragment.java
4.res/layout/fragment_barcode.xml

Step 1:

compile 'me.dm7.barcodescanner:zxing:1.9'


Step 2:

res/layoput/fragment_barcode.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:app="http://schemas.android.com/apk/res-auto"    
android:id="@+id/activity_main"    
android:layout_width="match_parent"    
android:layout_height="match_parent"    
android:orientation="vertical">
    <LinearLayout   
     android:layout_width="match_parent"    
      android:layout_height="match_parent"  
      android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">

        <ImageView
            android:layout_width="200dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:src="@drawable/logo" />

        <View
            android:layout_width="match_parent"
            android:layout_height="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="BarCode and QRCode Scaner"
            android:textColor="@color/colorDarkGreen"
            android:textSize="20dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical">
        <TextView
            android:id="@+id/text_scan" 
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="50dp"
            android:textSize="15dp"
            android:textColor="@android:color/black" 
           android:text="Scan" />


        <Button    
        android:id="@+id/buttonScan"
            android:layout_width="wrap_content"
            android:layout_height="40dp"  
          android:ems="15"       
     android:layout_gravity="center|bottom"  
          android:background="@drawable/button_selector"
            android:text="Scan QR Code"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:textColor="@android:color/white"
            android:textSize="13dp" />

    </LinearLayout>


</LinearLayout>



Step 3:

BarcodeActivity.java

public class BarcodeActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{

    private ZXingScannerView mScannerView;

    @Override    public void onCreate(Bundle state) {
        super.onCreate(state);
        mScannerView = new ZXingScannerView(this);   // Programmatically initialize the scanner view       
        setContentView(mScannerView);                // Set the scanner view as the content view    }

    @Override    public void onResume() {
        super.onResume();
        mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.  
        mScannerView.startCamera();          // Start camera on resume    }

    @Override    public void onPause() {
        super.onPause();
        mScannerView.stopCamera();           // Stop camera on pause    }

    @Override    public void handleResult(Result rawResult) {

        BarcodeQrcodeFragment.tx_scan.setText("Scan Result : "+rawResult.getText());
        onBackPressed();

    }
}


Step 4:

BarcodeQrcodeFragment .java

public class BarcodeQrcodeFragment extends Fragment{
    public static TextView tx_scan;
    Button bt_scan;
    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_bar_qr_code, container, false);

        bt_scan=(Button)view.findViewById(R.id.buttonScan);
        tx_scan=(TextView)view.findViewById(R.id.text_scan);

        bt_scan.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),BarcodeActivity.class);
                startActivity(intent);
            }
        });
        return view;
    }
    }

Step 5 :

Manifests

<uses-permission android:name="android.permission.CAMERA"/>




No comments:

Post a Comment

HOW TO CREATE CIRCLE IMAGE VIEW

Step 1: implementation 'de.hdodenhof:circleimageview:3.0.0' Step 2: <de.hdodenhof.circleimageview.CircleImageView xmlns:ap...