Skip to content

Commit 63f1606

Browse files
committed
Insert Example.java into README.md
1 parent 5fbd08e commit 63f1606

File tree

6 files changed

+124
-19
lines changed

6 files changed

+124
-19
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ display-updates:
3232
update:
3333
mvn versions:update-properties
3434

35+
.PHONY: insert-example
36+
insert-example:
37+
./scripts/insert-example.bash
38+
3539
.PHONY: after-gen
36-
after-gen: fix format
40+
after-gen: fix format insert-example

README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,26 @@ Then manually install the following JARs:
9191
Please follow the [installation](#installation) instruction and execute the following Java code:
9292

9393
```java
94+
package com.aspose.barcode.cloud.examples;
95+
96+
import com.aspose.barcode.cloud.ApiClient;
97+
import com.aspose.barcode.cloud.ApiException;
9498
import com.aspose.barcode.cloud.api.BarcodeApi;
9599
import com.aspose.barcode.cloud.model.BarcodeResponseList;
100+
import com.aspose.barcode.cloud.model.DecodeBarcodeType;
96101
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
97-
import com.aspose.barcode.cloud.model.PresetType;
98102
import com.aspose.barcode.cloud.requests.GetBarcodeGenerateRequest;
99-
import com.aspose.barcode.cloud.requests.PostBarcodeRecognizeFromUrlOrContentRequest;
103+
import com.aspose.barcode.cloud.requests.ScanBarcodeRequest;
100104

101105
import java.io.File;
106+
import java.util.Collections;
102107

103-
public class BarcodeApiExample {
108+
public class Example {
104109
public static void main(String[] args) {
105-
ApiClient client = new ApiClient(
106-
"Client Id from https://dashboard.aspose.cloud/applications",
107-
"Client Secret from https://dashboard.aspose.cloud/applications"
108-
);
110+
ApiClient client =
111+
new ApiClient(
112+
"Client Id from https://dashboard.aspose.cloud/applications",
113+
"Client Secret from https://dashboard.aspose.cloud/applications");
109114
client.setReadTimeout(5 * 60 * 1000);
110115

111116
BarcodeApi api = new BarcodeApi(client);
@@ -116,7 +121,7 @@ public class BarcodeApiExample {
116121
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
117122

118123
System.out.println("Recognizing barcode on image...");
119-
BarcodeResponseList recognized = recognizeBarcode(api, barcodeImage);
124+
BarcodeResponseList recognized = scanBarcode(api, barcodeImage);
120125
System.out.print("Barcode on image:");
121126
System.out.println(recognized.toString());
122127
} catch (ApiException e) {
@@ -130,18 +135,19 @@ public class BarcodeApiExample {
130135
String text = "Aspose.BarCode for Cloud Sample";
131136
GetBarcodeGenerateRequest request = new GetBarcodeGenerateRequest(type, text);
132137
request.textLocation = "None";
133-
138+
134139
return api.getBarcodeGenerate(request);
135140
}
136141

137-
private static BarcodeResponseList recognizeBarcode(BarcodeApi api, File barcodeImage) throws ApiException {
138-
PostBarcodeRecognizeFromUrlOrContentRequest recognizeRequest = new PostBarcodeRecognizeFromUrlOrContentRequest();
139-
recognizeRequest.image = barcodeImage;
140-
recognizeRequest.preset = PresetType.HIGHPERFORMANCE.toString();
142+
private static BarcodeResponseList scanBarcode(BarcodeApi api, File barcodeImage)
143+
throws ApiException {
144+
ScanBarcodeRequest request = new ScanBarcodeRequest(barcodeImage);
145+
request.decodeTypes = Collections.singletonList(DecodeBarcodeType.QR);
141146

142-
return api.postBarcodeRecognizeFromUrlOrContent(recognizeRequest);
147+
return api.scanBarcode(request);
143148
}
144149
}
150+
145151
```
146152

147153
## Licensing
@@ -281,3 +287,4 @@ Authentication schemes defined for the API:
281287
## Recommendation
282288

283289
It's recommended to create an instance of `ApiClient` per thread in a multithreaded environment to avoid any potential issues.
290+

scripts/extract_java_from_README.sh

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

scripts/insert-example.bash

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
set -euo pipefail
3+
4+
cd "$( dirname "${BASH_SOURCE[0]}" )/.."
5+
6+
python "./scripts/insert-example.py" "README.template" > "README.md"
7+
8+
rm "README.template"

scripts/insert-example.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import argparse
2+
import os
3+
import re
4+
import typing
5+
6+
REPLACE_RE = re.compile(r'^%insert (?P<file_name>[^%]+)%$', re.MULTILINE)
7+
8+
9+
def read_text(filename: str) -> str:
10+
with open(filename, "rt") as rf:
11+
text = rf.read()
12+
return text
13+
14+
15+
def main(template: typing.TextIO) -> None:
16+
content: str = template.read()
17+
18+
def sub_match(match):
19+
file_name = match.group('file_name')
20+
return read_text(file_name)
21+
22+
text = REPLACE_RE.sub(sub_match, content)
23+
print(text)
24+
25+
26+
def parse_args() -> typing.Dict[str, typing.Any]:
27+
parser = argparse.ArgumentParser()
28+
parser.add_argument("template", type=argparse.FileType("rt"), help="README.template")
29+
kwargs = vars(parser.parse_args())
30+
return kwargs
31+
32+
33+
if __name__ == "__main__":
34+
main(**parse_args())
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.aspose.barcode.cloud.examples;
2+
3+
import com.aspose.barcode.cloud.ApiClient;
4+
import com.aspose.barcode.cloud.ApiException;
5+
import com.aspose.barcode.cloud.api.BarcodeApi;
6+
import com.aspose.barcode.cloud.model.BarcodeResponseList;
7+
import com.aspose.barcode.cloud.model.DecodeBarcodeType;
8+
import com.aspose.barcode.cloud.model.EncodeBarcodeType;
9+
import com.aspose.barcode.cloud.requests.GetBarcodeGenerateRequest;
10+
import com.aspose.barcode.cloud.requests.ScanBarcodeRequest;
11+
12+
import java.io.File;
13+
import java.util.Collections;
14+
15+
public class Example {
16+
public static void main(String[] args) {
17+
ApiClient client =
18+
new ApiClient(
19+
"Client Id from https://dashboard.aspose.cloud/applications",
20+
"Client Secret from https://dashboard.aspose.cloud/applications");
21+
client.setReadTimeout(5 * 60 * 1000);
22+
23+
BarcodeApi api = new BarcodeApi(client);
24+
25+
try {
26+
System.out.println("Generating barcode...");
27+
File barcodeImage = generateBarcode(api);
28+
System.out.println("Barcode image saved to file " + barcodeImage.getAbsolutePath());
29+
30+
System.out.println("Recognizing barcode on image...");
31+
BarcodeResponseList recognized = scanBarcode(api, barcodeImage);
32+
System.out.print("Barcode on image:");
33+
System.out.println(recognized.toString());
34+
} catch (ApiException e) {
35+
System.err.println("Error");
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
private static File generateBarcode(BarcodeApi api) throws ApiException {
41+
String type = EncodeBarcodeType.QR.toString();
42+
String text = "Aspose.BarCode for Cloud Sample";
43+
GetBarcodeGenerateRequest request = new GetBarcodeGenerateRequest(type, text);
44+
request.textLocation = "None";
45+
46+
return api.getBarcodeGenerate(request);
47+
}
48+
49+
private static BarcodeResponseList scanBarcode(BarcodeApi api, File barcodeImage)
50+
throws ApiException {
51+
ScanBarcodeRequest request = new ScanBarcodeRequest(barcodeImage);
52+
request.decodeTypes = Collections.singletonList(DecodeBarcodeType.QR);
53+
54+
return api.scanBarcode(request);
55+
}
56+
}

0 commit comments

Comments
 (0)