Detecting Barcodes in a Camera Image Stream
This example uses the Scandit SDK to detect barcodes in frames from a camera.
This demo is currently limited to GNU/Linux systems with a Video4Linux 2 camera.
Low-End Devices
On slow hardware platforms like the Raspberry 2, 3 or Zero we recommend the following change to the sample: Set the define LOW_END_DEVICE_CONFIGURATION to 1 and run the sample with a resolution of 640x480.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define SCANDIT_SDK_LICENSE_KEY "-- INSERT YOUR LICENSE KEY HERE --"
#define DEFAULT_RESOLUTION_WIDTH 1280
#define DEFAULT_RESOLUTION_HEIGHT 720
#define LOW_END_DEVICE_CONFIGURATION 0
static volatile ScBool process_frames;
static void catch_exit(int signo) {
printf("SIGINT received.\n");
}
static void print_all_discrete_resolutions(
const ScCamera *cam) {
printf("This camera uses discrete resolutions:\n");
for (int32_t i = 0; i < resolution_count; i++) {
for (int32_t j = 0; j < framerate_count; j++) {
printf("\t%u:%u @ %.2f FPS\n", resolution_array[i].width, resolution_array[i].height, fps);
}
}
}
int main(int argc, const char *argv[]) {
if (signal(SIGINT, catch_exit) == SIG_ERR) {
printf("Could not set up signal handler.\n");
return -1;
}
if (argc > 1) {
} else {
}
if (camera == NULL) {
printf("No camera available.\n");
return -1;
}
uint32_t resolution_width = DEFAULT_RESOLUTION_WIDTH;
uint32_t resolution_height = DEFAULT_RESOLUTION_HEIGHT;
if (argc == 4) {
resolution_width = atoi(argv[2]);
resolution_height = atoi(argv[3]);
}
const uint32_t resolutions_size = 30;
ScSize resolutions[resolutions_size];
int32_t resolutions_found;
switch (resm) {
print_all_discrete_resolutions(camera);
if (!resolutions_found) {
printf("There was an error getting the discrete resolution capabilities of the camera.\n");
return -1;
}
for (int i = 0; i < resolutions_found; i++) {
if (resolutions[i].width == resolution_width &&
resolutions[i].height == resolution_height) {
break;
}
}
break;
printf("There was an error getting the stepwise resolution capabilities of the camera.\n");
return -1;
}
printf("This camera uses step-wise resolutions:\n");
}
break;
default:
printf("Could not get camera resolution mode.\n");
return -1;
}
if (!supported) {
printf("%dx%d is not supported by this camera.\nPlease specify a supported resolution on the command line or in the source code.\n", resolution_width, resolution_height);
return -1;
}
desired_resolution.
width = resolution_width;
desired_resolution.
height = resolution_height;
printf("Setting resolution failed.\n");
return -1;
}
printf("Start the camera failed.\n");
return -1;
}
if (context == NULL) {
printf("Could not initialize context.\n");
return -1;
}
if (settings == NULL) {
return -1;
}
.size.width = 1.0f, .size.height = 0.2f };
#if LOW_END_DEVICE_CONFIGURATION
#else
#endif
if (scanner == NULL) {
return -1;
}
while (process_frames) {
if (image_data == NULL) {
printf("Frame access failed. Exiting.\n");
break;
}
printf(
"Processing frame failed with error %d: '%s'\n", result.
status,
}
for (int i = 0; i < code_count; i++) {
printf("Barcode found: '%s'\n", data.str);
}
}
}
ScBool sc_camera_request_resolution(ScCamera *camera, ScSize resolution)
Tries to set the camera resolution.
A unsigned step-wise description of a size.
Definition: ScCamera.h:47
void sc_recognition_context_release(ScRecognitionContext *context)
Decrease reference count of recognition context by one.
int32_t sc_camera_query_supported_framerates(const ScCamera *camera, ScSize resolution, ScFramerate *framerate_array, uint32_t framerate_array_length)
Query supported discrete framerates of the camera at a given resolution.
ScBarcode * sc_barcode_array_get_item_at(const ScBarcodeArray *array, uint32_t index)
Get barcode at specific index in array.
uint32_t step_height
y dimension step size
Definition: ScCamera.h:53
ScByteArray sc_barcode_get_data(const ScBarcode *barcode)
Get the data encoded in the barcode.
@ SC_CODE_LOCATION_RESTRICT
Restrict decoding to the specified area.
Definition: ScBarcodeScannerSettings.h:122
void sc_barcode_scanner_settings_set_max_number_of_codes_per_frame(ScBarcodeScannerSettings *settings, uint32_t max_codes)
Set the maximum number of barcode to be decoded every frame.
Result status and frame id.
Definition: ScRecognitionContext.h:244
void sc_barcode_scanner_settings_set_code_location_area_1d(ScBarcodeScannerSettings *settings, ScRectangleF rect)
Set code location area for 1d codes.
uint32_t min_height
minimum y dimension
Definition: ScCamera.h:51
void sc_barcode_scanner_release(ScBarcodeScanner *scanner)
Decrease reference count of barcode scanner by one.
ScBarcodeScanner * sc_barcode_scanner_new_with_settings(ScRecognitionContext *context, const ScBarcodeScannerSettings *settings)
Create a new barcode scanner instance with the given settings.
@ SC_SYMBOLOGY_UPCA
Definition: ScBarcode.h:45
An opaque data structure holding configuration options for the barcode scanner.
ScCameraMode
Describes the resolution mode of the camera.
Definition: ScCamera.h:129
void sc_barcode_scanner_settings_set_code_location_constraint_2d(ScBarcodeScannerSettings *settings, ScCodeLocationConstraint constraint)
Set the 2d code location constraint.
ScBarcodeScannerSettings * sc_barcode_scanner_settings_new_with_preset(ScBarcodeScannerSettingsPresetFlags preset)
Create a new barcode scanner settings object.
ScContextStatusFlag status
Execution status flag.
Definition: ScRecognitionContext.h:245
ScBarcodeScannerSession * sc_barcode_scanner_get_session(ScBarcodeScanner *scanner)
Get the current scanning session.
const char * sc_context_status_flag_get_message(ScContextStatusFlag status)
Get a human readable error message for a result status.
@ SC_SYMBOLOGY_QR
Definition: ScBarcode.h:69
Barcode Scanner interface.
void sc_recognition_context_start_new_frame_sequence(ScRecognitionContext *context)
Start processing a new batch of continuous frames.
A fixed-size array of barcode objects.
void sc_barcode_scanner_settings_set_code_direction_hint(ScBarcodeScannerSettings *settings, ScCodeDirection direction)
Set the code direction hint.
void sc_barcode_scanner_settings_set_code_duplicate_filter(ScBarcodeScannerSettings *settings, int32_t filter)
Specifies the duplicate filter to use for the session.
ScBool sc_camera_enqueue_frame_data(ScCamera *camera, const uint8_t *frame_data)
Requeues the buffer after the data has been used.
Opaque handle to a camera object.
#define SC_FALSE
False value.
Definition: ScConfig.h:139
void sc_barcode_scanner_settings_set_focus_mode(ScBarcodeScannerSettings *settings, ScCameraFocusMode focus_mode)
Set the camera's focus type.
Recognition context interface.
#define SC_TRUE
True value.
Definition: ScConfig.h:133
float x
X-coordinate.
Definition: ScCommon.h:108
Camera creation and control interface.
uint32_t width
x dimension
Definition: ScCommon.h:140
void sc_barcode_scanner_settings_set_code_location_constraint_1d(ScBarcodeScannerSettings *settings, ScCodeLocationConstraint constraint)
Set the 1d code location constraint.
void sc_barcode_scanner_settings_release(ScBarcodeScannerSettings *settings)
Decrease reference count of barcode scanner settings object by one.
An opaque barcode scanner session object.
ScPointF position
Upper-left corner of the rectangle.
Definition: ScCommon.h:167
ScImageDescription * sc_image_description_new(void)
Create a new image description.
@ SC_CAMERA_MODE_STEPWISE
the camera captures at a wide range of step-wise generated resolutions
Definition: ScCamera.h:132
void sc_image_description_release(ScImageDescription *description)
Decrease reference count of image description by one.
ScCamera * sc_camera_new(void)
Create a camera object using default values.
@ SC_SYMBOLOGY_EAN13
Definition: ScBarcode.h:37
ScBool sc_camera_start_stream(ScCamera *camera)
Tells the camera to start capturing images.
ScCameraMode sc_camera_get_resolution_mode(const ScCamera *camera)
Query the resolution mode of the camera.
uint32_t step_width
x dimension step size
Definition: ScCamera.h:50
Describes dimensions as well as internal memory layout of an image buffer.
@ SC_CODE_DIRECTION_LEFT_TO_RIGHT
Definition: ScBarcodeScannerSettings.h:82
uint32_t max_height
maximum y dimension
Definition: ScCamera.h:52
ScBarcodeArray * sc_barcode_scanner_session_get_newly_recognized_codes(const ScBarcodeScannerSession *session)
Get list of recognized codes in the last processed frame.
int32_t ScBool
Boolean value, can be SC_TRUE or SC_FALSE.
Definition: ScConfig.h:127
Scans barcodes in images.
Describes a framerate.
Definition: ScCamera.h:63
uint32_t min_width
minimum x dimension
Definition: ScCamera.h:48
A 2-dimensional unsigned size.
Definition: ScCommon.h:139
ScProcessFrameResult sc_recognition_context_process_frame(ScRecognitionContext *context, const ScImageDescription *image_description, const uint8_t *image_data)
Process image frame with this recognition context.
@ SC_CAMERA_MODE_DISCRETE
the camera captures at a finite set of discrete resolutions
Definition: ScCamera.h:131
void sc_recognition_context_end_frame_sequence(ScRecognitionContext *context)
Signal that a batch of continuous frames ended.
@ SC_RECOGNITION_CONTEXT_STATUS_SUCCESS
The context status is okay. No error occured.
Definition: ScRecognitionContext.h:57
void sc_barcode_array_release(ScBarcodeArray *array)
Decrease reference count of barcode array by one.
Opaque recognition context data structure.
ScCamera * sc_camera_new_from_path(const char *device_path, uint32_t buffer_count)
Create a camera object from a device path.
void sc_camera_release(ScCamera *camera)
Decrease reference count of camera by one.
@ SC_CODE_LOCATION_HINT
The area indicates the most likely code position.
Definition: ScBarcodeScannerSettings.h:128
const uint8_t * sc_camera_get_frame(ScCamera *camera, ScImageDescription *image_description)
A 2-dimensional rectangle with floating point precision.
Definition: ScCommon.h:163
float sc_framerate_get_fps(const ScFramerate *frame_rate)
Returns the number of frames per second.
int32_t sc_camera_query_supported_resolutions(const ScCamera *camera, ScSize *resolution_array, uint32_t resolution_array_length)
Query supported discrete resolutions of the camera.
A located or recognized barcode/ 2d code in an image.
uint32_t height
y dimension
Definition: ScCommon.h:141
ScRecognitionContext * sc_recognition_context_new(const char *license_key, const char *writable_data_path, JNIEnv *env, jobject activity, const char *device_name)
Create a new recognition context (Android only).
A null terminated array of bytes.
@ SC_PRESET_NONE
Preset with all symbologies disabled.
Definition: ScBarcodeScannerSettings.h:32
void sc_barcode_scanner_settings_set_symbology_enabled(ScBarcodeScannerSettings *settings, ScSymbology symbology, ScBool enabled)
Enable/disable decoding of a certain symbology.
@ SC_CAMERA_FOCUS_MODE_FIXED
the camera cannot change its focus
Definition: ScCommon.h:208
ScBool sc_camera_query_supported_resolutions_stepwise(const ScCamera *camera, ScStepwiseResolution *resolutions)
Query supported step-wise resolutions of the camera.
void sc_barcode_scanner_settings_set_code_caching_duration(ScBarcodeScannerSettings *settings, int32_t duration)
Determines how long codes are kept in the session.
uint32_t max_width
maximum x dimension
Definition: ScCamera.h:49
uint32_t sc_barcode_array_get_size(const ScBarcodeArray *array)
Get the number of barcodes in the array.