Code Monkey home page Code Monkey logo

libgs1-kotlin's Introduction

GS1 barcode (EAN/UPC, Databar barcodes) parser library

This library parses the GS1 fields stream according to the GS1 specification.

Usage

Getting the GS1 fields from a stream

val fields = FieldsAI.from(gs1Stream)

FieldsAI.from will return an object with attributes

class FieldsAI(
    val list: List<FieldAI>,
    val hasError: Boolean
)

Each field in the resulting list of fields List<FieldAI> will contain the following information

data class FieldAI(
    val ai: AI,
    val textAi: String,
    val textBody: String,
    val referencePos: Int,
    val referenceLen: Int
)

Where ai is the complete description of this GS1 AI tag, textAi is the actual AI tag code from the stream, textBody is the body content of this GS1 field, referencePos is the starting position of this GS1 field in the source stream, referenceLen is the length of the GS1 field in the source stream.

Example

val gs1Stream = "010061414199999610ABCDEF123456\u001D21654321FEDCBA\u001D310200123411140823"
val fields = FieldsAI.from(gs1Stream)

val gtin = fields.list.firstOrNull { it.ai.ai == "01" }
val gtinText: String? = gtin?.textBody
println("AI 01: product identity code = \"$gtinText\"")

val net = fields.list.firstOrNull { it.ai.ai == "310n" }
val netWeight: Float? = net?.formatBody()?.toFloatOrNull()
println("AI 310n: net weight = $netWeight kg")

val some = fields.list.firstOrNull {
    with(it.ai.dataTitle) {
        contains("net", true) && contains("weight", true)
    }
}
println("Some net weight: AI group = ${some?.ai?.ai}, AI code = ${some?.textAi}, weight = ${some?.formatBody()} kg")

Prints

AI 01: product identity code = "00614141999996"
AI 310n: net weight = 12.34 kg
Some net weight: AI group = 310n, AI code = 3102, weight = 12.34 kg

You can find more examples of usage in the unit tests inside the project.

Installation

Download the latest release files. Place .jar file(s) in the <project>/app/libs folder (in case of Android project). Add dependensies to the build.gradle script of your app module.

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    ...
}

Run gradle sync. Done.

libgs1-kotlin's People

Contributors

sergereinov avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.