static void
capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
	int fd, retval;
	CameraFile *canonfile;
	CameraFilePath camera_file_path;

	printf("Capturing.\n");

	/* NOP: This gets overridden in the library to /capt0000.jpg */
	strcpy(camera_file_path.folder, "/");
	strcpy(camera_file_path.name, "foo.jpg");

	retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
	printf("  Retval: %d\n", retval);

	printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);

	fd = open(fn, O_CREAT | O_WRONLY, 0644);
	retval = gp_file_new_from_fd(&canonfile, fd);
	printf("  Retval: %d\n", retval);
	retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
		     GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
	printf("  Retval: %d\n", retval);

	printf("Deleting.\n");
	retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
			canoncontext);
	printf("  Retval: %d\n", retval);

	gp_file_free(canonfile);
}
示例#2
0
void capture_preview(Camera *camera, GPContext *context, const char **ptr, unsigned long int *size) 
{
	int retval;
	CameraFile *file;
	CameraFilePath camera_file_path;

	printf("Capturing.\n");

	/* NOP: This gets overridden in the library to /capt0000.jpg */
	strcpy(camera_file_path.folder, "/");
	strcpy(camera_file_path.name, "foo.jpg");

	retval = gp_camera_capture_preview(camera, &camera_file_path, context);
	printf("  Retval: %d\n", retval);

	printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);

	retval = gp_file_new(&file);
	printf("  Retval: %d\n", retval);
	retval = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name,
		     GP_FILE_TYPE_NORMAL, file, context);
	printf("  Retval: %d\n", retval);

	gp_file_get_data_and_size (file, ptr, size);

	printf("Deleting.\n");
	retval = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name,
			context);
	printf("  Retval: %d\n", retval);
	/*gp_file_free(file); */
}
示例#3
0
static void
capture_to_file(Camera *canon, GPContext *canoncontext, char *fn) {
	int fd, retval;
	CameraFile *canonfile;
	CameraFilePath camera_file_path;

	printf("Capturing.\n");

	retval = gp_camera_capture(canon, GP_CAPTURE_IMAGE, &camera_file_path, canoncontext);
	printf("  Retval: %d\n", retval);

	printf("Pathname on the camera: %s/%s\n", camera_file_path.folder, camera_file_path.name);

	fd = open(fn, O_CREAT | O_WRONLY | O_BINARY, 0644);
	retval = gp_file_new_from_fd(&canonfile, fd);
	printf("  Retval: %d\n", retval);
	retval = gp_camera_file_get(canon, camera_file_path.folder, camera_file_path.name,
		     GP_FILE_TYPE_NORMAL, canonfile, canoncontext);
	printf("  Retval: %d\n", retval);

	printf("Deleting.\n");
	retval = gp_camera_file_delete(canon, camera_file_path.folder, camera_file_path.name,
			canoncontext);
	printf("  Retval: %d\n", retval);

	gp_file_free(canonfile);
}
示例#4
0
void Camera::save_file(const std::string& folder, const std::string& name,
		const std::string& localfile, bool delete_from_cam) {
    std::lock_guard<std::mutex> g(mutex);
	int ret;

	// TODO smart handle
	int fd = open(localfile.c_str(), O_CREAT | O_WRONLY, 0644);
	if (fd == -1)
		throw std::runtime_error("open() failed: " + std::to_string(errno));

	CameraFile *filep;
	if ((ret = gp_file_new_from_fd(&filep, fd)) < GP_OK) {
		close(fd);
		throw Exception("gp_file_new_from_fd", ret);
	}
	std::unique_ptr<CameraFile, int (*)(CameraFile*)> file(filep, gp_file_unref);

	if ((ret = gp_camera_file_get(camera, folder.c_str(), name.c_str(),
			 GP_FILE_TYPE_NORMAL, filep, ctx->context)) < GP_OK) {
		close(fd);
		throw Exception("gp_camera_file_get", ret);
	}

	if (delete_from_cam) {
		if ((ret = gp_camera_file_delete(camera, folder.c_str(),
						name.c_str(), ctx->context)) < GP_OK) {
			close(fd);
			throw Exception("gp_camera_file_delete", ret);
		}
	}

	close(fd);
}
示例#5
0
std::vector<char> Camera::read_image(const std::string& folder, const std::string& name, bool delete_from_cam) {
    std::lock_guard<std::mutex> g(mutex); // TODO: needed?
    int ret;

    CameraFile *filep;
    if ((ret = gp_file_new(&filep)) < GP_OK)
        throw Exception("gp_file_new", ret);

    // Will unref the CameraFile as soon as this method returns (for whatever reason)
    std::unique_ptr<CameraFile, int (*)(CameraFile*)> file(filep, gp_file_unref);

    if ((ret = gp_camera_file_get(camera, folder.c_str(), name.c_str(), GP_FILE_TYPE_NORMAL, filep, ctx->context)) < GP_OK)
        throw Exception("gp_camera_file_get", ret);

    const char* data;
    unsigned long int size;

    if ((ret = gp_file_get_data_and_size(filep, &data, &size)) < GP_OK)
        throw Exception("gp_file_get_data_and_size", ret);

    std::vector<char> buffer(size);
    std::copy(data, data + size, buffer.begin());

    if (delete_from_cam) {
        if ((ret = gp_camera_file_delete(camera, folder.c_str(), name.c_str(), ctx->context)) < GP_OK)
            throw Exception("gp_camera_file_delete", ret);
    }

    return buffer;
}
CAMLprim
value caml_gp_camera_file_delete(value camera_val, value folder_val, \
  value fname_val, value context_val) {
  CAMLparam4(camera_val, folder_val, fname_val, context_val);
  Camera *camera = Camera_val(camera_val);
  GPContext *context = Context_val(context_val);
  const char *folder = String_val(folder_val);
  const char *fname = String_val(fname_val); 
  int ret = gp_camera_file_delete(camera, folder, fname, context);
  CHECK_RESULT(ret);
  CAMLreturn(Val_unit);
}
示例#7
0
void dt_camctl_import(const dt_camctl_t *c,const dt_camera_t *cam,GList *images,gboolean delete_orginals)
{
  //dt_camctl_t *camctl=(dt_camctl_t *)c;
  _camctl_lock(c,cam);

  GList *ifile=g_list_first(images);

  const char *output_path=_dispatch_request_image_path(c,cam);
  if(ifile)
    do
    {
      // Split file into folder and filename
      char *eos;
      char folder[4096]= {0};
      char filename[4096]= {0};
      char *file=(char *)ifile->data;
      eos=file+strlen(file);
      while( --eos>file && *eos!='/' );
      char *_file = g_strndup(file, eos-file);
      g_strlcat(folder, _file, 4096);
      g_strlcat(filename, eos+1, 4096);
      g_free(_file);

      const char *fname = _dispatch_request_image_filename(c,filename,cam);
      if(!fname) fname=filename;

      char *output = g_build_filename(output_path,fname,(char *)NULL);

      // Now we have filenames lets download file and notify listener of image download
      CameraFile *destination;
      int handle = open( output, O_CREAT | O_WRONLY,0666);
      if( handle > 0 )
      {
        gp_file_new_from_fd( &destination , handle );
        if( gp_camera_file_get( cam->gpcam, folder , filename, GP_FILE_TYPE_NORMAL, destination,  c->gpcontext) == GP_OK)
        {
          close( handle );
          _dispatch_camera_image_downloaded(c,cam,output);
          if( delete_orginals )
            gp_camera_file_delete(cam->gpcam, folder, filename, c->gpcontext);
        }
        else
          dt_print(DT_DEBUG_CAMCTL,"[camera_control] failed to download file %s\n",output);


      }
    }
    while( (ifile=g_list_next(ifile)) );

  _dispatch_control_status(c,CAMERA_CONTROL_AVAILABLE);
  _camctl_unlock(c);
}
示例#8
0
/**
 * @brief CameraHandler::_deleteImage
 */
