In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Android how to achieve QR code scanning and generation function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to achieve QR code scanning and generation function" bar!
The 1.demo is shown as follows:
1.1demo Home Page
1.2 scanning interface
You can modify it according to the requirements. The screenshots of the interface in my actual project are as follows:
1.3 generate a QR code
two。 Import file
Download demo, copy the com.google.zxing5 package and com.utils package from demo into your project.
2.2 copy the layout activity_scanner.xml and toolbar_scanner.xml in the demo of this project
2.3 copy the resource directory raw to this project, and beep.ogg is the tone when the scan is successful.
2.4 copy or merge file contents attrs.xml/colors.xml/ids.xml three files.
2.5 add references to the build.gradle file
Compile 'com.google.zxing:core:3.3.0'
2.6 modify R file reference path modify the R file reference address in the following 4 files, and reference the R of this project
/ / com.google can be com.google.zxing.activity.CaptureActivitycom.google.zxing.decoding.CaptureActivityHandlercom.google.zxing.decoding.DecodeHandlercom.google.zxing.view.ViewfinderView by replacing it with the package name of your own project
3. Permission configuration
3.1 add permission application code to AndroidManifest.xml:
4. Function realization
After the above integration is completed, the code scanning function can be realized by calling CaptureActivity. MainActivity source code:
Public class MainActivity extends AppCompatActivity {@ BindView (R.id.openQrCodeScan) Button openQrCodeScan; @ BindView (R.id.text) EditText text; @ BindView (R.id.CreateQrCode) Button CreateQrCode; @ BindView (R.id.QrCode) ImageView QrCode; @ BindView (R.id.qrCodeText) TextView qrCodeText; / / request code for opening scan interface private int REQUEST_CODE = 0x01; / / return code private int RESULT_OK = 0xA1 for successful scanning; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState) SetContentView (R.layout.activity_main); ButterKnife.bind (this);} @ OnClick ({R.id.openQrCodeScan, R.id.CreateQrCode}) public void onClick (View view) {switch (view.getId ()) {case R.id.openQrCodeScan: / / Open the QR code scanning interface if (CommonUtil.isCameraCanUse ()) {Intent intent = new Intent (MainActivity.this, CaptureActivity.class); startActivityForResult (intent, REQUEST_CODE) } else {Toast.makeText (this, "Please open the camera permission for this application!" , Toast.LENGTH_SHORT) .show ();} break; case R.id.CreateQrCode: try {/ / get the input text information String str = text.getText (). ToString (). Trim (); if (str! = null & &! ".equals (str.trim () {/ / generate the corresponding QR code based on the input text and display Bitmap mBitmap = EncodingHandler.createQRCode (text.getText (). ToString (), 500). If (mBitmap! = null) {Toast.makeText (this, "QR code generated successfully!" , Toast.LENGTH_SHORT) .show (); QrCode.setImageBitmap (mBitmap);}} else {Toast.makeText (this, "text messages cannot be empty!" , Toast.LENGTH_SHORT) .show ();}} catch (WriterException e) {e.printStackTrace ();} break;}} @ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); / / scan result callback if (resultCode = = RESULT_OK) {/ / RESULT_OK =-1 Bundle bundle = data.getExtras (); String scanResult = bundle.getString ("qr_scan_result") / / display the scanned information to qrCodeText.setText (scanResult);}
5. Source code analysis
5.1 Open the QR code scanning interface
/ / Open the QR code scanning interface if (CommonUtil.isCameraCanUse ()) {Intent intent = new Intent (MainActivity.this, CaptureActivity.class); startActivityForResult (intent, REQUEST_CODE);} else {Toast.makeText (this, "Please open the camera permission for this application!" , Toast.LENGTH_SHORT. Show ();}
5.2 generate the corresponding QR code according to the input text and display it
Try {/ / gets the input text information String str = text.getText (). ToString (). Trim (); if (str! = null & &! ".equals (str.trim () {/ / generates the corresponding QR code based on the input text and displays Bitmap mBitmap = EncodingHandler.createQRCode (text.getText (). ToString (), 500); if (mBitmap! = null) {Toast.makeText (this," QR code generated successfully! " , Toast.LENGTH_SHORT) .show (); QrCode.setImageBitmap (mBitmap);}} else {Toast.makeText (this, "text messages cannot be empty!" , Toast.LENGTH_SHORT) .show ();}} catch (WriterException e) {e.printStackTrace ();}
5.3 callback of scan results
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); / / scan result callback if (resultCode = = RESULT_OK) {/ / RESULT_OK =-1 Bundle bundle = data.getExtras (); String scanResult = bundle.getString ("qr_scan_result"); / / display the scanned information to qrCodeText.setText (scanResult) }} Thank you for your reading, the above is the content of "how to achieve the function of scanning and generating QR code in Android". After the study of this article, I believe you have a deeper understanding of how to achieve the function of scanning and generating QR code in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.