When using barcode detect function to scan camera preview stream , “Key not available ”  allows returned

When using barcode detect function to scan camera preview stream , “Key not available ”  allows returned

BY 10 Jan 2017 Native Application Development

Hi ,I’m using barcode detection to scan camera preview stream,but allways get errorcode: -126(Key not available ​). 

My code is follow:

#include "../../inc/Qrcode/QrcodeScan.h"

static bardetdata_s bardetdata;
qrscan_view_data *qr_scan_vm;

static void _on_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info);

static void _destroty_camera()
{
    dlog_print(DLOG_DEBUG,"QrCode","%s",__func__);
 int error_code = 0;
 error_code = camera_unset_media_packet_preview_cb(bardetdata.g_camera);
 if(error_code != CAMERA_ERROR_NONE)
  dlog_print(DLOG_ERROR, "QrCode", "camera_unset_media_packet_preview_cb ::error code = %d", error_code);

 error_code = camera_stop_preview(bardetdata.g_camera);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_stop_preview ::error code = %d", error_code);

 error_code = camera_destroy(bardetdata.g_camera);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_destroy::error code = %d", error_code);

 error_code = mv_destroy_engine_config(bardetdata.g_engine_cfg);
 if(error_code != MEDIA_VISION_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "mv_destroy_engine_config::error code = %d", error_code);

 error_code = mv_destroy_source(bardetdata.g_source);
 if(error_code != MEDIA_VISION_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "mv_destroy_source::error code = %d", error_code);
}

static void _barcode_detected_cb(mv_source_h source, mv_engine_config_h engine_cfg, const mv_quadrangle_s *barcode_locations,
  const char *message[], const mv_barcode_type_e *types, int number_of_barcodes, void *user_data)
{
 dlog_print(DLOG_DEBUG,"QrCode","%s",__func__);
 int i = 0;
 char type[50] =
 { '\0' };

 /* Clear the source for the next preview image */
 mv_source_clear(source);

 if(number_of_barcodes > 0)
 {
  for(i = 0; i < number_of_barcodes; i++)
   dlog_print(DLOG_INFO, "QrCode", "%d >> message: %s\n", i, message[i]);
 }
}

static void _camera_media_packet_preview_cb(media_packet_h pkt, void *user_data)
{
 mv_rectangle_s mv_roi =
 {
 { 0, 0 }, bardetdata.width, bardetdata.height };

 if(pkt == NULL) return;

 int error_code = 0;
 error_code = mv_source_fill_by_media_packet(bardetdata.g_source, pkt);
 if(error_code!=0) dlog_print(DLOG_ERROR, "QrCode", "mv_source_fill_by_media_packet::error code = %d", error_code);

 if(pkt)
 {
  error_code = media_packet_destroy(pkt);
  pkt = NULL;
 }

 error_code = mv_barcode_detect(bardetdata.g_source, bardetdata.g_engine_cfg, mv_roi, _barcode_detected_cb, NULL);
 dlog_print(DLOG_DEBUG,"QrCode","MEDIA_VISION_ERROR_INVALID_PARAMETER:%d,MEDIA_VISION_ERROR_NOT_SUPPORTED:%d,MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT:%d,MEDIA_VISION_ERROR_INTERNAL:%d",MEDIA_VISION_ERROR_INVALID_PARAMETER,MEDIA_VISION_ERROR_NOT_SUPPORTED,MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT,MEDIA_VISION_ERROR_INTERNAL);
 if(error_code != MEDIA_VISION_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "mv_barcode_detect::error code = %d", error_code);
}

