Mobile native

SelfCamera Sample Overview

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

Implementation

To use the camera:

  1. Add required privileges.

    To use the camera, the application has to request permission by adding the corresponding privileges to the tizen-manifest.xml file. In this case, the appmanager.launch privilege is also added to allow the launch of other applications, such as the gallery.

    <privileges>
       <privilege>http://tizen.org/privilege/camera</privilege>
       <privilege>http://tizen.org/privilege/appmanager.launch</privilege>
    </privileges>
    

    This can also be done using the Tizen Manifest Editor.

  2. 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); 
        
       ...
       _main_view_thumbnail_load(view);
       _main_view_register_cbs(view);
    
       ...
       return view->layout;
    } 
    
  3. 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;
    }
    
  4. 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;
       }
       else
       {
          _main_view_stop_timer(view);
          if (view->camera_enabled)
          {
             camera_start_capture(view->camera, _main_view_capture_cb, _main_view_capture_completed_cb, view);
          }
          ...
          return ECORE_CALLBACK_CANCEL;
       }
    }
    
  5. 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");
    
          fclose(file);
          _main_view_thumbnail_set(view, filename);
       }
    }
    
  6. 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)
    {
       ...
       camera_start_preview(view->camera);
    }
    
  7. 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;
       camera_stop_preview(view->camera);
       camera_destroy(view->camera);
       free(data);
    }
    
Go to top