Skip to content

Commit 6b6bf83

Browse files
committed
refactor
1 parent 0c405ac commit 6b6bf83

File tree

21 files changed

+292
-208
lines changed

21 files changed

+292
-208
lines changed

1.hello-world/14.read-video-webview/android/.gitignore

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
*.iml
22
.gradle
33
/local.properties
4-
.idea
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
510
.DS_Store
611
/build
712
/captures
Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
plugins {
22
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
34
}
45

56
android {
6-
compileSdkVersion 30
7-
buildToolsVersion "30.0.2"
7+
namespace 'com.dynamsoft.dbrjswebview'
8+
compileSdk 33
89

910
defaultConfig {
10-
applicationId "com.dynamsoft.javascript.readvideowebview"
11-
minSdkVersion 21
12-
targetSdkVersion 30
11+
applicationId "com.dynamsoft.dbrjswebview"
12+
minSdk 19
13+
targetSdk 33
1314
versionCode 1
1415
versionName "1.0"
1516

@@ -26,18 +27,19 @@ android {
2627
sourceCompatibility JavaVersion.VERSION_1_8
2728
targetCompatibility JavaVersion.VERSION_1_8
2829
}
29-
repositories {
30-
maven {
31-
url "https://download2.dynamsoft.com/maven/aar"
32-
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
3332
}
3433
}
3534

3635
dependencies {
37-
implementation 'androidx.appcompat:appcompat:1.2.0'
38-
implementation 'com.google.android.material:material:1.2.1'
39-
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
40-
testImplementation 'junit:junit:4.+'
41-
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
42-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
36+
37+
implementation 'androidx.core:core-ktx:1.7.0'
38+
implementation 'androidx.appcompat:appcompat:1.4.1'
39+
implementation 'com.google.android.material:material:1.5.0'
40+
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
41+
implementation 'androidx.webkit:webkit:1.4.0'
42+
testImplementation 'junit:junit:4.13.2'
43+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
44+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4345
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.dynamsoft.dbrjswebview
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.dynamsoft.dbrjswebview", appContext.packageName)
23+
}
24+
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dynamsoft.javascript.readvideowebview">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
34

45
<uses-permission android:name="android.permission.INTERNET" />
56
<uses-permission android:name="android.permission.CAMERA" />
7+
68
<application
79
android:allowBackup="true"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
811
android:fullBackupContent="@xml/backup_rules"
9-
android:icon="@mipmap/logodbr"
12+
android:icon="@mipmap/ic_launcher"
1013
android:label="@string/app_name"
11-
android:roundIcon="@mipmap/logodbr"
1214
android:supportsRtl="true"
13-
android:theme="@style/Theme.ReadVideoWebView">
15+
android:theme="@style/Theme.DynamsoftBarcode"
16+
tools:targetApi="31">
1417
<activity
1518
android:name=".MainActivity"
1619
android:exported="true">
@@ -19,10 +22,6 @@
1922

2023
<category android:name="android.intent.category.LAUNCHER" />
2124
</intent-filter>
22-
23-
<meta-data
24-
android:name="android.app.lib_name"
25-
android:value="" />
2625
</activity>
2726
</application>
2827

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0,user-scalable=no"">
7+
<meta name="description" content="Quickly read barcodes with Dynamsoft Barcode Reader from a live camera stream.">
8+
<meta name="keywords" content="camera based barcode reading">
9+
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/1.hello-world/1.hello-world.html">
10+
<title>Dynamsoft Barcode Reader Sample - Hello World (Decoding via Camera)</title>
11+
</head>
12+
13+
<body>
14+
Loading...
15+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.js"></script>
16+
<script>
17+
/** LICENSE ALERT - README
18+
* To use the library, you need to first specify a license key using the API "license" as shown below.
19+
*/
20+
21+
Dynamsoft.DBR.BarcodeReader.license = 'DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9';
22+
23+
/**
24+
* You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days.
25+
* Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license.
26+
* For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.11&utm_source=github#specify-the-license or contact [email protected].
27+
* LICENSE ALERT - THE END
28+
*/
29+
30+
(async function() {
31+
try {
32+
const scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance();
33+
/**
34+
* 'onFrameRead' is triggered after the library finishes reading a frame image.
35+
* There can be multiple barcodes on one image.
36+
*/
37+
scanner.onFrameRead = results => {
38+
console.log("Barcodes on one frame:");
39+
for (let result of results) {
40+
const format = result.barcodeFormatString;
41+
console.log(format + ": " + result.barcodeText);
42+
}
43+
};
44+
/**
45+
* 'onUniqueRead' is triggered only when a 'new' barcode is found.
46+
* The amount of time that the library 'remembers' a barcode is defined by
47+
* "duplicateForgetTime" in "ScanSettings". By default it is set to 3000 ms.
48+
*/
49+
scanner.onUniqueRead = (txt, result) => {
50+
alert(txt);
51+
console.log("Unique Code Found: ", result);
52+
}
53+
/**
54+
* 'show()' opens the camera and shows the video stream on the page.
55+
* After that, the library starts to scan the frame images continuously.
56+
*/
57+
await scanner.show();
58+
} catch (ex) {
59+
let errMsg;
60+
if (ex.message.includes("network connection error")) {
61+
errMsg = "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support ([email protected]) to acquire an offline license.";
62+
} else {
63+
errMsg = ex.message||ex;
64+
}
65+
console.error(errMsg);
66+
alert(errMsg);
67+
}
68+
})();
69+
</script>
70+
</body>
71+
72+
</html>

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

Lines changed: 0 additions & 77 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.dynamsoft.dbrjswebview
2+
3+
import android.Manifest
4+
import android.annotation.SuppressLint
5+
import android.content.pm.PackageManager
6+
import android.net.Uri
7+
import androidx.appcompat.app.AppCompatActivity
8+
import android.os.Bundle
9+
import android.util.Log
10+
import android.webkit.*
11+
import androidx.activity.result.contract.ActivityResultContracts
12+
import androidx.annotation.RequiresApi
13+
import androidx.core.content.ContextCompat
14+
import androidx.webkit.WebViewAssetLoader
15+
import androidx.webkit.WebViewClientCompat
16+
17+
class MainActivity : AppCompatActivity() {
18+
@SuppressLint("SetJavaScriptEnabled")
19+
override fun onCreate(savedInstanceState: Bundle?) {
20+
super.onCreate(savedInstanceState)
21+
setContentView(R.layout.activity_main)
22+
23+
val myWebView: WebView = findViewById(R.id.webview)
24+
myWebView.settings.javaScriptEnabled = true
25+
myWebView.settings.mediaPlaybackRequiresUserGesture = false
26+
myWebView.settings.domStorageEnabled = true
27+
Log.d("keillion Debug", "`myWebView.settings.cacheMode` default value: " + myWebView.settings.cacheMode.toString())
28+
myWebView.settings.cacheMode = WebSettings.LOAD_DEFAULT
29+
30+
myWebView.webViewClient = MyWebViewClient()
31+
myWebView.webChromeClient = MyWebChromeClient()
32+
33+
myWebView.loadUrl("https://appassets.androidplatform.net/assets/decodeBarcodeInVideo.html")
34+
}
35+
36+
// Warning: If you use online url, you don't need `LocalContentWebViewClient`
37+
// Refer: https://developer.android.com/develop/ui/views/layout/webapps/load-local-content?hl=en
38+
private inner class MyWebViewClient : WebViewClientCompat() {
39+
40+
private val assetLoader = WebViewAssetLoader.Builder()
41+
.addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this@MainActivity))
42+
.build()
43+
44+
@RequiresApi(21)
45+
override fun shouldInterceptRequest(
46+
view: WebView,
47+
request: WebResourceRequest
48+
): WebResourceResponse? {
49+
return assetLoader.shouldInterceptRequest(request.url)
50+
}
51+
52+
// to support API < 21
53+
@Deprecated("Deprecated in Java")
54+
override fun shouldInterceptRequest(
55+
view: WebView,
56+
url: String
57+
): WebResourceResponse? {
58+
return assetLoader.shouldInterceptRequest(Uri.parse(url))
59+
}
60+
}
61+
62+
var cameraPermissionReq: PermissionRequest? = null
63+
private inner class MyWebChromeClient : WebChromeClient() {
64+
@RequiresApi(21)
65+
override fun onPermissionRequest(request: PermissionRequest) {
66+
if(request.resources.contains(PermissionRequest.RESOURCE_VIDEO_CAPTURE)){
67+
// Refer: https://developer.android.com/training/permissions/requesting
68+
if(ContextCompat.checkSelfPermission(
69+
this@MainActivity,
70+
Manifest.permission.CAMERA
71+
) == PackageManager.PERMISSION_GRANTED){
72+
// You can use the API that requires the permission.
73+
request.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
74+
}else{
75+
// You can directly ask for the permission.
76+
// The registered ActivityResultCallback gets the result of this request.
77+
this@MainActivity.cameraPermissionReq = request
78+
requestCameraPermissionLauncher.launch(Manifest.permission.CAMERA)
79+
}
80+
}else{
81+
request.deny()
82+
}
83+
}
84+
}
85+
86+
// Refer: https://developer.android.com/training/permissions/requesting
87+
@RequiresApi(21)
88+
val requestCameraPermissionLauncher = registerForActivityResult(
89+
ActivityResultContracts.RequestPermission()
90+
) { isGranted: Boolean ->
91+
// Refer: https://www.dynamsoft.com/codepool/use-barcode-scanner-in-android-webview.html#set-up-webview
92+
this@MainActivity.runOnUiThread {
93+
if (isGranted) {
94+
// Permission is granted. Continue the action or workflow in your
95+
// app.
96+
cameraPermissionReq?.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
97+
} else {
98+
// Explain to the user that the feature is unavailable because the
99+
// feature requires a permission that the user has denied. At the
100+
// same time, respect the user's decision. Don't link to system
101+
// settings in an effort to convince the user to change their
102+
// decision.
103+
cameraPermissionReq?.deny()
104+
}
105+
cameraPermissionReq = null
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)