void _start_preview()
{
 int error_code = 0;

 //qrcode about
 error_code = mv_create_source(&bardetdata.g_source);
 if(error_code != MEDIA_VISION_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "mv_create_source::error code = %d", error_code);

 error_code = mv_create_engine_config(&bardetdata.g_engine_cfg);
 if(error_code != MEDIA_VISION_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "mv_create_engine_config::error code = %d", error_code);

 /* Create the camera handle */
 error_code = camera_create(CAMERA_DEVICE_CAMERA0, &bardetdata.g_camera);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_create::error code = %d", error_code);

 /*Check the camera state */
 camera_state_e state;
 error_code = camera_get_state(bardetdata.g_camera, &state);
 if(state != CAMERA_STATE_CREATED) dlog_print(DLOG_ERROR, "QrCode", "camera state: %d", state);

 /*set image quality*/
 error_code = camera_attr_set_image_quality(bardetdata.g_camera, 100);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_attr_set_image_quality::error code = %d", error_code);

 /* Set the camera display */
 error_code = camera_set_display(bardetdata.g_camera, CAMERA_DISPLAY_TYPE_EVAS, GET_DISPLAY(qr_scan_vm->camera_ret));
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_set_display::error code = %d", error_code);

 /* Get the camera preview resolution */
 error_code = camera_get_preview_resolution(bardetdata.g_camera, &bardetdata.width, &bardetdata.height);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_get_preview_resolution::error code %d", error_code);

 /* Set the camera preview callback */
 error_code = camera_set_media_packet_preview_cb(bardetdata.g_camera, _camera_media_packet_preview_cb, NULL);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_set_media_packet_preview_cb::error code = %d", error_code);

 /* Start the camera preview */
 error_code = camera_start_preview(bardetdata.g_camera);
 dlog_print(DLOG_DEBUG, "Qrcode",
   "CAMERA_ERROR_INVALID_STATE:%d,CAMERA_ERROR_SOUND_POLICY:%d,CAMERA_ERROR_INVALID_OPERATION:%d,CAMERA_ERROR_DEVICE_BUSY:%d,CAMERA_ERROR_DEVICE_NOT_FOUND:%d,CAMERA_ERROR_PERMISSION_DENIED:%d,CAMERA_ERROR_NOT_SUPPORTED:%d",CAMERA_ERROR_SOUND_POLICY,CAMERA_ERROR_INVALID_STATE,CAMERA_ERROR_INVALID_OPERATION,CAMERA_ERROR_DEVICE_BUSY,CAMERA_ERROR_DEVICE_NOT_FOUND,CAMERA_ERROR_PERMISSION_DENIED,CAMERA_ERROR_NOT_SUPPORTED);
 if(error_code != CAMERA_ERROR_NONE) dlog_print(DLOG_ERROR, "QrCode", "camera_start_preview::error code = %d", error_code);

}

void CreateQrcodeScanPage()
{
 dlog_print(DLOG_DEBUG, "QrCode", "%s", __func__);

 qr_scan_vm = (qrscan_view_data*) calloc(1, sizeof(qrscan_view_data));

 if(qr_scan_vm == NULL) return;

 /*layout */
 char edj_file_path[PATH_MAX] =
 { 0, };
 app_get_resource(QRSCAN_VIEW_EDJ, edj_file_path, PATH_MAX);

 Evas_Object *layout = elm_layout_add(appData->navframe);
 elm_layout_file_set(layout, edj_file_path, "main");
 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
 evas_object_size_hint_align_set(layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
 evas_object_show(layout);

 qr_scan_vm->layout = layout;

 //camera preview
 Evas *evas = evas_object_evas_get(appData->navframe);
 Evas_Object *camera_rect = evas_object_image_filled_add(evas);
 evas_object_show(camera_rect);

 elm_object_part_content_set(layout, "camera", camera_rect);

 qr_scan_vm->camera_ret = camera_rect;

 //set naviframe
 elm_naviframe_item_push(appData->navframe, "QrScan", NULL, NULL, layout, NULL);

 //set callback
 evas_object_event_callback_add(layout, EVAS_CALLBACK_DEL, _on_layout_del_cb, NULL);

 _start_preview();
}

static void _on_layout_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
{
 //destroy camera source
 _destroty_camera();
​//to do: free view data
}

 

Written by