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

為{幸葍}努か 提交于 2020-11-30 00:07:55

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/37613228/how-to-generate-ean13-barcode-that-can-satisfy-checksum-digit-check

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!