|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <string.h> |
| 4 | +#include <ScanbotSDK.h> |
| 5 | + |
| 6 | +const char *error_message(scanbotsdk_error_code_t ec) { |
| 7 | + if (ec == SCANBOTSDK_ERROR_INVALID_LICENSE_STATUS) { |
| 8 | + return "Invalid license status. Please check your license key."; |
| 9 | + } |
| 10 | + return scanbotsdk_error_message(); |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +struct frame { |
| 15 | + int width; |
| 16 | + int height; |
| 17 | + int channels; |
| 18 | + int stride; |
| 19 | + unsigned char *data; |
| 20 | +}; |
| 21 | +typedef struct frame frame; |
| 22 | + |
| 23 | +struct mock_camera { |
| 24 | + frame mock_frame; |
| 25 | +}; |
| 26 | +typedef struct mock_camera mock_camera; |
| 27 | + |
| 28 | +frame *next_frame(mock_camera *camera) { |
| 29 | + return &camera->mock_frame; |
| 30 | +} |
| 31 | + |
| 32 | +mock_camera *init_camera(const char *path) { |
| 33 | + scanbotsdk_image_t *image = NULL; |
| 34 | + scanbotsdk_path_image_load_options_t *load_options = NULL; |
| 35 | + scanbotsdk_path_image_load_options_create_with_defaults(&load_options); |
| 36 | + scanbotsdk_error_code_t ec = scanbotsdk_image_create_from_path(path, load_options, &image); |
| 37 | + scanbotsdk_path_image_load_options_free(load_options); |
| 38 | + if (ec != SCANBOTSDK_OK) { |
| 39 | + fprintf(stderr, "Failed to create image from path: %d: %s\n", ec, error_message(ec)); |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + scanbotsdk_raw_image_t *raw_image = NULL; |
| 43 | + ec = scanbotsdk_image_to_raw_image(image, &raw_image); |
| 44 | + scanbotsdk_image_free(image); |
| 45 | + if (ec != SCANBOTSDK_OK) { |
| 46 | + fprintf(stderr, "Failed to obtain raw image: %d: %s\n", ec, error_message(ec)); |
| 47 | + return NULL; |
| 48 | + } |
| 49 | + frame f; |
| 50 | + |
| 51 | + scanbotsdk_raw_image_get_width(raw_image, &f.width); |
| 52 | + |
| 53 | + scanbotsdk_raw_image_get_height(raw_image, &f.height); |
| 54 | + scanbotsdk_raw_image_get_channels(raw_image, &f.channels); |
| 55 | + scanbotsdk_raw_image_get_stride(raw_image, &f.stride); |
| 56 | + |
| 57 | + void *data; |
| 58 | + scanbotsdk_raw_image_get_data(raw_image, &data); |
| 59 | + |
| 60 | + size_t data_size = f.stride * f.height; |
| 61 | + f.data = malloc(data_size); |
| 62 | + memcpy(f.data, data, data_size); |
| 63 | + scanbotsdk_raw_image_free(raw_image); |
| 64 | + mock_camera *camera = malloc(sizeof(mock_camera)); |
| 65 | + camera->mock_frame = f; |
| 66 | + return camera; |
| 67 | +} |
| 68 | + |
| 69 | +void free_camera(mock_camera *camera) { |
| 70 | + if (!camera) { |
| 71 | + return; |
| 72 | + } |
| 73 | + free(camera->mock_frame.data); |
| 74 | + free(camera); |
| 75 | +} |
| 76 | + |
| 77 | +int main(int argc, char *argv[]) { |
| 78 | + if (argc < 3) { |
| 79 | + fprintf(stderr, "Usage: %s <license_key> <path_to_camera_mock_image>\n", argv[0]); |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + const char *license_key = argv[1]; |
| 84 | + const char *mock_frame_path = argv[2]; |
| 85 | + |
| 86 | + // region Initialize Scanbot SDK |
| 87 | + scanbotsdk_init_params_t params; |
| 88 | + params.license_key = license_key; |
| 89 | + params.writeable_path = "."; |
| 90 | + |
| 91 | + scanbotsdk_error_code_t ec; |
| 92 | + ec = scanbotsdk_initialize(¶ms); |
| 93 | + if (ec != SCANBOTSDK_OK) { |
| 94 | + fprintf(stderr, "Failed to initialize Scanbot SDK: %d: %s\n", ec, error_message(ec)); |
| 95 | + return 1; |
| 96 | + } |
| 97 | + // endregion |
| 98 | + |
| 99 | + // region Create Barcode Scanner Configuration |
| 100 | + scanbotsdk_barcode_format_common_configuration_t *common_format_configuration = NULL; |
| 101 | + ec = scanbotsdk_barcode_format_common_configuration_create_with_defaults( |
| 102 | + &common_format_configuration); |
| 103 | + if (ec != SCANBOTSDK_OK) { |
| 104 | + fprintf(stderr, "Failed to create common format configuration: %d: %s\n", ec, |
| 105 | + error_message(ec)); |
| 106 | + return 1; |
| 107 | + } |
| 108 | + scanbotsdk_barcode_format_t common_formats[] = { |
| 109 | + SCANBOTSDK_BARCODE_FORMAT_AZTEC, |
| 110 | + SCANBOTSDK_BARCODE_FORMAT_CODABAR, |
| 111 | + SCANBOTSDK_BARCODE_FORMAT_CODE_39, |
| 112 | + SCANBOTSDK_BARCODE_FORMAT_CODE_93, |
| 113 | + SCANBOTSDK_BARCODE_FORMAT_CODE_128, |
| 114 | + SCANBOTSDK_BARCODE_FORMAT_DATA_MATRIX, |
| 115 | + SCANBOTSDK_BARCODE_FORMAT_DATABAR, |
| 116 | + SCANBOTSDK_BARCODE_FORMAT_DATABAR_EXPANDED, |
| 117 | + SCANBOTSDK_BARCODE_FORMAT_DATABAR_LIMITED, |
| 118 | + SCANBOTSDK_BARCODE_FORMAT_EAN_13, |
| 119 | + SCANBOTSDK_BARCODE_FORMAT_EAN_8, |
| 120 | + SCANBOTSDK_BARCODE_FORMAT_ITF, |
| 121 | + SCANBOTSDK_BARCODE_FORMAT_PDF_417, |
| 122 | + SCANBOTSDK_BARCODE_FORMAT_QR_CODE, |
| 123 | + SCANBOTSDK_BARCODE_FORMAT_UPC_A, |
| 124 | + SCANBOTSDK_BARCODE_FORMAT_UPC_E |
| 125 | + }; |
| 126 | + ec = scanbotsdk_barcode_format_common_configuration_set_formats(common_format_configuration, common_formats, |
| 127 | + sizeof(common_formats) / sizeof(common_formats[0])); |
| 128 | + if (ec != SCANBOTSDK_OK) { |
| 129 | + scanbotsdk_barcode_format_common_configuration_free(common_format_configuration); |
| 130 | + fprintf(stderr, "Failed to set formats in common format configuration: %d: %s\n", ec, |
| 131 | + error_message(ec)); |
| 132 | + return 1; |
| 133 | + } |
| 134 | + |
| 135 | + scanbotsdk_barcode_format_configuration_base_t *common_format_configuration_as_base = NULL; |
| 136 | + scanbotsdk_barcode_format_common_configuration_as_scanbotsdk_barcode_format_configuration_base( |
| 137 | + common_format_configuration, &common_format_configuration_as_base); |
| 138 | + |
| 139 | + scanbotsdk_barcode_scanner_configuration_t *barcode_scanner_configuration; |
| 140 | + ec = scanbotsdk_barcode_scanner_configuration_create_with_defaults( |
| 141 | + &barcode_scanner_configuration); |
| 142 | + |
| 143 | + if (ec != SCANBOTSDK_OK) { |
| 144 | + scanbotsdk_barcode_format_configuration_base_free(common_format_configuration_as_base); |
| 145 | + fprintf(stderr, "Failed to create barcode scanner configuration: %d: %s\n", ec, |
| 146 | + error_message(ec)); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + ec = scanbotsdk_barcode_scanner_configuration_set_barcode_format_configurations( |
| 150 | + barcode_scanner_configuration, &common_format_configuration_as_base, 1); |
| 151 | + scanbotsdk_barcode_format_configuration_base_free(common_format_configuration_as_base); |
| 152 | + if (ec != SCANBOTSDK_OK) { |
| 153 | + scanbotsdk_barcode_scanner_configuration_free(barcode_scanner_configuration); |
| 154 | + fprintf(stderr, "Failed to set barcode format configurations: %d: %s\n", ec, |
| 155 | + error_message(ec)); |
| 156 | + return 1; |
| 157 | + } |
| 158 | + // endregion |
| 159 | + |
| 160 | + // region Create Barcode Scanner |
| 161 | + scanbotsdk_barcode_scanner_t *barcode_scanner = NULL; |
| 162 | + ec = scanbotsdk_barcode_scanner_create(barcode_scanner_configuration, &barcode_scanner); |
| 163 | + scanbotsdk_barcode_scanner_configuration_free(barcode_scanner_configuration); |
| 164 | + if (ec != SCANBOTSDK_OK) { |
| 165 | + fprintf(stderr, "Failed to create barcode scanner: %d: %s\n", ec, |
| 166 | + error_message(ec)); |
| 167 | + return 1; |
| 168 | + } |
| 169 | + // endregion |
| 170 | + |
| 171 | + // region create mock camera that always returns the same frame |
| 172 | + mock_camera *camera = init_camera(mock_frame_path); |
| 173 | + if (!camera) { |
| 174 | + scanbotsdk_barcode_scanner_free(barcode_scanner); |
| 175 | + fprintf(stderr, "Failed to initialize mock camera with path: %s\n", mock_frame_path); |
| 176 | + return 1; |
| 177 | + } |
| 178 | + // endregion |
| 179 | + |
| 180 | + for (size_t frameNumber = 0; frameNumber < 10; frameNumber++) { |
| 181 | + fprintf(stdout, "Processing frame %zu\n", frameNumber); |
| 182 | + |
| 183 | + // region create scanbotsdk_image_t from mock camera frame |
| 184 | + frame *frame = next_frame(camera); |
| 185 | + bool imageIsLive = true; |
| 186 | + scanbotsdk_raw_image_load_options_t *load_options = NULL; |
| 187 | + scanbotsdk_raw_image_load_options_create_with_defaults(imageIsLive, &load_options); |
| 188 | + scanbotsdk_image_t *image = NULL; |
| 189 | + ec = scanbotsdk_image_create_from_raw_image( |
| 190 | + frame->data, frame->width, frame->height, frame->channels, frame->stride, load_options, &image); |
| 191 | + scanbotsdk_raw_image_load_options_free(load_options); |
| 192 | + if (ec != SCANBOTSDK_OK) { |
| 193 | + scanbotsdk_barcode_scanner_free(barcode_scanner); |
| 194 | + free_camera(camera); |
| 195 | + fprintf(stderr, "Failed to create image from raw data: %d: %s\n", ec, |
| 196 | + error_message(ec)); |
| 197 | + return 1; |
| 198 | + } |
| 199 | + // endregion |
| 200 | + |
| 201 | + // region run barcode scanner on the image |
| 202 | + scanbotsdk_barcode_scanner_result_t *result = NULL; |
| 203 | + ec = scanbotsdk_barcode_scanner_run(barcode_scanner, image, &result); |
| 204 | + scanbotsdk_image_free(image); |
| 205 | + if (ec != SCANBOTSDK_OK) { |
| 206 | + scanbotsdk_barcode_scanner_free(barcode_scanner); |
| 207 | + free_camera(camera); |
| 208 | + fprintf(stderr, "Failed to run barcode scanner: %d: %s\n", ec, |
| 209 | + error_message(ec)); |
| 210 | + return 1; |
| 211 | + } |
| 212 | + bool success = false; |
| 213 | + scanbotsdk_barcode_scanner_result_get_success(result, &success); |
| 214 | + if (!success) { |
| 215 | + fprintf(stdout, "No barcodes found in frame %zu\n", frameNumber); |
| 216 | + } else { |
| 217 | + size_t barcodeCount = 0; |
| 218 | + scanbotsdk_barcode_scanner_result_get_barcodes_size(result, &barcodeCount); |
| 219 | + fprintf(stdout, "%zu barcodes found in frame %zu\n", barcodeCount, frameNumber); |
| 220 | + scanbotsdk_barcode_item_t **barcodes = malloc(barcodeCount * sizeof(scanbotsdk_barcode_item_t *)); |
| 221 | + scanbotsdk_barcode_scanner_result_get_barcodes(result, barcodes, barcodeCount); |
| 222 | + for (size_t i = 0; i < barcodeCount; i++) { |
| 223 | + const char *text = NULL; |
| 224 | + scanbotsdk_barcode_item_get_text(barcodes[i], &text); |
| 225 | + fprintf(stdout, "Barcode %zu: %s\n", i, text); |
| 226 | + } |
| 227 | + free(barcodes); |
| 228 | + } |
| 229 | + scanbotsdk_barcode_scanner_result_free(result); |
| 230 | + // endregion |
| 231 | + } |
| 232 | + scanbotsdk_barcode_scanner_free(barcode_scanner); |
| 233 | + free_camera(camera); |
| 234 | + |
| 235 | + return 0; |
| 236 | +} |
| 237 | + |
0 commit comments