Miki
Miki

Reputation: 2517

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

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?

Upvotes: 0

Views: 5305

Answers (2)

Andrey Danilov
Andrey Danilov

Reputation: 6592

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

Upvotes: 5

James Perkins
James Perkins

Reputation: 58

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.

Upvotes: 1

Related Questions