QR code for Android development, android
What I have been doing before is. NET Development uses the C # language. java was used only when an APP was created recently. The QR code scanning and integration into the APP did not return a value after scanning the QR code, after repeated attempts, I finally got the returned value. Then I thought it was necessary to record it and share it here.
Download the Zxing open-source package online and import it to AndroidStudio.
In Mainfest, add the camera call permission
<! -- Vibration -->
<Uses-permission android: name = "android. permission. VIBRATE"/>
<! -- Auto-focus -->
<Uses-feature android: name = "android. hardware. camera. autofocus"/>
<! -- Camera --->
<Uses-permission android: name = "android. permission. CAMERA">
Next, add the dependency in gradle.
implementation 'com.google.zxing:core:3.3.0'
The Button click event can be found on any page, but the scan result is returned.
OnActivityResult: This method is used to receive the returned value. This method can only be rewritten in the MainActivity class.
The returned value can be obtained. If it is rewritten in another class, nothing will be returned.
private void checkButtonOnclick() {
btn_QrCode.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
startQrCode();
}
});
}
// Start scanning
Public void startQrCode (){
If (ContextCompat. checkSelfPermission (context, Manifest. permission. CAMERA )! = PackageManager. PERMISSION_GRANTED ){
// Apply for Permissions
ActivityCompat. requestPermissions (context, new String [] {Manifest. permission. CAMERA}, Constant. REQ_PERM_CAMERA );
Return;
}
// QR code scanning
Try
{
Intent intent = new Intent (context, CaptureActivity. class );
Context. startActivityForResult (intent, Constant. REQ_QR_CODE );
} Catch (Exception e ){
E. printStackTrace ();
AppLog. Erro ("startQrCode", e. getMessage ());
}
}
@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );
If (requestCode = Constant. REQ_QR_CODE & resultCode = RESULT_ OK ){
Bundle bundle = data. getExtras ();
ScanResult = bundle. getString (Constant. INTENT_EXTRA_KEY_QR_SCAN );
// Display the scanned Information
// MainActivity. showSussecc ("", scanResult );
New SweetAlertDialog (this, SweetAlertDialog. ERROR_TYPE)
. SetTitleText ("")
. SetContentText (scanResult)
. Show ();
}
}
I hope you will forgive me for writing a blog for the first time. If there are any mistakes, I can discuss them together. If there are any reposts, please describe the reposts.