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

前端 未结 8 1524
我寻月下人不归
我寻月下人不归 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:28

    I am really late but I wish to reply on this for someone else to be helped later. This is not to say that the above methods and solution are wrong, Its just an additional info so, for the developer he/she will choose the better way. It is good to have thousand way to go than having one.

    So, let's start into our gradle and add

    compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
    compile 'com.google.zxing:core:3.2.1'
    

    Calling the module like (eg: on button click):

    IntentIntegrator integrator = new IntentIntegrator(Home.this);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                integrator.setPrompt("Scan Code");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(true);
                integrator.setBarcodeImageEnabled(false);
                integrator.initiateScan();
    

    Get the results like:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        if(intentResult != null) {
            if(intentResult.getContents() == null) {
                Log.d("MainActivity", "Cancelled");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
    
            } else {
                Log.d("MainActivity", "Scanned");
                Toast.makeText(this, "Scanned: " + intentResult.getContents(), Toast.LENGTH_LONG).show();
            }
        }
    

    }

    For more info you can check the link https://github.com/pethoalpar/ZxingExample

    0 讨论(0)
  • 2020-12-07 10:34

    Elaborating stackex's answer.... Create an activity with any name.

    public class CaptureActivityAnyOrientation extends CaptureActivity {
    }
    

    In manifest define the desired orientation or leave like below to work in both landscape and portait.

    <activity android:name=".CaptureActivityAnyOrientation"
          android:screenOrientation="fullSensor"
          android:stateNotNeeded="true"
          android:theme="@style/zxing_CaptureTheme"
          android:windowSoftInputMode="stateAlwaysHidden"/>
    

    Finally use the activity created as setCaptureActivity.

    IntentIntegrator.forSupportFragment(fragment)
                        .setCaptureActivity(CaptureActivityAnyOrientation.class)
                        .setOrientationLocked(false)
                        .setBeepEnabled(true)
                        .addExtra("PROMPT_MESSAGE", "Scan QR Code")
                        .initiateScan(IntentIntegrator.QR_CODE_TYPES);
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题