Search code examples
androidhtmlbarcode

Android barcode scanner integration with web page


I have been researching all morning about integrating an android barcode scanner app into a web page, but haven't found exactly what I need to know. I want to have a web page that the user can fill in text fields by using an android barcode scanner. So the user would be on a web page and would either click inside the text field or click a button next to the text field that would start the android barcode scanner. They would then scan the barcode and the text field would be filled in.

I have found solutions on how to do this and then go to a different page, but it is important that the user stays on the same page. I have seen the zxing project and thought that might be able to be used, but I'm not sure if it allows for the page to stay the same.

I'm pretty sure this is possible and is wondering if any one could give me a high level overview on how they would do it. I was thinking it might be able to be done with an ajax request that gets submitted on a button click. The ajax request would get sent to my server, the server would send something to the android device that would start the scanner and return the data which in turn gets sent back in the ajax response. Is there any way to cut out the server though and just have the android browser starting the barcode scanner? Thank you for your time and I appreciate any discussion on it.


Solution

  • Using a javascript interface and loadurl(javascript...) you can communicate with your webpage from Android

    public void loadScript(String script){      
        webview.loadUrl("javascript:(function() { " + script + "})()");             
    }
    
    private class JavaScriptInterface {     
        public void startQRScan() {
            ...
        }
    }
    

    There are plenty of examples on google.