Skip to content

Commit 1bd3930

Browse files
committed
test pure js
1 parent be3ff2b commit 1bd3930

File tree

5 files changed

+48
-157
lines changed

5 files changed

+48
-157
lines changed

1.hello-world/14.read-video-webview/android/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dynamsoft.javascript.readvideowebview">
33

44
<uses-permission android:name="android.permission.INTERNET" />
5+
<uses-permission android:name="android.permission.CAMERA" />
56
<application
67
android:allowBackup="true"
78
android:fullBackupContent="@xml/backup_rules"

1.hello-world/14.read-video-webview/android/app/src/main/assets/index.html

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,45 @@
3232
* LICENSE ALERT - THE END
3333
*/
3434

35-
let pScanner = null;
36-
(async function () {
35+
(async function() {
3736
try {
38-
await Dynamsoft.DBR.BarcodeReader.loadWasm();
39-
window.DBR_Android.onWasmLoaded();
37+
const scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
38+
/**
39+
* 'onFrameRead' is triggered after the library finishes reading a frame image.
40+
* There can be multiple barcodes on one image.
41+
*/
42+
scanner.onFrameRead = results => {
43+
console.log("Barcodes on one frame:");
44+
for (let result of results) {
45+
const format = result.barcodeFormatString;
46+
console.log(format + ": " + result.barcodeText);
47+
}
48+
};
49+
/**
50+
* 'onUniqueRead' is triggered only when a 'new' barcode is found.
51+
* The amount of time that the library 'remembers' a barcode is defined by
52+
* "duplicateForgetTime" in "ScanSettings". By default it is set to 3000 ms.
53+
*/
54+
scanner.onUniqueRead = (txt, result) => {
55+
alert(txt);
56+
console.log("Unique Code Found: ", result);
57+
}
58+
/**
59+
* 'show()' opens the camera and shows the video stream on the page.
60+
* After that, the library starts to scan the frame images continuously.
61+
*/
62+
await scanner.show();
4063
} catch (ex) {
4164
let errMsg;
4265
if (ex.message.includes("network connection error")) {
4366
errMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license.";
4467
} else {
45-
errMsg = ex.message || ex;
68+
errMsg = ex.message||ex;
4669
}
4770
console.error(errMsg);
4871
alert(errMsg);
4972
}
5073
})();
51-
52-
async function startScanner() {
53-
let scanner = await (pScanner = pScanner || Dynamsoft.DBR.BarcodeScanner.createInstance());
54-
await scanner.setUIElement(document.getElementById('div-ui-container'));
55-
scanner.onUniqueRead = (txt, result) => {
56-
const format = result.barcodeFormatString;
57-
window.DBR_Android.onUniqueRead(format + " " + txt)
58-
};
59-
await scanner.show();
60-
}
61-
62-
async function stopScanner() {
63-
let scanner = await pScanner;
64-
scanner && scanner.hide();
65-
}
6674
</script>
6775
</body>
6876

1.hello-world/14.read-video-webview/android/app/src/main/java/com/dynamsoft/javascript/readvideowebview/DBRWebViewHelper.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

1.hello-world/14.read-video-webview/android/app/src/main/java/com/dynamsoft/javascript/readvideowebview/MainActivity.java

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,36 @@
1111
import android.webkit.WebChromeClient;
1212
import android.webkit.WebView;
1313
import android.webkit.WebViewClient;
14-
import android.widget.Button;
15-
import android.widget.Toast;
14+
import androidx.core.app.ActivityCompat;
15+
import androidx.core.content.ContextCompat;
1616

1717
public class MainActivity extends AppCompatActivity {
18+
static int Camera_Permission_Request_Code = 32765;
1819
WebView mWebView;
19-
DBRWebViewHelper dbrWebViewHelper;
20-
Button mButton;
2120

22-
@SuppressLint("ShowToast")
2321
@Override
2422
protected void onCreate(Bundle savedInstanceState) {
2523
super.onCreate(savedInstanceState);
2624
setContentView(R.layout.activity_main);
27-
mButton = findViewById(R.id.myButton);
28-
29-
// In order to avoid conflicts, you can use the following code to set the request code for requesting camera permission before you pollute
30-
// dbrWebViewHelper.setCameraPermissionRequestCode(Your code);
3125

3226
// Initialize WebView
3327
mWebView = findViewById(R.id.myWebview);
34-
// Pollute your WebView
35-
dbrWebViewHelper = new DBRWebViewHelper();
36-
dbrWebViewHelper.pollute(mWebView);
37-
Toast toast = Toast.makeText(this, "Loading Library...", Toast.LENGTH_LONG);
38-
toast.show();
28+
WebSettings settings = webView.getSettings();
29+
settings.setJavaScriptEnabled(true);
30+
settings.setDomStorageEnabled(true);
31+
settings.setMediaPlaybackRequiresUserGesture(false);
32+
3933
// for development, enabled debugging and clear html files cache
4034
WebView.setWebContentsDebuggingEnabled(true);
41-
mWebView.clearCache(true);
4235
mWebView.setWebChromeClient(new WebChromeClient() {
4336
@Override
4437
public void onPermissionRequest(final PermissionRequest request) {
38+
if (ContextCompat.checkSelfPermission(mActivity, "android.permission.CAMERA") != PackageManager.PERMISSION_GRANTED) {
39+
ActivityCompat.requestPermissions(mActivity, new String[]{"android.permission.CAMERA"}, Camera_Permission_Request_Code);
40+
}
41+
// else {
42+
// evaluateJavascript("startScanner()");
43+
// }
4544
request.grant(request.getResources());
4645
}
4746
});
@@ -50,29 +49,10 @@ public void onPermissionRequest(final PermissionRequest request) {
5049
mWebView.loadUrl("file:///android_asset/index.html");
5150
}
5251

53-
public void switchScanner(View view) {
54-
CharSequence text = mButton.getText();
55-
if (text.toString().equals("Start")) {
56-
mButton.setText(R.string.btn_stop);
57-
dbrWebViewHelper.startScanner();
58-
} else {
59-
mButton.setText(R.string.btn_start);
60-
dbrWebViewHelper.stopScanner();
61-
}
62-
}
63-
64-
public void showResult(String result) {
65-
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
66-
dialog.setTitle("Result:").setPositiveButton("OK", null).setMessage(result).show();
67-
}
68-
69-
public void showController() {
70-
runOnUiThread(() -> mButton.setVisibility(View.VISIBLE));
71-
}
52+
// @Override
53+
// public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
54+
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
55+
// //dbrWebViewHelper.cameraPermissionHandler(requestCode, permissions, grantResults);
7256

73-
@Override
74-
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
75-
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
76-
dbrWebViewHelper.cameraPermissionHandler(requestCode, permissions, grantResults);
77-
}
57+
// }
7858
}

1.hello-world/14.read-video-webview/android/app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,5 @@
2020

2121
</WebView>
2222

23-
<Button
24-
android:id="@+id/myButton"
25-
android:layout_width="wrap_content"
26-
android:layout_height="wrap_content"
27-
android:onClick="switchScanner"
28-
android:text="@string/btn_start"
29-
android:visibility="invisible"
30-
app:layout_constraintBottom_toBottomOf="parent"
31-
app:layout_constraintEnd_toEndOf="parent"
32-
app:layout_constraintHorizontal_bias="0.5"
33-
app:layout_constraintStart_toStartOf="parent"
34-
app:layout_constraintTop_toTopOf="parent"
35-
app:layout_constraintVertical_bias="0.75" />
36-
3723

3824
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)