The easiest way to use the library is to fetch if from mavenCentral. Simply add
implementation("io.github.zxing-cpp:kotlin-native:2.2.1")to your build.gradle.kts file in the dependencies section of nativeMain source set.
A trivial use case looks like this:
import zxingcpp.BarcodeFormat
import zxingcpp.BarcodeReader
import zxingcpp.ImageFormat
import zxingcpp.ImageView
val data: ByteArray = ... // the image data
val width: Int = ... // the image width
val height: Int = ... // the image height
val format: ImageFormat = ImageFormat.Lum // ImageFormat.Lum assumes grey scale image data
val image: ImageView = ImageView(data, width, height, format)
val barcodeReader = BarcodeReader().apply {
formats = setOf(BarcodeFormat.EAN13, BarcodeFormat.QRCode)
tryHarder = true
maxNumberOfSymbols = 3
// more options, see documentation
}
barcodeReader.read(image).joinToString("\n") { barcode: Barcode ->
"${barcode.format} (${barcode.contentType}): ${barcode.text}"
}Here you have to load your image into memory by yourself and pass the decoded data to the constructor of ImageView.
A trivial use case looks like this:
import zxingcpp.*
val text: String = "Hello, World!"
val format = BarcodeFormat.QRCode
@OptIn(ExperimentalWriterApi::class)
val cOpts = CreatorOptions(format) // more options, see documentation
@OptIn(ExperimentalWriterApi::class)
val barcode = Barcode(text, cOpts)
// or
@OptIn(ExperimentalWriterApi::class)
val barcode2 = Barcode(text.encodeToByteArray(), format)
@OptIn(ExperimentalWriterApi::class)
val wOpts = WriterOptions().apply {
sizeHint = 400
// more options, see documentation
}
@OptIn(ExperimentalWriterApi::class)
val svg: String = barcode.toSVG(wOpts)
@OptIn(ExperimentalWriterApi::class)
val image: Image = barcode.toImage(wOpts)Note: The Writer api is still experimental and may change in future versions. You will have to opt-in
zxingcpp.ExperimentalWriterApito use it.
- Install JDK, CMake and Android NDK(With
$ANDROID_NDKcorrectly configured) and ensure their executable binaries appear in$PATH. - Prepare kotlin/native toolchain (You can easily do this by cloning
K/N Toolchain Initializer and executing
gradle buildso that kotlin will download toolchains needed into user's home dir.). - Ensure there's
run_konanavailable in$PATHor specify path of kotlin-native toolchain inlocal.propertieslikekonan.dir=/home/user/.konan/kotlin-native-prebuilt-linux-x86_64-1.9.22. - Ensure there's
llvm-aravailable in$PATHfor mingwX64 target building.
And then you can build the project from the command line:
$ ./gradlew :assemble