How to integrate Zxing Barcode Scanner without installing the actual zxing app (cannot resolve symbol: .android.CaptureActivity)?

前端 未结 8 1566
我寻月下人不归
我寻月下人不归 2020-12-07 10:04

I want to integrate zxing scanner into my app without needed of external application (zxing scanner from play store). This is my code

Button scan = (Button)         


        
8条回答
  •  忘掉有多难
    2020-12-07 10:22

    As all the answers I found so far are based on Eclipse and are quite dated as of now, I'm adding my answer to add ZXing as a standalone library on Android Studio (2.1.2).

    I've put the compiled project on Github https://github.com/tarun0/ZXing-Standalone-library Just add the zxing_standalone module in your project and you'll be good to go. For details or to compile it for the newer versions, continue reading.

    It may seem long procedure but once you go through it, you'll find it quite easy and short.

    1. Download the ZXing library source code from https://github.com/zxing/zxing/ We'll only need the android directory from this. So if you happen to have this folder already, you don't need to download the whole branch which is about 126MB in size.
    2. Download the latest core-x.x.x.jar from http://repo1.maven.org/maven2/com/google/zxing/core/3.2.1/
    3. Add the android project in your own project. To do this, first, choose the Project from the Project Explorer(refer to the image)

    1. Now click on your project and then right click it and select New>Module>Import Gradle Project. Now, in select the android directory from the downloaded source code (in step 1). (Reference pic below for the absolute novice). Optionally, change the name and let the default settings for the import.
    2. In this imported project, make a folder libs and put the core.jar file downloaded in step 2 in this folder. Then open Project Structure from the file menu and add this core.jar file as a dependency.

    1. Download CameraConfigurationalUtils.java and paste it in the project.

    2. Now make a few modifications in the imported project's gradle file. Change the apply plugin: 'com.android.application' to apply plugin: 'com.android.library' to tell the system that it's a library. And remove the applicationId ... statement.

    3. In the imported project's Manifest.xml file, make following modifications. In the tag, remove the android:icon... and android:logo...and remove this from the intent filter as this is a library now and is not supposed to be on the launcher (if you don't remove this, you'll end up having two launcher activities).

    4. In your project's build.gradle file, in the dependencies block, add this line compile project (':android') Here, replace the android with the name you chose while importing the project in step 4. Sync and clean the project. You'll see some errors in switch statements. Click on those switch cases and select the option replace with if option provided by Android Studio.

    That's it. Now you can use the ZXing library in your own app. :)

    To use the added library, just use the Intents as stated in the very first answer above (copying the same codes only):

    While scanning (like on clicking a button), send Intent:

    Intent intent = new Intent(getApplicationContext(),CaptureActivity.class);
    intent.setAction("com.google.zxing.client.android.SCAN");
    intent.putExtra("SAVE_HISTORY", false);
    startActivityForResult(intent, 0);
    

    Then, in OnActivityResult:

    if (requestCode == 0) {
    if (resultCode == RESULT_OK) {
    String contents = data.getStringExtra("SCAN_RESULT");
    Log.d(TAG, "contents: " + contents);
    } else if (resultCode == RESULT_CANCELED) {
    // Handle cancel
    Log.d(TAG, "RESULT_CANCELED");
    }
    }
    

    I tried to be as descriptive as possible. I hope people find it useful.

    Please read this answer by one of the authors of the code regarding copying of the code into your own app: https://stackoverflow.com/a/9942761

    References: https://stackoverflow.com/a/29818279 https://stackoverflow.com/a/29960361 And a few other blogs/SO answers.

提交回复
热议问题