Page Example

Self Camera Sample Overview

Mobile native

The SelfCamera sample application demonstrates how you can use the front camera in your application.

The following figure illustrates the main view of the SelfCamera application in its normal state and the countdown state.

Figure: SelfCamera main view

SelfCamera main screen


To ensure proper application execution, the following privileges must be set:



To use the camera:

  1. Create the layout for the camera preview.

    The main_view_add() function creates the main layout of the SelfCamera application and adds the evas_object_image for displaying the camera preview.

    Evas_Object * main_view_add(Evas_Object *navi)
       // SELF_CAMERA_LAYOUT refers to "edje/self-camera.edj"
       elm_layout_file_set(view->layout, get_resource_path(SELF_CAMERA_LAYOUT), "layout");
       // Signal for default timer option to be selected
       elm_object_signal_emit(view->layout, "mouse,clicked,1", "timer_2");
       view->preview_canvas = evas_object_image_filled_add(evas_object_evas_get(view->layout));
       // view->preview_canvas is placed at "elm.swallow.content"
       elm_object_part_content_set(view->layout, "elm.swallow.content", view->preview_canvas);
       view->camera_enabled = _main_view_start_preview(view); 
       return view->layout;
  2. Initialize the camera and start the preview.

    The _main_view_start_preview() function creates the camera handle and sets the parameters for the camera preview. It assigns a previously created image to be displayed in the camera preview and then starts the preview.

    static Eina_Bool _main_view_start_preview(main_view *view)
       // Create camera handle for front camera (CAMERA_DEVICE_CAMERA1)
       int result = camera_create(CAMERA_DEVICE_CAMERA1, &view->camera);
       if (CAMERA_ERROR_NONE == result)
          if (view->preview_canvas)
             // Set image to be used for displaying camera preview
             result = camera_set_display(view->camera, CAMERA_DISPLAY_TYPE_EVAS, GET_DISPLAY(view->preview_canvas));
             if (CAMERA_ERROR_NONE == result)
                // Set preview display size
                camera_set_display_mode(view->camera, CAMERA_DISPLAY_MODE_ORIGIN_SIZE);
                // Set preview display rotation
                camera_set_display_rotation(view->camera, CAMERA_ROTATION_180); 
                // Set preview display flip
                camera_set_display_flip(view->camera, CAMERA_FLIP_VERTICAL); 
                // Start camera preview 
                result = camera_start_preview(view->camera);
                if (CAMERA_ERROR_NONE == result)
                   // Show preview display
                   camera_set_display_visible(view->camera, true); 
                   // Start camera auto-focusing
                   camera_start_focusing(view->camera, TRUE);
       return !result;
  3. Start the camera capture.

    When the countdown reaches 0, the _main_view_timer_cb() timer callback starts camera capture using a shot, which the camera_start_capture() function called. This function changes the camera state from CAMERA_STATE_CAPTURING to CAMERA_STATE_CAPTURED automatically and the corresponding callback functions, _main_view_capture_cb() and _main_view_capture_completed_cb() are invoked.

    static Eina_Bool _main_view_timer_cb(void *data)
       view->timer_count = view->timer_count - 1;
       if (view->timer_count > 0)
          return ECORE_CALLBACK_RENEW;
          if (view->camera_enabled)
             camera_start_capture(view->camera, _main_view_capture_cb, _main_view_capture_completed_cb, view);
          return ECORE_CALLBACK_CANCEL;
  4. Save the captured image.

    The captured image is delivered to the _main_view_capture_cb() function, which saves it into the file.

    static void _main_view_capture_cb(camera_image_data_s *image, camera_image_data_s *postview, camera_image_data_s *thumbnail, void *user_data)
       if (image->format == CAMERA_PIXEL_FORMAT_JPEG)
          char filename[PATH_MAX] = {'\0'};
          size_t size = _main_view_get_file_path(filename, sizeof(filename));
          DBG("%s", filename);
          RETM_IF(0 == size, "_main_view_get_filename() failed");
          FILE *file = fopen(filename, "w+");
          RETM_IF(!file, "fopen() failed");
          size = fwrite(image->data, image->size, 1, file);
          WARN_IF(size != 1, "fwrite() failed");
          _main_view_thumbnail_set(view, filename);
  5. Restart the camera preview.

    The _main_view_capture_completed_cb() callback is called when camera capture is complete. It restarts the camera preview using the camera_start_preview() function.

    static void _main_view_capture_completed_cb(void *data)
  6. Stop the camera preview and destroy the handle.

    The camera preview can be stopped using the camera_stop_preview() function. The camera handle must be destroyed using the camera_destroy() function.

    static void _main_view_destroy(void *data, Evas *e, Evas_Object *obj, void *event_info)
       main_view *view = data;