Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Bar code scanner using Zxing library

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 908
    Comment on it

    There are too many libraries available to read qr code like Zbar and zxing library. Here we are showing how to use Zxing lib to scan qr code :

    First of all add dependency in app gradle file :

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

    Then on click of button start an activity to read qr code :

    1. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
    2. requestPermissions(new String[]{Manifest.permission.CAMERA}, THE_PERMISSIONS_REQUEST_CAMERA);
    3. } else {
    4. Intent intent = new Intent(AadhaarView.this, ScannerActivity.class);
    5. startActivityForResult(intent, THE_QR_SCAN_REQUEST);
    6. }

    Now scanner activity will scan the qr code and will return data back to Main sctivity :

    1. public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
    2.  
    3. private ZXingScannerView mScannerView;
    4.  
    5. @Override
    6. protected void onCreate(Bundle savedInstanceState) {
    7. super.onCreate(savedInstanceState);
    8. setContentView(R.layout.activity_scanner);
    9. setupScanner();
    10.  
    11. }
    12.  
    13. and in onActivityResult of Main activity we can read result :
    14.  
    15. @Override
    16. public void onActivityResult(int requestCode, int resultCode, Intent data) {
    17. super.onActivityResult(requestCode, resultCode, data);
    18. if(requestCode == THE_QR_SCAN_REQUEST) {
    19.  
    20. Toast.makeText(getApplicationContext()," result :"+data.getStringExtra("result"),Toast.LENGTH_LONG).show();
    21. }
    22. }
    23. private void setupScanner(){
    24. mScannerView = new ZXingScannerView(ScannerActivity.this);
    25. setContentView(mScannerView);
    26. mScannerView.setResultHandler(ScannerActivity.this);
    27. mScannerView.startCamera();
    28. }
    29.  
    30.  
    31. @Override
    32. public void handleResult(Result result) {
    33. Intent returnIntent = new Intent();
    34. returnIntent.putExtra("result",result.getText().toString());
    35. setResult(Activity.RESULT_OK,returnIntent);
    36. finish();
    37. }
    38.  
    39. }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: