QR code for Android development, android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.