QTLError QTLCamera::_deleteImage(CameraFilePath *cameraFilePath, CameraFile *cameraFile) {
    QTLError result;
    result.rc = gp_camera_file_delete(params->camera, cameraFilePath->folder,
                                      cameraFilePath->name, params->context);
    if (result.rc != GP_OK) {
        result.errorText = gp_result_as_string(result.rc);
        qDebug() << "Problem deleting file from camera." << result.errorText;
    } else {
        qDebug() << "File" << cameraFilePath->folder << cameraFilePath->name
             << "deleted from camera.";
        gp_file_unref(cameraFile);
    }
    return result;
}
示例#9
0
bool Image_GPhoto::capture()
{
    lock_guard<recursive_mutex> lock(_gpMutex);

    if (_selectedCameraIndex == -1)
    {
        Log::get() << Log::WARNING << "Image_GPhoto::" << __FUNCTION__ << " - A camera must be selected before trying to capture" << Log::endl;
        return false;
    }

    GPhotoCamera camera = _cameras[_selectedCameraIndex];

    CameraFilePath filePath{};
    int res;
    if ((res = gp_camera_capture(camera.cam, GP_CAPTURE_IMAGE, &filePath, _gpContext)) == GP_OK)
    {
        if (string(filePath.name).find(".jpg") != string::npos || string(filePath.name).find(".JPG") != string::npos)
        {
            CameraFile* destination;
            int handle = open((string("/tmp/") + string(filePath.name)).c_str(), O_CREAT | O_WRONLY, 0666);
            if (handle != -1)
            {
                gp_file_new_from_fd(&destination, handle);
                if (gp_camera_file_get(camera.cam, filePath.folder, filePath.name, GP_FILE_TYPE_NORMAL, destination, _gpContext) == GP_OK)
                    Log::get() << Log::DEBUGGING << "Image_GPhoto::" << __FUNCTION__ << " - Sucessfully downloaded file " << string(filePath.folder) << "/" << string(filePath.name)
                               << Log::endl;
                close(handle);
            }

            // Read the downloaded file
            readFile(string("/tmp/") + string(filePath.name));
        }
        else
        {
            Log::get() << Log::WARNING << "Image_GPhoto::" << __FUNCTION__ << " - Captured image filetype is not jpeg. Maybe the camera is set to RAW?" << Log::endl;
            res = GP_ERROR;
        }

        gp_camera_file_delete(camera.cam, filePath.folder, filePath.name, _gpContext);

        // Delete the file
        if (remove((string("/tmp/") + string(filePath.name)).c_str()) == -1)
            Log::get() << Log::WARNING << "Image_GPhoto::" << __FUNCTION__ << " - Unable to delete file /tmp/" << filePath.name << Log::endl;
    }

    if (res != GP_OK)
        return false;
    else
        return true;
}
示例#10
0
	int capture_to_file(std::string file_prefix, float exposure,
			std::string iso="1600", std::string aperture="0",
			bool gen_fits=true) {
		int aborted = 0;
		int fd, retval;
		CameraFile *canonfile;
		CameraFilePath camera_file_path;
		set_config_value_string(camera, "iso", iso.c_str(), context);
		if (aperture=="0")
			set_config_value_index(camera, "aperture", 0, context);
		else
			set_config_value_string(camera, "aperture", aperture.c_str(), context);

		printf("Capturing.\n");
		//press shutter, wait for "exposure" seconds, release shutter
		CameraEventType eventtype;
		void* eventdata;
		set_config_value_string(camera, "eosremoterelease", "Press Full", context);
		usleep(exposure*1e6);
		if (errno == EINTR) aborted = 1;
		set_config_value_string(camera, "eosremoterelease", "Release Full", context);

		std::cout << "Waiting for file...\n";
		eventtype = GP_EVENT_UNKNOWN;
		while (eventtype != GP_EVENT_FILE_ADDED) {
			gp_camera_wait_for_event(camera, TIMEOUT, &eventtype, &eventdata, context);
		}
		camera_file_path = *((CameraFilePath*) eventdata);

		printf("Pathname on the camera: %s/%s\n", camera_file_path.folder,
				camera_file_path.name);

		fd = open((file_prefix+".cr2").c_str(),
				O_CREAT | O_WRONLY, 0644);
		retval = gp_file_new_from_fd(&canonfile, fd);
		if (retval != GP_OK) return retval;

		retval = gp_camera_file_get(camera, camera_file_path.folder,
				camera_file_path.name, GP_FILE_TYPE_NORMAL, canonfile,
				context);
		if (retval != GP_OK) return retval;

		retval = gp_camera_file_delete(camera, camera_file_path.folder,
				camera_file_path.name, context);
		if (retval != GP_OK) return retval;

		gp_file_free(canonfile);
		if (gen_fits) produce_fits(file_prefix, aborted);
		return GP_OK;
	}
