Skip to content

Commit f731593

Browse files
committed
add ikea vendor specific codes, restrict itf to itf14
1 parent 940987c commit f731593

File tree

13 files changed

+133
-16
lines changed

13 files changed

+133
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [0.10.1]
5+
6+
## Added
7+
- Experimental support for IKEA vendor specific codes
8+
9+
## Changed
10+
- ITF code detection is now restricted to ITF14
11+
412
## [0.10.0]
513

614
### Added

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ allprojects {
2424
}
2525

2626
project.ext {
27-
sdkVersion='0.10.0'
27+
sdkVersion='0.10.1'
2828
versionCode=1
2929

3030
compileSdkVersion=28

core/src/main/java/io/snabble/sdk/BarcodeFormat.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public enum BarcodeFormat {
55
CODE_128,
66
EAN_8,
77
EAN_13,
8-
ITF,
8+
ITF_14,
99
QR_CODE,
1010
DATA_MATRIX;
1111

@@ -20,7 +20,7 @@ public static BarcodeFormat parse(String str) {
2020
case "ean13":
2121
return EAN_13;
2222
case "itf14":
23-
return ITF;
23+
return ITF_14;
2424
case "datamatrix":
2525
return DATA_MATRIX;
2626
case "qr":

core/src/main/java/io/snabble/sdk/Project.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.CopyOnWriteArrayList;
2222

2323
import io.snabble.sdk.encodedcodes.EncodedCodesOptions;
24+
import io.snabble.sdk.utils.IntRange;
2425
import io.snabble.sdk.utils.JsonUtils;
2526
import io.snabble.sdk.utils.SimpleActivityLifecycleCallbacks;
2627
import okhttp3.OkHttpClient;
@@ -55,6 +56,7 @@ public class Project {
5556
private boolean verifyInternalEanChecksum;
5657
private BarcodeFormat[] supportedBarcodeFormats;
5758
private Shop checkedInShop;
59+
private Map<BarcodeFormat, IntRange> barcodeFormatRanges;
5860

5961
private Map<String, String> urls;
6062

@@ -160,6 +162,12 @@ void parse(JsonObject jsonObject) {
160162
formats.add(BarcodeFormat.CODE_128);
161163
}
162164

165+
barcodeFormatRanges = new HashMap<>();
166+
// TODO parse from metadata
167+
if (id.contains("ikea")) {
168+
barcodeFormatRanges.put(BarcodeFormat.ITF_14, new IntRange(0, 8));
169+
}
170+
163171
supportedBarcodeFormats = formats.toArray(new BarcodeFormat[formats.size()]);
164172

165173
if (jsonObject.has("shops")) {
@@ -334,6 +342,14 @@ public Checkout getCheckout() {
334342
return checkout;
335343
}
336344

345+
public IntRange getRangeForBarcodeFormat(BarcodeFormat barcodeFormat) {
346+
if (barcodeFormatRanges != null) {
347+
return barcodeFormatRanges.get(barcodeFormat);
348+
}
349+
350+
return null;
351+
}
352+
337353
Events getEvents() {
338354
return events;
339355
}

core/src/main/java/io/snabble/sdk/codes/EAN13.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class EAN13 extends ScannableCode implements Serializable {
2323
private boolean useGermanPrintPrefix;
2424

2525
EAN13(Project project, String code) {
26-
super(code);
26+
super(project, code);
2727

2828
if (!isEan13(code)) {
2929
throw new IllegalArgumentException("Not a valid EAN13 code");

core/src/main/java/io/snabble/sdk/codes/EAN14.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package io.snabble.sdk.codes;
22

3+
import io.snabble.sdk.Project;
4+
35
public class EAN14 extends ScannableCode {
4-
EAN14(String code) {
5-
super(code);
6+
EAN14(Project project, String code) {
7+
super(project, code);
68

79
if (code.length() == 16) {
810
this.code = code.substring(2, code.length());

core/src/main/java/io/snabble/sdk/codes/EdekaProductCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class EdekaProductCode extends ScannableCode {
99
private String overriddenCode;
1010

1111
EdekaProductCode(Project project, String code) {
12-
super(code);
12+
super(project, code);
1313

1414
lookupCode = code.substring(2, 15);
1515
price = Integer.parseInt(code.substring(15, 21));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package io.snabble.sdk.codes;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import io.snabble.sdk.Project;
5+
6+
public class IKEADataMatrixCode extends ScannableCode {
7+
private String lookupCode;
8+
private int price;
9+
10+
IKEADataMatrixCode(Project project, String code) {
11+
super(project, code);
12+
13+
String[] split = code.split("\u001D");
14+
String codePart = split[3];
15+
String pricePart = split[4];
16+
17+
lookupCode = codePart.substring(3, codePart.length());
18+
price = Integer.parseInt(pricePart.substring(pricePart.length() - 6, pricePart.length() - 1));
19+
price *= 100; // price is in EUR
20+
}
21+
22+
@Override
23+
public String getLookupCode() {
24+
return lookupCode;
25+
}
26+
27+
@Override
28+
public int getEmbeddedData() {
29+
return price;
30+
}
31+
32+
@Override
33+
public boolean hasPriceData() {
34+
return true;
35+
}
36+
37+
@Override
38+
public boolean hasEmbeddedData() {
39+
return true;
40+
}
41+
42+
@Override
43+
public boolean isEmbeddedDataOk() {
44+
return true;
45+
}
46+
47+
public static boolean isIKEADataMatrixCode(Project project, String code) {
48+
return project.getId().contains("ikea")
49+
&& StringUtils.countMatches(code, '\u001D') == 4;
50+
}
51+
}

core/src/main/java/io/snabble/sdk/codes/ScannableCode.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import io.snabble.sdk.Project;
66

77
public class ScannableCode implements Serializable {
8+
protected final Project project;
89
protected String code;
910

10-
ScannableCode(String code) {
11+
ScannableCode(Project project, String code) {
12+
this.project = project;
1113
this.code = code;
1214
}
1315

@@ -51,11 +53,13 @@ public static ScannableCode parse(Project project, String code) {
5153
if (EAN13.isEan13(code)) {
5254
return new EAN13(project, code);
5355
} else if (EAN14.isEan14(code)) {
54-
return new EAN14(code);
56+
return new EAN14(project, code);
5557
} else if (EdekaProductCode.isEdekaProductCode(project, code)) {
5658
return new EdekaProductCode(project, code);
57-
} else {
58-
return new ScannableCode(code);
59+
} else if (IKEADataMatrixCode.isIKEADataMatrixCode(project, code)) {
60+
return new IKEADataMatrixCode(project, code);
61+
} else {
62+
return new ScannableCode(project, code);
5963
}
6064
}
6165
}

ui/src/main/java/io/snabble/sdk/ui/scanner/BarcodeScannerView.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,13 @@ public void run() {
663663
@Override
664664
public void run() {
665665
if (decodeEnabled && callback != null && isAttachedToWindow && !isPaused) {
666+
// ZXing decodes all ITF_14 lengths, but we only care about ITF14.
667+
if (finalResult.getBarcodeFormat() == com.google.zxing.BarcodeFormat.ITF) {
668+
if (finalResult.getText().length() != 14) {
669+
return;
670+
}
671+
}
672+
666673
Barcode barcode = new Barcode(
667674
ZXingHelper.fromZXingFormat(finalResult.getBarcodeFormat()),
668675
finalResult.getText(),

0 commit comments

Comments
 (0)