import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.Button;
import
android.widget.TextView;
import
android.widget.Toast;
import
androidx.annotation.Nullable;
import
androidx.appcompat.app.AppCompatActivity;
import
com.google.zxing.integration.android.IntentIntegrator;
import
com.google.zxing.integration.android.IntentResult;
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener {
Button scanBtn;
TextView messageText, messageFormat;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scanBtn = findViewById(R.id.scanBtn);
messageText = findViewById(R.id.textContent);
messageFormat = findViewById(R.id.textFormat);
scanBtn.setOnClickListener(
this
);
}
@Override
public
void
onClick(View v) {
IntentIntegrator intentIntegrator =
new
IntentIntegrator(
this
);
intentIntegrator.setPrompt(
"Scan a barcode or QR Code"
);
intentIntegrator.setOrientationLocked(
true
);
intentIntegrator.initiateScan();
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
@Nullable
Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if
(intentResult !=
null
) {
if
(intentResult.getContents() ==
null
) {
Toast.makeText(getBaseContext(),
"Cancelled"
, Toast.LENGTH_SHORT).show();
}
else
{
messageText.setText(intentResult.getContents());
messageFormat.setText(intentResult.getFormatName());
}
}
else
{
super
.onActivityResult(requestCode, resultCode, data);
}
}
}