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 :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA}, THE_PERMISSIONS_REQUEST_CAMERA);
} else {
Intent intent = new Intent(AadhaarView.this, ScannerActivity.class);
startActivityForResult(intent, THE_QR_SCAN_REQUEST);
}
Now scanner activity will scan the qr code and will return data back to Main sctivity :
public class ScannerActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView mScannerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
setupScanner();
}
and in onActivityResult of Main activity we can read result :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == THE_QR_SCAN_REQUEST) {
Toast.makeText(getApplicationContext()," result :"+data.getStringExtra("result"),Toast.LENGTH_LONG).show();
}
}
private void setupScanner(){
mScannerView = new ZXingScannerView(ScannerActivity.this);
setContentView(mScannerView);
mScannerView.setResultHandler(ScannerActivity.this);
mScannerView.startCamera();
}
@Override
public void handleResult(Result result) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result.getText().toString());
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
}
0 Comment(s)