in android dev ~ read.

Code128 barcode with Kotlin

Recently I needed to use Code128 barcode in project but there was none good and easy libraries that fits my need so I decided I'll make one.

So what is Code 128?
Code 128 is a high-density linear barcode symbology. It is used for alphanumeric or numeric-only barcodes. It can encode all 128 characters of ASCII.
More you can get on Wikipedia

In order to draw barcode properly we need to study table that we saw on Wikipedia page.

Code128 specifies a combination of 6 bars and spaces for each symbol. Thus, each symbol begins with a bar and ends with a space. In barcode fonts, the final bar is generally combined with the stop symbol to make a wider stop pattern. The following table details the widths associated with each bar and space for each symbol. The width of each bar or space may be 1, 2, 3 or 4 units (modules). Using the example above, an 'A' would be depicted with the pattern 10100011000, or as 111323 in the tables below.

Based on that and knowledge we can easily start creating our drawing procedure that will based on data we provide draw lines.
This is small portion that draws bars for barcode and as you can see is very simple

while (inputX < code.size) {
    if (code?.get(inputX)?.toInt()  == 1) {
        canvas.drawRect(outputX.toFloat(), TOP_GAP.toFloat(), (outputX + multiple).toFloat(), outputHeight.toFloat(), barPaint)
    }
    inputX++
    outputX += multiple
}

Full code with sample usage you can get on Github: https://github.com/InvalidExcepti0n/kCode128

For any question drop comment or issue on Github