'Zxing QR code scanner integration with webview
I'm trying to integrate the Zxing qr code scanner with my android webview app. I successfully imported a zxing project and deployed it. The scanner worked without any issues. Now i'm trying to integrate this scanner with my webview.
The scenario i'm working on is, "when user opens the app, it should boot the webview, and inside a webview there will be a link called "Scan", on clicking the link it should send an intent to native java code "Zxing scanner function" to start the scanner and switch off webview. When the scan is complete, the decoded data should be passed on to the webview, the scanner page should close and webview reopened.
Here is the code i have worked up so far
MainActivity.Java
package com.androiddvlpr.zxingandroid;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.budiyev.android.codescanner.CodeScanner;
import com.budiyev.android.codescanner.CodeScannerView;
import com.budiyev.android.codescanner.DecodeCallback;
import com.google.zxing.Result;
public class MainActivity extends AppCompatActivity {
private CodeScanner mCodeScanner;
private WebView webView;
private class WebChromeClientDemo extends WebChromeClient {
private WebChromeClientDemo() {
}
}
private class WebViewClientDemo extends WebViewClient {
private WebViewClientDemo() {
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
@SuppressLint({"SetJavaScriptEnabled"})
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.simpleWebView);
this.webView.setWebViewClient(new WebViewClientDemo());
this.webView.setWebChromeClient(new WebChromeClientDemo());
this.webView.addJavascriptInterface(new WebAppInterface(this), "Android");
WebSettings settings = this.webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
CookieManager.getInstance();
this.webView.loadUrl("https://mbracecloud.com/camera1.php");
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
== PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.CAMERA}, 123);
} else {
//startScanning();
}
}
public void startScanning() {
CodeScannerView scannerView = findViewById(R.id.scanner_view);
mCodeScanner = new CodeScanner(this, scannerView);
mCodeScanner.setDecodeCallback(new DecodeCallback() {
@Override
public void onDecoded(@NonNull final Result result) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, result.getText(), Toast.LENGTH_SHORT).show();
}
});
}
});
scannerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCodeScanner.startPreview();
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 123) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Camera permission granted", Toast.LENGTH_LONG).show();
startScanning();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
if(mCodeScanner != null) {
mCodeScanner.startPreview();
}
}
@Override
protected void onPause() {
if(mCodeScanner != null) {
mCodeScanner.releaseResources();
}
super.onPause();
}
}
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.budiyev.android.codescanner.CodeScannerView
android:id="@+id/scanner_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:autoFocusButtonColor="@android:color/white"
app:autoFocusButtonVisible="true"
app:flashButtonColor="@android:color/white"
app:flashButtonVisible="true"
app:frameColor="@android:color/white"
app:frameCornersSize="50dp"
app:frameCornersRadius="0dp"
app:frameAspectRatioWidth="1"
app:frameAspectRatioHeight="1"
app:frameSize="0.75"
app:frameThickness="2dp"
app:maskColor="#77000000"/>
<WebView
android:id="@+id/simpleWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true" />
</android.support.constraint.ConstraintLayout>
Any help would be much appreciated as we are stuck with this day for almost a month.
Solution 1:[1]
In WebChromeClientDemo class, add
@Override
public void onPermissionRequest(PermissionRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
request.grant(request.getResources());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani |
