In APP development, the two-dimensional code scanning function and the generation of two-dimensional code are often encountered. Android is mostly integrated with zxing, an open source project. Open source project address below to introduce you to the specific integration steps

The integration steps

Source code Demo access method

Follow [net development] wechat public account, reply [163] can be received. Net the world method, convenient for you and me to develop, more iOS technology dry goods waiting to receive, all documents will continue to update, welcome to pay attention to grow together!

1. The demo is displayed as follows:

1.1 the demo page

1.2 Scan Interface

It can be modified according to requirements. The screenshot of the interface in my actual project is as follows:

1.3 Generating A TWO-DIMENSIONAL Code

2. Import files

2.1 Download demo, copy the com.google.zxing5 package and com.utils package in demo and introduce them into your own project.

compile 'com. Google. Zxing: core: 3.3.0'
Copy the code

2.6 Changing the R File Reference Path Change the R file reference addresses in the following four files to reference R of this project

/ / com. Google replace your project package name can com. Google. Zxing. Activity. CaptureActivity com. Google. Zxing. Decoding. CaptureActivityHandler com.google.zxing.decoding.DecodeHandler com.google.zxing.view.ViewfinderViewCopy the code

3. Configure permissions

3.1 Add permission application code to Androidmanifest.xml:

    <uses-permission android:name="android.permission.INTERNET"/ > <! --> <uses-permission android:name="android.permission.VIBRATE"/ > <! --> <uses-permission Android :name="android.permission.CAMERA"/ > <! -- Camera permission --> <uses-feature Android :name="android.hardware.camera.autofocus"/ > <! -- Autofocus permission -->Copy the code

4. Function realization

Once the integration is complete, the scan function can be implemented 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; Private int REQUEST_CODE = 0x01; private int REQUEST_CODE = 0x01; Private int RESULT_OK = 0xA1; // Scan success return code private int RESULT_OK = 0xA1; @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()) {
            caseR.i.d.opengqrcodescan: // The QR code scanning screen is displayedif(CommonUtil.isCameraCanUse()){
                    Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                    startActivityForResult(intent, REQUEST_CODE);
                }else{
                    Toast.makeText(this,"Please open camera permissions for this app!",Toast.LENGTH_SHORT).show();
                }
                break;
            caseR.ic.reateqrcode: try {// Get input text. String STR = text.gettext ().toString().trim();if(str ! = null && !""Equals (STR. The trim ())) {/ / based on the input text to generate the corresponding qr code and display Bitmap mBitmap = EncodingHandler. CreateQRCode (text. The getText (), toString (), 500);if(mBitmap ! = null){ Toast.makeText(this,"Two-dimensional code generation success!",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 callbackif (resultCode == RESULT_OK) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("qr_scan_result"); Qrcodetext.settext (scanResult); }}}Copy the code

5. Source code analysis

5.1 Opening the QR Code Scanning screen

// Open the QR code scanning screenif(CommonUtil.isCameraCanUse()){
                    Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                    startActivityForResult(intent, REQUEST_CODE);
                }else{
                    Toast.makeText(this,"Please open camera permissions for this app!",Toast.LENGTH_SHORT).show();
                }
Copy the code

5.2 Generate corresponding TWO-DIMENSIONAL code according to the input text and display it

String STR = text.gettext ().toString().trim();if(str ! = null && !""Equals (STR. The trim ())) {/ / based on the input text to generate the corresponding qr code and display Bitmap mBitmap = EncodingHandler. CreateQRCode (text. The getText (), toString (), 500);if(mBitmap ! = null){ Toast.makeText(this,"Two-dimensional code generation success!",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();
                }
Copy the code

5.3 Callback scan Results

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Scan result callbackif (resultCode == RESULT_OK) { //RESULT_OK = -1
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("qr_scan_result"); Qrcodetext.settext (scanResult); }}Copy the code

I hope I can help you

If there is anything wrong or inadequate place, but also look for readers to make suggestions or suggestions

If you need to reprint, please contact me, after authorization can be reprinted, thank you