简体   繁体   中英

How can I apply the QR Code scanner to my webview application and then put the QR Code in the Google search engine, for example

How can I apply the QR Code scanner to my Webview application in android studio and then put the QRCode in the Google search engine, for example, the application now puts the QR Code in the result box. Please help me.

public class MainActivity extends AppCompatActivity {
    Button btn_scan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_scan = findViewById(R.id.btn_scan);
        btn_scan.setOnClickListener(v -> {
            scanCode();
        });
    }

    private void scanCode() {
        ScanOptions options = new ScanOptions();
        options.setPrompt("Volume up to flash on");
        options.setBeepEnabled(true);
        options.setOrientationLocked(true);
        options.setCaptureActivity(CaptureAct.class);
        barlaucher.launch(options);
    }

    ActivityResultLauncher<ScanOptions> barlaucher = registerForActivityResult(new ScanContract(), result -> {
        if (result.getContents() != null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setTitle("Result");
            builder.setMessage(result.getContents());
            builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            }).show();
        }
    });
}

It might help us to approach this problem in phases.

  • Tasks
    • Set up QR Code 'grabber'.
    • Set up grabbed QR Code 'parser'.
    • Set up WebView component initialized to load 'google.com' in stable efficient manner.
    • Set up basic JS interop for DOM manipulation. Best to use native Android API for JS interop . Every time we're just making one simple pass from App to WebView anyway, aka the representation of the QR Code.
    • Make sure QR Code 'parsed' data is converted to HTML friendly string .
    • In the meantime, we just load up google and inspect the seachbox and search button DOM element vars for the Google main page. This is not really app related .
    • We target these DOM element vars, set the search box text to final converted parsed data, activate the search submit button via JS and we should be more or less on the right path.

Personally, I'd possibly just load the QR Code stuff directly so it gets handled fairly proper by native WebView framework. This will mitigate well for general situations. But any time we allow end-users to interact with DOM like that we are inviting exponential JS havoc . To be honest this would be a rather alarming feature to have on an official app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM