Android uses Zxing to scan QR code and jump to details page

It's just like the picture above


Because it's integrated by the third party, so|||
The old pattern first injected dependency:

//ABCD QR code
    compile 'cn.yipianfengye.android:zxing-library:2.1'

Add the permission to open the camera in the Manifest registration list:

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.INTERNET" />

Then there is the activity main.xml layout file. Give a control to trigger the click event (click to open the camera):

<TextView
        android:id="@+id/tvSys"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Order me to sweep"
        android:textSize="22sp"/>

Then create a new layout file to jump to display the details of the QR Code:

    <EditText
        android:id="@+id/receptionTv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp" />

    <WebView
        android:id="@+id/wb"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>

Then a key point is to create a new class to inherit the application and initialize the zxing package:

public class ZXingApplication extends Application {
    //oncreate method
    @Override
    public void onCreate() {
        super.onCreate();
        //Initializing the ZXING package
        ZXingLibrary.initDisplayOpinion(this);
    }
}

Do not forget to declare the following application class in the ManiFast registration list:

<application
        android:name=".ZXingApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        //This is to jump to the details page and don't forget to declare this kind of
        <activity android:name=".ZXingActivity" />
    </application>

Then there is the code of MainActivity:

public class MainActivity extends AppCompatActivity {
    //Define a global static constant
    private static final int REQUEST_CODE = 001;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Permission in motion
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            //Request write? External? Storage permission
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 1);
        }
        //Find control
        TextView tvSys = (TextView) findViewById(R.id.tvSys);
        //Click event
        tvSys.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //To call the system camera function is to jump to the camera interface
                Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
                //The reason to jump with this method is: in order to call back the method of onActivityResult below
                startActivityForResult(intent, REQUEST_CODE);
            }
        });

    }
    //Scan return value
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            if (bundle == null) {
                return;
            }
            if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
                //This is to get the information scanned by parsing and convert it into a string
                String result = bundle.getString(CodeUtils.RESULT_STRING);

                Toast.makeText(this, "Analytical results:" + result, Toast.LENGTH_LONG).show();
                //Analyze the scanned QR code and jump to the page
                Intent intent = new Intent(MainActivity.this, ZXingActivity.class);
                //Take the information (i.e. string) scanned and parsed to the details page
                intent.putExtra("path", result);
                startActivity(intent);
            } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
                //Otherwise, the chieftain fails to parse the QR code
                Toast.makeText(this, "Failed to parse QR code:", Toast.LENGTH_LONG).show();
            }
        }

    }
}

Then the page of jump details is the ZXingActivity class:

public class ZXingActivity extends Activity {
    //Control set global
    private WebView wb;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zxing_webview_layout);
        //Find control
        wb = findViewById(R.id.wb);
        TextView receptionTv = findViewById(R.id.receptionTv);
        //Support js language
        wb.getSettings().setJavaScriptEnabled(true);
        // Zoom to screen size
        wb.getSettings().setLoadWithOverviewMode(true);
        //Supports scaling
        wb.getSettings().setSupportZoom(true);
        //Declare an Intent intent to accept the value from MainActivity
        Intent intent = getIntent();
        //Get the value passed by MainActivity and return a string,
        //The password should be consistent
        String jxString = intent.getStringExtra("path");
        //Assign string to input box
        receptionTv.setText(jxString);
        //webVew to load web pages
        wb.loadUrl(jxString);
        //Set to open with your own browser
        wb.setWebViewClient(new MyWebViewClient());

        //Set its progress
        /*wb.setWebChromeClient(new WebChromeClient() {


            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                //Get int value of current progress

            }
        });

    }*/

    //Allow to return
   /* @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && wb.canGoBack()) {
            //Let webView return to the previous level
            wb.goBack();
        }
        return true;
    }*/

    //Custom browser
    class MyWebViewClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);

            return super.shouldOverrideUrlLoading(view, url);
        }


    }
    //Prevent memory leaks
    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}

come to an end

Several points are emphasized:
1. My Android studio is version 2.3.3, under SDK - v7:26. + '

compile 'com.android.support:appcompat-v7:26.+' 

Tags: Android xml SDK

Posted on Wed, 06 May 2020 08:54:39 -0700 by Thauwa