Add support for barcode type EAN8. Closes #205 (#209)

This commit is contained in:
Simon Tenbeitel 2018-08-17 15:24:34 +02:00 committed by ligi
parent bfcbe4446e
commit 9d2300bd0a
3 changed files with 17 additions and 4 deletions

View file

@ -8,6 +8,7 @@ enum class PassBarCodeFormat {
CODE_39,
CODE_128,
DATA_MATRIX,
EAN_8,
EAN_13,
ITF,
PDF_417,
@ -23,6 +24,7 @@ enum class PassBarCodeFormat {
CODE_39 -> BarcodeFormat.CODE_39
CODE_128 -> BarcodeFormat.CODE_128
DATA_MATRIX -> BarcodeFormat.DATA_MATRIX
EAN_8 -> BarcodeFormat.EAN_8
EAN_13 -> BarcodeFormat.EAN_13
ITF -> BarcodeFormat.ITF
PDF_417 -> BarcodeFormat.PDF_417

View file

@ -9,8 +9,7 @@ import kotlinx.android.synthetic.main.barcode_edit.view.*
import org.ligi.kaxt.doAfterEdit
import org.ligi.passandroid.model.pass.BarCode
import org.ligi.passandroid.model.pass.PassBarCodeFormat
import org.ligi.passandroid.model.pass.PassBarCodeFormat.EAN_13
import org.ligi.passandroid.model.pass.PassBarCodeFormat.QR_CODE
import org.ligi.passandroid.model.pass.PassBarCodeFormat.*
import org.ligi.passandroid.ui.BarcodeUIController
import org.ligi.passandroid.ui.PassViewHelper
import java.util.*
@ -56,6 +55,7 @@ class BarcodeEditController(val rootView: View, internal val context: AppCompatA
rootView.randomButton.setOnClickListener({
rootView.messageInput.setText(when (barcodeFormat) {
EAN_8 -> getRandomEAN8()
EAN_13 -> getRandomEAN13()
PassBarCodeFormat.ITF -> getRandomITF()
else -> UUID.randomUUID().toString().toUpperCase()

View file

@ -1,14 +1,25 @@
package org.ligi.passandroid.ui.edit
import com.google.zxing.oned.EAN13Writer
import com.google.zxing.oned.EAN8Writer
import com.google.zxing.oned.UPCEANWriter
fun getRandomEAN13(): String {
val randomString = (0..11).map { (Math.random() * 9).toInt() }.joinToString(separator = "")
return (0..9).map { randomString + it }.first(::isValidEAN13)
}
fun isValidEAN13(payload: String) = try {
EAN13Writer().encode(payload)
fun isValidEAN13(payload: String) = isValidEAN(payload, EAN13Writer())
fun getRandomEAN8(): String {
val randomString = (0..6).map { (Math.random() * 9).toInt() }.joinToString(separator = "")
return (0..9).map { randomString + it }.first(::isValidEAN8)
}
fun isValidEAN8(payload: String) = isValidEAN(payload, EAN8Writer())
fun isValidEAN(payload: String, writer: UPCEANWriter) = try {
writer.encode(payload)
true
} catch(e: IllegalArgumentException) {
false