//dependency for bar code scannercompile 'com.journeyapps:zxing-android-embedded:3.4.0'
package com.example.gangsofcoder.testdemo.BarCodeScanner;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.gangsofcoder.testdemo.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class BarCodeScanner extends AppCompatActivity {
private TextView tvName;
private Button btnScanCode;
private IntentIntegrator qrScannner;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanner_layout);
tvName = (TextView) findViewById(R.id.tvName);
btnScanCode = (Button) findViewById(R.id.btnScan);
//Here initialize scanning object qrScannner = new IntentIntegrator(this);
qrScannner.setOrientationLocked(false);
qrScannner.setPrompt("SCANNING...");
qrScannner.setBeepEnabled(true);
btnScanCode.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//Here intialization of
qrScannner.initiateScan();
}
});
}
//call after scanning bar code @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Toast.makeText(this, "Nothing Found", Toast.LENGTH_LONG).show();
} else {
try {
tvName.setText(result.getContents());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, result.getContents(), Toast.LENGTH_LONG).show();
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
No comments :
Post a Comment