BÀI 82 - SCAN QR VÀ BARCODE TRONG ANDROID

  • Thư viện này sẽ giúp ứng dụng đọc nội dung trên các mã QR và BarCode: tại đây

implementation 'me.dm7.barcodescanner:zxing:1.9.8'

  • Thêm thư viện này

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

android:id="@+id/realativeLayout_QRCode"

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

xmlns:app="http://schemas.android.com/apk/res-auto"

tools:context=".quantrac.ScanQRActivity">

<FrameLayout

android:id="@+id/frameLayout_qrcode_mayanh"

android:layout_alignParentTop="true"

android:layout_above="@+id/linearLayout_qrcode_button"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<me.dm7.barcodescanner.zxing.ZXingScannerView

android:id="@+id/qrcode"

app:borderColor="#FFFF"

app:borderLength="24dp"

app:borderWidth="4dp"

app:laserColor="#00ff77"

app:maskColor="@android:color/transparent"

app:roundedCorner="false"

app:squaredFinder="true"

android:layout_width="match_parent"

android:layout_height="match_parent"/>

</FrameLayout>

<LinearLayout

android:id="@+id/linearLayout_qrcode_button"

android:gravity="center"

android:background="@color/black"

android:layout_alignParentBottom="true"

android:layout_width="match_parent"

android:layout_height="wrap_content">

<ImageView

android:id="@+id/imageView_QRCode_focus"

android:layout_margin="20dp"

android:src="@drawable/icon_no_focus"

android:layout_width="40dp"

android:layout_height="40dp"

tools:ignore="ContentDescription" />

<ImageView

android:id="@+id/imageView_QRCode_flash"

android:layout_margin="20dp"

android:src="@drawable/icon_flash"

android:layout_width="40dp"

android:layout_height="40dp"

tools:ignore="ContentDescription" />

</LinearLayout>


</RelativeLayout>

  • main.java

public class ScanQRActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {


private static final int REQUEST_CAMERA = 1;


ZXingScannerView scannerView;

RelativeLayout rlQRCode;

ImageView imgFlash, imgFocus;

FrameLayout frameScanQR;


boolean flash = false;

boolean focus = false;


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_scan_qractivity);

AnhXa();


SetupScanQR();


Click_button();

}


private void Click_button() {

imgFlash.setOnClickListener(v -> {

flash = !flash;

if (flash) {

scannerView.setFlash(flash);

imgFlash.setImageResource(R.drawable.icon_on_flash);

} else {

scannerView.setFlash(flash);

imgFlash.setImageResource(R.drawable.icon_flash);

}

});


imgFocus.setOnClickListener(v -> {

focus = !focus;

if (focus){

scannerView.setAutoFocus(true);

imgFocus.setImageResource(R.drawable.icon_focus);

} else {

scannerView.setAutoFocus(false);

imgFocus.setImageResource(R.drawable.icon_no_focus);

}

});

}


private void SetupScanQR() {


scannerView = new ZXingScannerView(this);

frameScanQR.addView(scannerView);


int apiVersion = android.os.Build.VERSION.SDK_INT;

if (apiVersion >= android.os.Build.VERSION_CODES.M) {

if (!checkPermission()) {

requestPermission();

}

}

}


private boolean checkPermission() {

return (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED);

}


private void requestPermission() {

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA);

}


@SuppressLint("MissingSuperCall")

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if (requestCode == REQUEST_CAMERA) {

if (grantResults.length > 0) {

boolean cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED;

if (cameraAccepted) {

Toast.makeText(getApplicationContext(), "Permission Granted, Now you can access camera", Toast.LENGTH_LONG).show();

} else {

Toast.makeText(getApplicationContext(), "Permission Denied, You cannot access and camera", Toast.LENGTH_LONG).show();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {

showMessage((dialog, which) -> {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

requestPermissions(new String[]{Manifest.permission.CAMERA},

REQUEST_CAMERA);

}

});

}

}

}

}

}

}


private void showMessage(DialogInterface.OnClickListener okListener) {

new AlertDialog.Builder(ScanQRActivity.this)

.setMessage("You need to allow access to both the permissions")

.setPositiveButton("OK", okListener)

.setNegativeButton("Cancel", null)

.create()

.show();

}


@Override

protected void onResume() {

super.onResume();

scannerView.setResultHandler(this);

scannerView.startCamera();

}


@Override

protected void onPause() {

super.onPause();

scannerView.stopCamera();

}


@Override

public void handleResult(com.google.zxing.Result result) {

final String stringResult = result.getText();

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Scan Result");

builder.setNegativeButton("Again", (dialog, which) -> scannerView.resumeCameraPreview(ScanQRActivity.this));

builder.setPositiveButton("Go to", (dialog, which) -> {

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(stringResult));

startActivity(browserIntent);

});

builder.setMessage(stringResult);

AlertDialog alert = builder.create();

alert.show();

}


private void AnhXa() {

scannerView = findViewById(R.id.qrcode);

rlQRCode = findViewById(R.id.realativeLayout_QRCode);

imgFlash = findViewById(R.id.imageView_QRCode_flash);

imgFocus = findViewById(R.id.imageView_QRCode_focus);

frameScanQR = findViewById(R.id.frameLayout_qrcode_mayanh);

}


@Override

public void onBackPressed() {

super.onBackPressed();

}

}