示例#11
0
bool photo_camera::photo_camera_capture_to_file( std::string filename )
{
  int fd, error_code;
  CameraFile *photo_file;
  CameraFilePath photo_file_path;

  // NOP: This gets overridden in the library to /capt0000.jpg
  strcpy( photo_file_path.folder, "/");
  strcpy( photo_file_path.name, "foo.jpg");

  error_code = gp_camera_capture( camera_, GP_CAPTURE_IMAGE, &photo_file_path, context_ );
  if( error_code < GP_OK )
  {
    photo_reporter::error( "gp_camera_capture()" );
    gp_context_error( context_, "Could not capture image  (error code %d)\n", error_code );
    return false;
  }

  fd = open( filename.c_str(), O_CREAT|O_WRONLY, 0644 );
  error_code = gp_file_new_from_fd( &photo_file, fd );
  if( error_code < GP_OK )
  {
    photo_reporter::error( "gp_file_new_from_fd()" );
    gp_context_error( context_, "Could not create a new image file from %s%s (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    gp_file_free( photo_file );
    return false;
  }

  error_code = gp_camera_file_get( camera_, photo_file_path.folder, photo_file_path.name, GP_FILE_TYPE_NORMAL, photo_file, context_ );
  if( error_code < GP_OK )
  {
    photo_reporter::error( "gp_camera_file_get()" );
    gp_context_error( context_, "Could not get file %s%s (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    gp_file_free( photo_file );
    return false;
  }

  error_code = gp_camera_file_delete( camera_, photo_file_path.folder, photo_file_path.name, context_ );
  if( error_code < GP_OK )
  {
    photo_reporter::error( "gp_camera_file_delete()" );
    gp_context_error( context_, "Could delete file %s%s  (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    gp_file_free( photo_file );
    return false;
  }

  gp_file_free( photo_file );
  return true;
}
示例#12
0
bool GPCamera::deleteItem(const QString& folder, const QString& itemName)
{
#ifdef HAVE_GPHOTO2
    d->status->cancel = false;
    int errorCode     = gp_camera_file_delete(d->camera, QFile::encodeName(folder).constData(),
                                              QFile::encodeName(itemName).constData(),
                                              d->status->context);

    if (errorCode != GP_OK)
    {
        qCDebug(DIGIKAM_IMPORTUI_LOG) << "Failed to delete camera item!";
        printGphotoErrorDescription(errorCode);
        return false;
    }

    return true;
#else
    Q_UNUSED(folder);
    Q_UNUSED(itemName);
    return false;
#endif /* HAVE_GPHOTO2 */
}
示例#13
0
/**
 * Capture image, and store file in @field grabbedFrames.
 * Do not read a file. File will be deleted from camera automatically.
 */
bool DigitalCameraCapture::grabFrame()
{
    CameraFilePath filePath;
    CameraFile * file = NULL;
    try
    {
        CR(gp_file_new(&file));

        if (preview)
        {
            CR(gp_camera_capture_preview(camera, file, context));
        }
        else
        {
            // Capture an image
            CR(gp_camera_capture(camera, GP_CAPTURE_IMAGE, &filePath, context));
            CR(gp_camera_file_get(camera, filePath.folder, filePath.name, GP_FILE_TYPE_NORMAL,
                    file, context));
            CR(gp_camera_file_delete(camera, filePath.folder, filePath.name, context));
        }
        // State update
        if (firstCapturedFrameTime == 0)
        {
            firstCapturedFrameTime = time(0);
        }
        capturedFrames++;
        grabbedFrames.push_back(file);
    }
    catch (GPhoto2Exception & e)
    {
        if (file)
            gp_file_unref(file);
        message(WARNING, "cannot grab new frame", e);
        return false;
    }
    return true;
}
示例#14
0
bool photo_camera::photo_camera_capture( photo_image* image )
{
  int fd, error_code;
  CameraFile *photo_file;
  CameraFilePath photo_file_path;
  char temp_file_name[20];

  // NOP: This gets overridden in the library to /capt0000.jpg
  strcpy( photo_file_path.folder, "/" );
  strcpy( photo_file_path.name, "foo.jpg" );

  error_code = gp_camera_capture( camera_, GP_CAPTURE_IMAGE, &photo_file_path, context_ );
  if( error_code < GP_OK )
  {
    photo_reporter::error( "gp_camera_capture()" );
    gp_context_error( context_, "Could not capture image  (error code %d)\n", error_code );
    return false;
  }

  // create temporary file
  strcpy( temp_file_name, "tmpfileXXXXXX" );
  fd = mkstemp( temp_file_name );
  error_code = gp_file_new_from_fd( &photo_file, fd );
  if( error_code < GP_OK )
  {
    close( fd );
    unlink( temp_file_name );

    photo_reporter::error( "gp_file_new_from_fd()" );
    gp_context_error( context_, "Could not create a new image file from %s%s (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    gp_file_free( photo_file );
    return false;
  }

  // get image from camera and store in temporary file
  error_code = gp_camera_file_get( camera_, photo_file_path.folder, photo_file_path.name, GP_FILE_TYPE_NORMAL, photo_file, context_ );
  if( error_code < GP_OK )
  {
    gp_file_unref( photo_file );
    unlink( temp_file_name );
    photo_reporter::error( "gp_camera_file_get()" );
    gp_context_error( context_, "Could not get file %s%s (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    return false;
  }

  // delete image from camera's memory
  error_code = gp_camera_file_delete( camera_, photo_file_path.folder, photo_file_path.name, context_ );
  if( error_code < GP_OK )
  {
    unlink( temp_file_name );
    photo_reporter::error( "gp_camera_file_delete()" );
    gp_context_error( context_, "Could delete file %s%s  (error code %d)\n", photo_file_path.folder, photo_file_path.name, error_code );
    gp_file_free( photo_file );
    return false;
  }

  // load image from temporary file
  if( image->photo_image_read( std::string(temp_file_name) ) == true )
  {
    gp_file_free( photo_file );
    unlink( temp_file_name );
    return true;
  }

  photo_reporter::error( "photo_image_read()" );
  gp_file_free( photo_file );
  unlink( temp_file_name );
  return false;
}
bool photoController::capture(const char *filename) 
{
	if(checkCameraDetection() == false)
	{
		DEBUG_PRINTF(V_WARNING, "No camera detected.\n");
		return false;
	}
	int fd, retval;
	CameraFile *file;
	CameraFilePath camera_file_path;
	START_CHRONOMETER();
	DEBUG_PRINTF(V_MESSAGE, "Deleting old files.\n");
	retval = gp_camera_folder_delete_all(camera, "/", context);
	if(retval != GP_OK) // Error.
	{
		DEBUG_PRINTF(V_WARNING, "ERROR: Couldn't delete old files in camera memory. Code: %d\n", retval);
		return false;
	}
	DEBUG_PRINTF(V_MESSAGE, "Camera capture.\n");

	retval = gp_camera_capture(camera, GP_CAPTURE_IMAGE, &camera_file_path, context);
	if(retval != GP_OK) // Error.
	{
		if(retval == GP_ERROR_NOT_SUPPORTED)
			DEBUG_PRINTF(V_WARNING, "ERROR: This camera can not capture.\n");
		else
		{
			DEBUG_PRINTF(V_WARNING, "ERROR: Unexpected gp_camera_capture return. Code: %d\n", retval);
			releaseCamera(&camera, context);
			camera_detected = false;
			initCamera();
		}
		return false;
	}
	DEBUG_PRINTF(V_MESSAGE, "camera_file_path.folder %s!\n", camera_file_path.folder);
	DEBUG_PRINTF(V_MESSAGE, "Open %s!\n", filename);
	fd = open(filename, O_CREAT | O_WRONLY, 0644);
	if(fd < 0)  // Error.
	{
		DEBUG_PRINTF(V_WARNING, "Error opening file: %s!\n", strerror(errno));
		return false;
	}
	DEBUG_PRINTF(V_MESSAGE, "Create new CameraFile object from a file descriptor FD: %d.\n", fd);
	retval = gp_file_new_from_fd(&file, fd);
	if(retval != GP_OK) // Error.
	{
		DEBUG_PRINTF(V_WARNING, "ERROR: Unexpected gp_file_new_from_fd return. Code: %d\n", retval);
		gp_file_free(file);
		close(fd);
		return false;
	}
	if(checkCameraDetection() == false)
	{
		DEBUG_PRINTF(V_WARNING, "No camera detected 2.\n");
		gp_file_free(file);
		close(fd);
		return false;
	}
	DEBUG_PRINTF(V_MESSAGE, "Copy file from camera.\n");
	retval = gp_camera_file_get(camera, camera_file_path.folder, camera_file_path.name, GP_FILE_TYPE_NORMAL, file, context);
	if(retval != GP_OK) // Error.
	{
		if(retval == GP_ERROR_DIRECTORY_NOT_FOUND)
			DEBUG_PRINTF(V_WARNING, "Photo directory not found.\n");
		else if(retval == GP_ERROR_FILE_NOT_FOUND)
			DEBUG_PRINTF(V_WARNING, "Photo file name not found.\n");
		else
			DEBUG_PRINTF(V_WARNING, "ERROR: Unexpected gp_camera_file_get return. Code: %d\n", retval);
		gp_file_free(file);
		close(fd);
		return false;
	}
	if(checkCameraDetection() == false)
	{
		DEBUG_PRINTF(V_WARNING, "No camera detected 3.\n");
		gp_file_free(file);
		close(fd);
		return false;
	}
	DEBUG_PRINTF(V_MESSAGE, "Delete file from camera.\n");
	retval = gp_camera_file_delete(camera, camera_file_path.folder, camera_file_path.name, context);
	if(retval != GP_OK) // Error.
	{
		DEBUG_PRINTF(V_WARNING, "ERROR: Unexpected gp_camera_file_delete return. Code: %d\n", retval);
		gp_file_free(file);
		close(fd);
		return false;
	}

	DEBUG_PRINTF(V_MESSAGE, "Free CameraFile object.\n");
	gp_file_unref(file);
	close(fd);

	cv::Mat raw = cv::imread(filename);
	if(raw.data == NULL)
	{
		DEBUG_PRINTF(V_WARNING, "ERROR: OpenCV failed to open image file.\n");
		return false;
	}
	cv::Size raw_size = raw.size();
	DEBUG_PRINTF(V_MESSAGE, "capture() cv::Mat total=%u width=%d height=%d refcount=%d\n", raw.total(), raw_size.width, raw_size.height, (int)(void*)raw.refcount);
	crop(raw);
	raw.release();

	STOP_CHRONOMETER("Capture");
	return true;
}