简体   繁体   English

如何生成可以满足校验和数字校验的EAN13条码?

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

I am using zxing to generate barcodes. 我正在使用zxing生成条形码。 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? 正确的方法是什么?

you can use the algorithm on http://en.wikipedia.org/wiki/EAN-13 to base your code off of. 您可以在http://en.wikipedia.org/wiki/EAN-13上使用该算法作为基础代码。

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 http://www.codeproject.com/Articles/156402/Android-Generating-an-EAN-Barcode

I would of made a comment but I dont have enough rep. 我会发表评论,但我没有足够的代表。

I wrote generation method using kotlin, may be it will be helpful for some1 我使用kotlin编写了生成方法,可能会对某人有所帮助

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 只需调用generateBarcode()即可将EAN13条码获取为String

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM