Skip to content

Commit 44347cb

Browse files
author
Cube
authored
Merge pull request #108 from Dynamsoft/_dev
Dev
2 parents 1894d20 + d06c5d2 commit 44347cb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1798
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Hello-world for Android WebView - Dynamsoft Barcode Reader Sample
2+
3+
This sample demonstrates how to use the [Dynamsoft Barcode Reader](https://www.dynamsoft.com/barcode-reader/overview/) JS Edition in Android.
4+
5+
If you want to learn how to use the Android Edition SDK in javascript, you can check [Android WebView Barcode Scanning](https://github.com/Dynamsoft/barcode-reader-mobile-samples/tree/main/android/JavaScript/WebViewBarcodeScanning).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
plugins {
2+
id 'com.android.application'
3+
id 'org.jetbrains.kotlin.android'
4+
}
5+
6+
android {
7+
namespace 'com.dynamsoft.dbrjswebview'
8+
compileSdk 33
9+
10+
defaultConfig {
11+
applicationId "com.dynamsoft.dbrjswebview"
12+
minSdk 19
13+
targetSdk 33
14+
versionCode 1
15+
versionName "1.0"
16+
17+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18+
}
19+
20+
buildTypes {
21+
release {
22+
minifyEnabled false
23+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24+
}
25+
}
26+
compileOptions {
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = '1.8'
32+
}
33+
}
34+
35+
dependencies {
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'
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.CAMERA" />
7+
8+
<application
9+
android:allowBackup="true"
10+
android:dataExtractionRules="@xml/data_extraction_rules"
11+
android:fullBackupContent="@xml/backup_rules"
12+
android:icon="@mipmap/ic_launcher"
13+
android:label="@string/app_name"
14+
android:supportsRtl="true"
15+
android:theme="@style/Theme.DynamsoftBarcode"
16+
tools:targetApi="31">
17+
<activity
18+
android:name=".MainActivity"
19+
android:exported="true">
20+
<intent-filter>
21+
<action android:name="android.intent.action.MAIN" />
22+
23+
<category android:name="android.intent.category.LAUNCHER" />
24+
</intent-filter>
25+
</activity>
26+
</application>
27+
28+
</manifest>
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/dynamsoft-javascript-barcode@9.6.20/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.20&utm_source=github#specify-the-license or contact support@dynamsoft.com.
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 (support@dynamsoft.com) 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>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.webkit.*
10+
import androidx.activity.result.contract.ActivityResultContracts
11+
import androidx.annotation.RequiresApi
12+
import androidx.core.content.ContextCompat
13+
import androidx.webkit.WebViewAssetLoader
14+
import androidx.webkit.WebViewClientCompat
15+
16+
class MainActivity : AppCompatActivity() {
17+
@SuppressLint("SetJavaScriptEnabled")
18+
override fun onCreate(savedInstanceState: Bundle?) {
19+
super.onCreate(savedInstanceState)
20+
setContentView(R.layout.activity_main)
21+
22+
val myWebView: WebView = findViewById(R.id.webview)
23+
myWebView.settings.javaScriptEnabled = true
24+
myWebView.settings.mediaPlaybackRequiresUserGesture = false
25+
myWebView.settings.domStorageEnabled = true
26+
27+
myWebView.webViewClient = MyWebViewClient()
28+
myWebView.webChromeClient = MyWebChromeClient()
29+
30+
myWebView.loadUrl("https://appassets.androidplatform.net/assets/decodeBarcodeInVideo.html")
31+
}
32+
33+
// Warning: If you use online url, you don't need `LocalContentWebViewClient`
34+
// Refer: https://developer.android.com/develop/ui/views/layout/webapps/load-local-content?hl=en
35+
private inner class MyWebViewClient : WebViewClientCompat() {
36+
37+
private val assetLoader = WebViewAssetLoader.Builder()
38+
.addPathHandler("/assets/", WebViewAssetLoader.AssetsPathHandler(this@MainActivity))
39+
.build()
40+
41+
@RequiresApi(21)
42+
override fun shouldInterceptRequest(
43+
view: WebView,
44+
request: WebResourceRequest
45+
): WebResourceResponse? {
46+
return assetLoader.shouldInterceptRequest(request.url)
47+
}
48+
49+
// to support API < 21
50+
@Deprecated("Deprecated in Java")
51+
override fun shouldInterceptRequest(
52+
view: WebView,
53+
url: String
54+
): WebResourceResponse? {
55+
return assetLoader.shouldInterceptRequest(Uri.parse(url))
56+
}
57+
}
58+
59+
var cameraPermissionReq: PermissionRequest? = null
60+
private inner class MyWebChromeClient : WebChromeClient() {
61+
@RequiresApi(21)
62+
override fun onPermissionRequest(request: PermissionRequest) {
63+
if(request.resources.contains(PermissionRequest.RESOURCE_VIDEO_CAPTURE)){
64+
// Refer: https://developer.android.com/training/permissions/requesting
65+
if(ContextCompat.checkSelfPermission(
66+
this@MainActivity,
67+
Manifest.permission.CAMERA
68+
) == PackageManager.PERMISSION_GRANTED){
69+
// You can use the API that requires the permission.
70+
request.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
71+
}else{
72+
// You can directly ask for the permission.
73+
// The registered ActivityResultCallback gets the result of this request.
74+
this@MainActivity.cameraPermissionReq = request
75+
requestCameraPermissionLauncher.launch(Manifest.permission.CAMERA)
76+
}
77+
}else{
78+
request.deny()
79+
}
80+
}
81+
}
82+
83+
// Refer: https://developer.android.com/training/permissions/requesting
84+
@RequiresApi(21)
85+
val requestCameraPermissionLauncher = registerForActivityResult(
86+
ActivityResultContracts.RequestPermission()
87+
) { isGranted: Boolean ->
88+
// Refer: https://www.dynamsoft.com/codepool/use-barcode-scanner-in-android-webview.html#set-up-webview
89+
this@MainActivity.runOnUiThread {
90+
if (isGranted) {
91+
// Permission is granted. Continue the action or workflow in your
92+
// app.
93+
cameraPermissionReq?.grant(arrayOf(PermissionRequest.RESOURCE_VIDEO_CAPTURE))
94+
} else {
95+
// Explain to the user that the feature is unavailable because the
96+
// feature requires a permission that the user has denied. At the
97+
// same time, respect the user's decision. Don't link to system
98+
// settings in an effort to convince the user to change their
99+
// decision.
100+
cameraPermissionReq?.deny()
101+
}
102+
cameraPermissionReq = null
103+
}
104+
}
105+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:aapt="http://schemas.android.com/aapt"
3+
android:width="108dp"
4+
android:height="108dp"
5+
android:viewportWidth="108"
6+
android:viewportHeight="108">
7+
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
8+
<aapt:attr name="android:fillColor">
9+
<gradient
10+
android:endX="85.84757"
11+
android:endY="92.4963"
12+
android:startX="42.9492"
13+
android:startY="49.59793"
14+
android:type="linear">
15+
<item
16+
android:color="#44000000"
17+
android:offset="0.0" />
18+
<item
19+
android:color="#00000000"
20+
android:offset="1.0" />
21+
</gradient>
22+
</aapt:attr>
23+
</path>
24+
<path
25+
android:fillColor="#FFFFFF"
26+
android:fillType="nonZero"
27+
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
28+
android:strokeWidth="1"
29+
android:strokeColor="#00000000" />
30+
</vector>

0 commit comments

Comments
 (0)