How to generate EAN13 barcode that can satisfy checksum digit check?

后端 未结 2 600
慢半拍i
慢半拍i 2021-01-26 10:14

I am using zxing to generate barcodes. I want to store incremental number in it and I want to avoid checksum errors. How can I avoid it? What\'s the correct approach?

相关标签:
2条回答
  • 2021-01-26 11:03

    you can use the algorithm on http://en.wikipedia.org/wiki/EAN-13 to base your code off of.

    This should give you a good approach to creating it and using it at the same time

    http://www.codeproject.com/Articles/156402/Android-Generating-an-EAN-Barcode

    I would of made a comment but I dont have enough rep.

    0 讨论(0)
  • 2021-01-26 11:09

    I wrote generation method using kotlin, may be it will be helpful for some1

    fun generateBarcode(): String {
        var result = ""
        for (i in 0..11) {
            result += (0..9).random()
        }
    
        return result+getCheckSum(result)
    }
    
    fun getCheckSum(code:String): String {
        var odd = 0
        var even = 0
        for (i in 0..code.length-1) {
            val index = i+1
            if (index.isOdd())
                odd+=code[i].toString().toInt()
            else
                even+=code[i].toString().toInt()
        }
        return ((10-((odd+even*3)%10))%10).toString()
    }
    

    just call generateBarcode() to get EAN13 barcode as String

    0 讨论(0)
提交回复
热议问题