Skip to content

Commit 2308deb

Browse files
Only pass encoding hint in automatic if UTF-8
If zxing is not explicitly told a barcode is UTF-8, it may render it incorrectly. Which caused #2555. However, when an encode hint is set, it will cause zxing to set an ECI hint inside the barcode, which some scanners may trip over and cause scanning failures, leading to #2921. This change only passes the encoding in automatic mode if zxing explicitly guesses it to be UTF-8, and otherwise doesn't pass anything, to keep the ECI empty. This might need to be expanded for other types like SJIS, but as nobody ever reported such a bug let's assume it's not necessary for now.
1 parent 8252f52 commit 2308deb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

app/src/main/java/protect/card_locker/BarcodeImageWriterTask.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.lang.ref.WeakReference;
2323
import java.nio.charset.Charset;
2424
import java.util.Map;
25+
import java.util.Objects;
2526

2627
import protect.card_locker.async.CompatCallable;
2728

@@ -190,7 +191,16 @@ private Bitmap generate() {
190191
} else {
191192
String guessedEncoding = StringUtils.guessEncoding(cardId.getBytes(), new ArrayMap<>());
192193
Log.d(TAG, "Guessed encoding: " + guessedEncoding);
193-
encodeHints.put(EncodeHintType.CHARACTER_SET, Charset.forName(guessedEncoding));
194+
195+
// We don't want to pass the gussed encoding as an encoding hint unless it is UTF-8 as
196+
// zxing is likely to add the mentioned encoding hint as ECI inside the barcode.
197+
//
198+
// Due to many barcode scanners in the wild being badly coded they may trip over ECI
199+
// info existing and fail to scan, such as in https://github.com/CatimaLoyalty/Android/issues/2921
200+
if (Objects.equals(guessedEncoding, "UTF8")) {
201+
Log.d(TAG, "Guessed encoding is UTF8, so passing as encoding hint");
202+
encodeHints.put(EncodeHintType.CHARACTER_SET, Charset.forName(guessedEncoding));
203+
}
194204
}
195205

196206
BitMatrix bitMatrix;

0 commit comments

Comments
 (0)