Skip to content

Commit 6795442

Browse files
authored
Merge pull request #316 from dynamsoft-docs/preview
update to internal commit 18533b18
2 parents 1997e8d + 6a08aef commit 6795442

File tree

2 files changed

+244
-217
lines changed

2 files changed

+244
-217
lines changed

programming/android/user-guide.md

Lines changed: 189 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -114,64 +114,64 @@ The first step in code configuration is to include a valid license in the `Barco
114114

115115
We first start with the package imports and then start implementing the MainActivity class, which starts with some simple Android UI configuration and creating the TextView that will display the results, followed by defining the license via the `setLicense` method of `BarcodeScannerConfig`.
116116

117-
<div class="sample-code-prefix"></div>
118-
>- Java
119-
>- Kotlin
120-
>
121-
>1.
122-
```java
123-
package com.dynamsoft.scansinglebarcode;
124-
import android.os.Bundle;
125-
import android.widget.TextView;
126-
import com.dynamsoft.dbrbundle.ui.BarcodeScanResult;
127-
import com.dynamsoft.dbrbundle.ui.BarcodeScannerActivity;
128-
import com.dynamsoft.dbrbundle.ui.BarcodeScannerConfig;
129-
import com.dynamsoft.core.basic_structures.DSRect;
130-
import androidx.activity.result.ActivityResultLauncher;
131-
import androidx.annotation.Nullable;
132-
import androidx.appcompat.app.AppCompatActivity;
133-
public class MainActivity extends AppCompatActivity {
134-
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
135-
@Override
136-
protected void onCreate(@Nullable Bundle savedInstanceState) {
137-
super.onCreate(savedInstanceState);
138-
setContentView(R.layout.activity_main);
139-
TextView textView = findViewById(R.id.tv_result);
140-
BarcodeScannerConfig config = new BarcodeScannerConfig();
141-
config.setLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
142-
}
117+
<div class="sample-code-prefix"></div>
118+
>- Java
119+
>- Kotlin
120+
>
121+
>1.
122+
```java
123+
package com.dynamsoft.scansinglebarcode;
124+
import android.os.Bundle;
125+
import android.widget.TextView;
126+
import com.dynamsoft.dbrbundle.ui.BarcodeScanResult;
127+
import com.dynamsoft.dbrbundle.ui.BarcodeScannerActivity;
128+
import com.dynamsoft.dbrbundle.ui.BarcodeScannerConfig;
129+
import com.dynamsoft.core.basic_structures.DSRect;
130+
import androidx.activity.result.ActivityResultLauncher;
131+
import androidx.annotation.Nullable;
132+
import androidx.appcompat.app.AppCompatActivity;
133+
public class MainActivity extends AppCompatActivity {
134+
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
135+
@Override
136+
protected void onCreate(@Nullable Bundle savedInstanceState) {
137+
super.onCreate(savedInstanceState);
138+
setContentView(R.layout.activity_main);
139+
TextView textView = findViewById(R.id.tv_result);
140+
BarcodeScannerConfig config = new BarcodeScannerConfig();
141+
config.setLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
143142
}
144-
```
145-
2.
146-
```kotlin
147-
package com.dynamsoft.scansinglebarcode
148-
import android.os.Bundle
149-
import android.view.View
150-
import android.widget.TextView
151-
import androidx.activity.result.ActivityResultLauncher
152-
import androidx.appcompat.app.AppCompatActivity
153-
import com.dynamsoft.dbrbundle.ui.BarcodeScanResult
154-
import com.dynamsoft.dbrbundle.ui.BarcodeScannerActivity
155-
import com.dynamsoft.dbrbundle.ui.BarcodeScannerConfig
156-
import com.dynamsoft.core.basic_structures.DSRect
157-
class MainActivity : AppCompatActivity() {
158-
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
159-
override fun onCreate(savedInstanceState: Bundle?) {
160-
super.onCreate(savedInstanceState)
161-
setContentView(R.layout.activity_main)
162-
val textView = findViewById<TextView>(R.id.tv_result)
163-
val config = BarcodeScannerConfig().apply {
164-
license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
165-
}
166-
}
143+
}
144+
```
145+
2.
146+
```kotlin
147+
package com.dynamsoft.scansinglebarcode
148+
import android.os.Bundle
149+
import android.view.View
150+
import android.widget.TextView
151+
import androidx.activity.result.ActivityResultLauncher
152+
import androidx.appcompat.app.AppCompatActivity
153+
import com.dynamsoft.dbrbundle.ui.BarcodeScanResult
154+
import com.dynamsoft.dbrbundle.ui.BarcodeScannerActivity
155+
import com.dynamsoft.dbrbundle.ui.BarcodeScannerConfig
156+
import com.dynamsoft.core.basic_structures.DSRect
157+
class MainActivity : AppCompatActivity() {
158+
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
159+
override fun onCreate(savedInstanceState: Bundle?) {
160+
super.onCreate(savedInstanceState)
161+
setContentView(R.layout.activity_main)
162+
val textView = findViewById<TextView>(R.id.tv_result)
163+
val config = BarcodeScannerConfig().apply {
164+
license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"
165+
}
167166
}
168-
```
167+
}
168+
```
169169

170-
>Note:
171-
>
172-
>- The license string here grants a time-limited free trial which requires network connection to work.
173-
>- You can request a 30-day trial license via the [Request a Trial License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=guide&package=ios){:target="_blank"} link.
174-
>- If you download the <a href="https://www.dynamsoft.com/barcode-reader/downloads/?utm_source=docs#mobile" target="_blank">Installation Package</a>, it comes with a 30-day trial license by default.
170+
>Note:
171+
>
172+
>- The license string here grants a time-limited free trial which requires network connection to work.
173+
>- You can request a 30-day trial license via the [Request a Trial License](https://www.dynamsoft.com/customer/license/trialLicense?product=dbr&utm_source=guide&package=ios){:target="_blank"} link.
174+
>- If you download the <a href="https://www.dynamsoft.com/barcode-reader/downloads/?utm_source=docs#mobile" target="_blank">Installation Package</a>, it comes with a 30-day trial license by default.
175175
176176
## Step 4: Implementing the Barcode Scanner
177177

@@ -181,125 +181,152 @@ Each result comes with a `resultStatus` that can be one of *RS_FINISHED*, *RS_CA
181181

182182
Once a barcode is found, the content of the barcode (text result as well as barcode format) is outputted to the `TextView` object set up earlier. Continuing the code from step 3:
183183

184-
<div class="sample-code-prefix"></div>
185-
>- Java
186-
>- Kotlin
187-
>
188-
>1.
189-
```java
190-
public class MainActivity extends AppCompatActivity {
191-
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
192-
@Override
193-
protected void onCreate(@Nullable Bundle savedInstanceState) {
194-
/* <CONTINUATION OF THE CODE FROM STEP 3> */
195-
launcher = registerForActivityResult(new BarcodeScannerActivity.ResultContract(), result -> {
196-
if (result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_FINISHED && result.getBarcodes() != null) {
197-
String content = "Result: format: " + result.getBarcodes()[0].getFormatString() + "\n" + "content: "
198-
+ result.getBarcodes()[0].getText();
199-
textView.setText(content);
200-
} else if(result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_CANCELED ){
201-
textView.setText("Scan canceled.");
202-
}
203-
if (result.getErrorString() != null && !result.getErrorString().isEmpty()) {
204-
textView.setText(result.getErrorString());
205-
}
206-
});
207-
findViewById(R.id.btn_navigate).setOnClickListener(v -> launcher.launch(config));
208-
}
184+
<div class="sample-code-prefix"></div>
185+
>- Java
186+
>- Kotlin
187+
>
188+
>1.
189+
```java
190+
public class MainActivity extends AppCompatActivity {
191+
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
192+
@Override
193+
protected void onCreate(@Nullable Bundle savedInstanceState) {
194+
/* <CONTINUATION OF THE CODE FROM STEP 3> */
195+
launcher = registerForActivityResult(new BarcodeScannerActivity.ResultContract(), result -> {
196+
if (result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_FINISHED && result.getBarcodes() != null) {
197+
String content = "Result: format: " + result.getBarcodes()[0].getFormatString() + "\n" + "content: "
198+
+ result.getBarcodes()[0].getText();
199+
textView.setText(content);
200+
} else if(result.getResultStatus() == BarcodeScanResult.EnumResultStatus.RS_CANCELED ){
201+
textView.setText("Scan canceled.");
202+
}
203+
if (result.getErrorString() != null && !result.getErrorString().isEmpty()) {
204+
textView.setText(result.getErrorString());
205+
}
206+
});
207+
findViewById(R.id.btn_navigate).setOnClickListener(v -> launcher.launch(config));
209208
}
210-
```
211-
2.
212-
```kotlin
213-
class MainActivity : AppCompatActivity() {
214-
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
215-
override fun onCreate(savedInstanceState: Bundle?) {
216-
launcher = registerForActivityResult(BarcodeScannerActivity.ResultContract()) { result ->
217-
if (result.resultStatus == BarcodeScanResult.EnumResultStatus.RS_FINISHED && result.barcodes != null) {
218-
val content = """
219-
Result: format: ${result.barcodes[0].formatString}
220-
content: ${result.barcodes[0].text}
221-
""".trimIndent()
222-
textView.text = content
223-
} else if (result.resultStatus == BarcodeScanResult.EnumResultStatus.RS_CANCELED) {
224-
textView.text = "Scan canceled."
225-
}
226-
if (result.errorString != null && result.errorString.isNotEmpty()) {
227-
textView.text = result.errorString
228-
}
209+
}
210+
```
211+
2.
212+
```kotlin
213+
class MainActivity : AppCompatActivity() {
214+
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
215+
override fun onCreate(savedInstanceState: Bundle?) {
216+
launcher = registerForActivityResult(BarcodeScannerActivity.ResultContract()) { result ->
217+
if (result.resultStatus == BarcodeScanResult.EnumResultStatus.RS_FINISHED && result.barcodes != null) {
218+
val content = """
219+
Result: format: ${result.barcodes[0].formatString}
220+
content: ${result.barcodes[0].text}
221+
""".trimIndent()
222+
textView.text = content
223+
} else if (result.resultStatus == BarcodeScanResult.EnumResultStatus.RS_CANCELED) {
224+
textView.text = "Scan canceled."
229225
}
230-
findViewById<View>(R.id.btn_navigate).setOnClickListener {
231-
launcher.launch(config)
226+
if (result.errorString != null && result.errorString.isNotEmpty()) {
227+
textView.text = result.errorString
232228
}
233-
}
229+
}
230+
findViewById<View>(R.id.btn_navigate).setOnClickListener {
231+
launcher.launch(config)
232+
}
234233
}
235-
```
236-
237-
## Step 5: Configure the Barcode Scanner (optional)
234+
}
235+
```
236+
237+
## Step 5: Configure your layout file
238+
239+
Open your **activity_main.xml** and replace it with the following code:
240+
241+
```xml
242+
<?xml version="1.0" encoding="utf-8"?>
243+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
244+
android:layout_width="match_parent"
245+
android:layout_height="match_parent"
246+
android:gravity="center"
247+
android:orientation="vertical">
248+
249+
<Button
250+
android:id="@+id/btn_navigate"
251+
android:layout_width="wrap_content"
252+
android:layout_height="wrap_content"
253+
android:text="Start Scanning" />
254+
255+
<TextView
256+
android:id="@+id/tv_result"
257+
android:layout_width="wrap_content"
258+
android:layout_height="wrap_content"
259+
android:textSize="20sp"
260+
android:text=""/>
261+
</LinearLayout>
262+
```
263+
264+
## Step 6: Configure the Barcode Scanner (optional)
238265

239266
This next step, although optional, is highly recommended to help achieve a more smooth-looking and intuitive UI. In this setup we will configure the visibility of the torch button as well as the close button. In addition, a scan region will be defined that will limit the reading region of the Barcode Scanner to the specified dimensions. To do this, we are going back to the `BarcodeScannerConfig` object we used to define the license, and will make use of some of the other parameters available in the `BarcodeScannerConfig` class.
240267

241-
<div class="sample-code-prefix"></div>
242-
>- Java
243-
>- Kotlin
244-
>
245-
>1.
246-
```java
247-
public class MainActivity extends AppCompatActivity {
248-
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
249-
@Override
250-
protected void onCreate(@Nullable Bundle savedInstanceState) {
251-
/* CONTINUATION OF THE CODE FROM STEP 3 */
252-
BarcodeScannerConfig config = new BarcodeScannerConfig();
253-
config.setLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
268+
<div class="sample-code-prefix"></div>
269+
>- Java
270+
>- Kotlin
271+
>
272+
>1.
273+
```java
274+
public class MainActivity extends AppCompatActivity {
275+
private ActivityResultLauncher<BarcodeScannerConfig> launcher;
276+
@Override
277+
protected void onCreate(@Nullable Bundle savedInstanceState) {
278+
/* CONTINUATION OF THE CODE FROM STEP 3 */
279+
BarcodeScannerConfig config = new BarcodeScannerConfig();
280+
config.setLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
281+
// You can use the following code to specify the barcode format. If you are using a template file, the "BarcodeFormat" can also be specified via the template file.
282+
config.setBarcodeFormats(EnumBarcodeFormat.BF_ONED | EnumBarcodeFormat.BF_QR_CODE);
283+
// If you have a customized template file, please put it under "src\main\assets\Templates\" and call the following code.
284+
config.setTemplateFilePath("ReadSingleBarcode.json");
285+
// The following settings will display a scan region on the view. Only the barcode in the scan region can be decoded.
286+
config.setScanRegion(new DSRect(0.15f, 0.25f, 0.85f, 0.65f, true));
287+
// The following code enables the beep sound when a barcode is scanned.
288+
config.setBeepEnabled = true
289+
// The following code controls whether to display a torch button.
290+
config.setTorchButtonVisible(true);
291+
// The following code controls whether to display a close button.
292+
config.setCloseButtonVisible(true);
293+
// The following code controls whether to display the scan laser.
294+
config.setScanLaserVisible(true);
295+
/* CONTINUATION OF THE CODE FROM STEP 4 */
296+
}
297+
}
298+
```
299+
2.
300+
```kotlin
301+
class MainActivity : AppCompatActivity() {
302+
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
303+
override fun onCreate(savedInstanceState: Bundle?) {
304+
/* CONTINUATION OF THE CODE FROM STEP 3 */
305+
val config = BarcodeScannerConfig().apply {
306+
license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
254307
// You can use the following code to specify the barcode format. If you are using a template file, the "BarcodeFormat" can also be specified via the template file.
255-
config.setBarcodeFormats(EnumBarcodeFormat.BF_ONED | EnumBarcodeFormat.BF_QR_CODE);
308+
barcodeFormats = EnumBarcodeFormat.BF_ONED or EnumBarcodeFormat.BF_QR_CODE
256309
// If you have a customized template file, please put it under "src\main\assets\Templates\" and call the following code.
257-
config.setTemplateFilePath("ReadSingleBarcode.json");
310+
templateFilePath = "ReadSingleBarcode.json"
258311
// The following settings will display a scan region on the view. Only the barcode in the scan region can be decoded.
259-
config.setScanRegion(new DSRect(0.15f, 0.25f, 0.85f, 0.65f, true));
260-
// The following code enables the beep sound when a barcode is scanned.
261-
config.setBeepEnabled = true
262-
// The following code controls whether to display a torch button.
263-
config.setTorchButtonVisible(true);
264-
// The following code controls whether to display a close button.
265-
config.setCloseButtonVisible(true);
266-
// The following code controls whether to display the scan laser.
267-
config.setScanLaserVisible(true);
268-
/* CONTINUATION OF THE CODE FROM STEP 4 */
269-
}
270-
}
271-
```
272-
2.
273-
```kotlin
274-
class MainActivity : AppCompatActivity() {
275-
private lateinit var launcher: ActivityResultLauncher<BarcodeScannerConfig>
276-
override fun onCreate(savedInstanceState: Bundle?) {
277-
/* CONTINUATION OF THE CODE FROM STEP 3 */
278-
val config = BarcodeScannerConfig().apply {
279-
license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9";
280-
// You can use the following code to specify the barcode format. If you are using a template file, the "BarcodeFormat" can also be specified via the template file.
281-
barcodeFormats = EnumBarcodeFormat.BF_ONED or EnumBarcodeFormat.BF_QR_CODE
282-
// If you have a customized template file, please put it under "src\main\assets\Templates\" and call the following code.
283-
templateFilePath = "ReadSingleBarcode.json"
284-
// The following settings will display a scan region on the view. Only the barcode in the scan region can be decoded.
285-
scanRegion = DSRect(0.15f, 0.3f, 0.85f, 0.7f, true)
286-
// Add the following line to disable the beep sound.
287-
isBeepEnabled = false
288-
// Add the following line if you don't want to display the torch button.
289-
isTorchButtonVisible = false
290-
// Add the following line if you don't want to display the close button.
291-
isCloseButtonVisible = false
292-
// Add the following line if you want to hide the scan laser.
293-
isScanLaserVisible = false
294-
// Add the following line if you want the camera to auto-zoom when the barcode is far away.
295-
isAutoZoomEnabled = true
296-
}
297-
/* CONTINUATION OF THE CODE FROM STEP 4 */
298-
}
312+
scanRegion = DSRect(0.15f, 0.3f, 0.85f, 0.7f, true)
313+
// Add the following line to disable the beep sound.
314+
isBeepEnabled = false
315+
// Add the following line if you don't want to display the torch button.
316+
isTorchButtonVisible = false
317+
// Add the following line if you don't want to display the close button.
318+
isCloseButtonVisible = false
319+
// Add the following line if you want to hide the scan laser.
320+
isScanLaserVisible = false
321+
// Add the following line if you want the camera to auto-zoom when the barcode is far away.
322+
isAutoZoomEnabled = true
323+
}
324+
/* CONTINUATION OF THE CODE FROM STEP 4 */
299325
}
300-
```
326+
}
327+
```
301328

302-
## Step 6: Run the Project
329+
## Step 7: Run the Project
303330

304331
Now that the code has been written and the project complete, it's time to run the project. During setup, all of the gradle settings should have already been configured for you, so pretty much all you need to do now is to connect a physical Android device, select the proper configuration, and click Run.
305332

0 commit comments

Comments
 (0)