1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# C bindings for zxing-cpp
This is a preview/proposal for a C-API to zxing-cpp. If you have any comments or feedback, please have a look at https://github.com/zxing-cpp/zxing-cpp/discussions/583.
## Installation
It is currently included in the default build to be trivially accessible for everyone.
Probably the easiest way to play with the C-API is to just modify the [ZXingCTest.c](https://github.com/zxing-cpp/zxing-cpp/blob/master/wrappers/c/ZXingCTest.c) file.
## Usage
The following is close to the most trivial use case scenario that is supported.
```c
#include "ZXing/ZXingC.h"
int main(int argc, char** argv)
{
int width, height;
unsigned char* data;
/* load your image data from somewhere. ZXing_ImageFormat_Lum assumes grey scale image data. */
ZXing_ImageView* iv = ZXing_ImageView_new(data, width, height, ZXing_ImageFormat_Lum, 0, 0);
ZXing_ReaderOptions* opts = ZXing_ReaderOptions_new();
/* set ReaderOptions properties, if requried, e.g. */
ZXing_ReaderOptions_setFormats(ZXing_BarcodeFormat_QRCode | ZXing_BarcodeFromat_EAN13);
ZXing_Barcodes* barcodes = ZXing_ReadBarcodes(iv, opts);
ZXing_ImageView_delete(iv);
ZXing_ReaderOptions_delete(opts);
if (barcodes) {
for (int i = 0, n = ZXing_Barcodes_size(barcodes); i < n; ++i) {
const ZXing_Barcode* barcode = ZXing_Barcodes_at(barcodes, i);
char* format = ZXing_BarcodeFormatToString(ZXing_Barcode_format(barcode));
printf("Format : %s\n", format);
ZXing_free(format);
char* text = ZXing_Barcode_text(barcode);
printf("Text : %s\n", text);
ZXing_free(text);
}
ZXing_Barcodes_delete(barcodes);
} else {
char* error = ZXing_LastErrorMsg();
fprintf(stderr, "%s\n", error);
ZXing_free(error);
}
return 0;
}
```
|