Android implements QR code scanning function (1) ZXing plug-in access

Introduction
Regarding the implementation of the Android QR code scanning function, there is a lot of relevant information on the Internet. After comparison, I chose ZXing, which was modified by my predecessors, to directly connect it to the project. This demo was specially made to introduce the whole process.

(Latest update) The access method explained in this article is a bit difficult for some new developers, so we made a library version of the code scanning library, which is more convenient for access. If you don’t want to care about the details, you can go directly to Android to implement the QR code. Scanning function (5) - Encapsulation and access to ZXing scanning library.

Effect preview:
The above picture shows the effect first (the simulator does not have a camera, so the recording effect is not good, so just leave it at that)

Integration step
1. Copy the com.google.zxing5 packages in the demo of this project and introduce them into your own project.

2. Copy the layout activity_scanner.xml and toolbar_scanner.xml in the demo of this project

3. Copy the resource directory raw to this project. beep.ogg is the prompt sound when the scan is successful.

4. Copy or merge the three files attrs.xml/colors.xml/ids.xml.

5. Add references to the build.gradle file

compile 'com.google.zxing:core:3.3.0'

6. Modify the R file reference path
. Modify the R file reference addresses in the following four files to reference the R of this project.

com.google.zxing.activity.CaptureActivity
com.google.zxing.decoding.CaptureActivityHandler
com.google.zxing.decoding.DecodeHandler
com.google.zxing.view.ViewfinderView
 

This ends the integration part. Let’s take a look at how to implement the function.


Add permission application code in permission configuration AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" /> <!-- Network permission-->
<uses-permission android:name="android.permission.VIBRATE" /> <!-- Vibration permission- ->
<uses-permission android:name="android.permission.CAMERA" /> <!-- Camera permission--> <
uses-feature android:name="android.hardware.camera.autofocus" /> <!- - Auto focus permissions -->

Register CaptureActivity:

<application...
    <activity android:name="com.google.zxing.activity.CaptureActivity"/>
</application>

After Android 6.0, Camera needs to add dynamic permission application code, which will be given in the implementation part below.

Function Implementation
After completing the above integration, the QR code scanning function can be realized by calling CaptureActivity.
MainActivity source code part:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button btnQrCode; // 扫码
    TextView tvResult; // 结果

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        btnQrCode = (Button) findViewById(R.id.btn_qrcode);
        btnQrCode.setOnClickListener(this);

        tvResult = (TextView) findViewById(R.id.txt_result);
    }

    // Start scanning code
    private void startQrCode() {         if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {             if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {                 Toast. makeText(MainActivity.this, "Please go to the permission center to open the camera access permission of this application", Toast.LENGTH_LONG).show(); }             //             Apply for             permissionActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission .CAMERA}, Constant.REQ_PERM_CAMERA);             return;         }         // QR code scanning         Intent intent = new Intent(MainActivity.this, CaptureActivity.class);










        startActivityForResult(intent, Constant.REQ_QR_CODE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_qrcode:
                startQrCode();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {         super.onActivityResult(requestCode, resultCode, data);         //Scan result callback         if (requestCode == Constant.REQ_QR_CODE && resultCode == RESULT_OK) {             Bundle bundle = data.getExtras();             String scanResult = bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN);             //Display the scanned information             tvResult.setText(scanResult);         }     }








    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {         super.onRequestPermissionsResult(requestCode, permissions, grantResults);         switch (requestCode) {             case Constant.REQ_PERM_CAMERA:                 // Camera permission application                 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                     // Obtain authorization                     startQrCode();                 } else {                     // Authorization is prohibited                     Toast.makeText(MainActivity.this, "Please go to the permission center to open this App's camera access permissions", Toast.LENGTH_LONG).show();                 }











                break;
        }
    }


}

Before initiating CaptureActivity, you need to apply for dynamic permissions

if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
   // 申请权限
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, Constant.REQ_PERM_CAMERA);
    return;
}

Application results are processed in the onRequestPermissionsResult method, please refer to the comments.

 @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {     super.onRequestPermissionsResult(requestCode, permissions, grantResults);     switch (requestCode) {         case Constant.REQ_PERM_CAMERA:             // Camera permission application             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                 // Obtain authorization                 startQrCode();             } else {                 // Authorization is prohibited                 Toast.makeText(MainActivity.this, "Please go to the permission center to open this App's camera access permissions", Toast.LENGTH_LONG).show();             }             break;     } }













Obtaining scanning results
zxing's CaptureActivity has perfectly implemented scanning and result return. We only need to process the return result on the caller.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     //Scan result callback     if (requestCode == Constant.REQ_QR_CODE && resultCode == RESULT_OK) {         Bundle bundle = data.getExtras();         String scanResult = bundle.getString(Constant.INTENT_EXTRA_KEY_QR_SCAN);         //Display the scanned information         tvResult.setText(scanResult);     } }









 

A TextView is used here to present the scan code results.

Conclusion:
The Android code scanning function has been completed here, and the code can be scanned and processed in real time by calling the camera, and the results can be used directly in the project. Thanks to the contributions of relevant open source authors, it is so easy for us to integrate the code scanning function.

References

http://www.jianshu.com/p/e80a85b17920
Source code download
Upload the source code to csdn and download it if necessary. (Old version)
http://download.csdn.net/detail/ahuyangdong/9915661

Github project address (keep updated), the adaptation problem of photo album selection has been basically solved:
https://github.com/ahuyangdong/QrCodeDemo4
————————————
Copyright statement: This article This is an original article by CSDN blogger "ahuyangdong" and follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement when reprinting.
Original link: https://blog.csdn.net/ahuyangdong/article/details/76405557
————————————
Copyright Statement: This article is the original work of CSDN blogger “Oracle 18” This article follows the CC 4.0 BY-SA copyright agreement. Please attach a link to the original source and this statement when reprinting.
Original link: https://blog.csdn.net/weixin_72957457/article/details/126702804

Guess you like

Origin blog.csdn.net/weixin_42504805/article/details/133354499