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); */ }
static void liveview(Camera *canon, GPContext *canoncontext) { int retval; CameraFile *file; char output_file[64]; retval = gp_file_new(&file); if (retval != GP_OK) { printf(" Retval: %d\n", retval); exit(1); } retval = gp_camera_capture_preview(canon, file, canoncontext); if (retval != GP_OK) { printf(" Retval: %d\n", retval); exit(1); } sprintf(output_file, "/dev/shm/liveviewpre.jpg"); retval = gp_file_save(file, output_file); if (retval != GP_OK) { printf(" Retval: %d\n", retval); exit(1); } gp_file_unref(file); }
void data_serv_working_loop(t_serv_comm *s) { CameraFile *file; const char *data; char *header; unsigned long size; int len = 0; struct timeval time_beggining; struct timeval time_final; FD_ZERO(&(s->rd_fds)); FD_SET(s->sock_serv, &(s->rd_fds)); if (s->sock_serv > s->bigger_fd) s->bigger_fd = s->sock_serv; if (select((s->bigger_fd + 1), &(s->rd_fds), NULL, NULL, NULL) == -1) fprintf(stderr, "Select Error"); if (FD_ISSET(s->sock_serv, &(s->rd_fds))) serv_accept_new_connections_data(s); gp_file_new(&file); while (1) { gettimeofday(&time_beggining, NULL); if (!s->c->liveview) { if (len != 0) gp_camera_exit(s->c->camera, s->c->context); pthread_mutex_lock(&s->c->liveview_mutex); pthread_cond_wait(&s->c->liveview_condvar, &s->c->liveview_mutex); pthread_mutex_unlock(&s->c->liveview_mutex); } gp_camera_capture_preview(s->c->camera, file, s->c->context); gp_file_get_data_and_size(file, &data, &size); len = asprintf(&header, "||>>%li>>||", size); gettimeofday(&time_final, NULL); check_time(s->c, &time_beggining, &time_final); if (write(s->first_client->sock, header, len) < 0) { s->c->liveview = 0; gp_camera_exit(s->c->camera, s->c->context); client_diconnected(s, s->first_client->sock); return; } if (write(s->first_client->sock, data, size) < 0) { s->c->liveview = 0; gp_camera_exit(s->c->camera, s->c->context); client_diconnected(s, s->first_client->sock); return; } free(header); } }
CAMLprim value caml_gp_camera_capture_preview(value vcam, value vcon) { CAMLparam2(vcam, vcon); Camera *camera = Camera_val(vcam); GPContext *context = Context_val(vcon); CameraFile *file; gp_file_new(&file); gp_camera_capture_preview(camera, file, context); CAMLreturn(encapsulate_pointer(file)); }
void* capture(void* arg) { int res; int i = 0; CameraFile* file; pthread_cleanup_push(cleanup, NULL); while(!global->stop) { unsigned long int xsize; const char* xdata; pthread_mutex_lock(&control_mutex); res = gp_file_new(&file); CAMERA_CHECK_GP(res, "gp_file_new"); res = gp_camera_capture_preview(camera, file, context); CAMERA_CHECK_GP(res, "gp_camera_capture_preview"); pthread_mutex_lock(&global->in[plugin_id].db); res = gp_file_get_data_and_size(file, &xdata, &xsize); if(xsize == 0) { if(i++ > 3) { IPRINT("Restarted too many times; giving up\n"); return NULL; } int value = 0; IPRINT("Read 0 bytes from camera; restarting it\n"); camera_set("capture", &value); sleep(3); value = 1; camera_set("capture", &value); } else i = 0; CAMERA_CHECK_GP(res, "gp_file_get_data_and_size"); memcpy(global->in[plugin_id].buf, xdata, xsize); res = gp_file_unref(file); pthread_mutex_unlock(&control_mutex); CAMERA_CHECK_GP(res, "gp_file_unref"); global->in[plugin_id].size = xsize; DBG("Read %d bytes from camera.\n", global->in[plugin_id].size); pthread_cond_broadcast(&global->in[plugin_id].db_update); pthread_mutex_unlock(&global->in[plugin_id].db); usleep(delay); } pthread_cleanup_pop(1); return NULL; }
std::vector<char> Camera::preview() { std::lock_guard<std::mutex> g(mutex); auto file = GP_NEW_UNIQUE(CameraFile, gp_file); int ret; if ((ret = gp_camera_capture_preview(camera, file.get(), ctx->context)) < GP_OK) throw Exception("gp_camera_capture_preview", ret); const char *rawbuf; size_t nbytes; if ((ret = gp_file_get_data_and_size(file.get(), &rawbuf, &nbytes)) < GP_OK) throw Exception("gp_file_get_data_and_size", ret); std::vector<char> buf(nbytes); std::copy(rawbuf, rawbuf + nbytes, &buf[0]); return buf; }
bool GPCamera::getPreview(QImage& preview) { #ifdef HAVE_GPHOTO2 int errorCode; CameraFile* cfile = 0; const char* data = 0; unsigned long int size; d->status->cancel = false; gp_file_new(&cfile); errorCode = gp_camera_capture_preview(d->camera, cfile, d->status->context); if (errorCode != GP_OK) { qCDebug(DIGIKAM_IMPORTUI_LOG) << "Failed to initialize camera preview mode!"; printGphotoErrorDescription(errorCode); gp_file_unref(cfile); return false; } errorCode = gp_file_get_data_and_size(cfile, &data, &size); if (errorCode != GP_OK) { qCDebug(DIGIKAM_IMPORTUI_LOG) << "Failed to get preview from camera!"; printGphotoErrorDescription(errorCode); gp_file_unref(cfile); return false; } preview.loadFromData((const uchar*) data, (uint) size); gp_file_unref(cfile); return true; #else Q_UNUSED(preview); return false; #endif /* HAVE_GPHOTO2 */ }
/** * 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; }
void GPhotoCameraWorker::capturePreview() { openCamera(); if (m_status != QCamera::ActiveStatus) { setStatus(QCamera::StartingStatus); } gp_file_clean(m_file); int ret = gp_camera_capture_preview(m_camera, m_file, m_context); if (GP_OK == ret) { const char* data; unsigned long int size = 0; ret = gp_file_get_data_and_size(m_file, &data, &size); if (GP_OK == ret) { m_capturingFailCount = 0; const QImage &result = QImage::fromData(QByteArray(data, int(size))); setStatus(QCamera::ActiveStatus); emit previewCaptured(result); return; } } qWarning() << "Failed retrieving preview" << ret; ++m_capturingFailCount; if (m_capturingFailCount >= capturingFailLimit) { qWarning() << "Closing camera because of capturing fail"; emit error(QCamera::CameraError, tr("Unable to capture frame")); setStatus(QCamera::UnloadedStatus); closeCamera(); } }
static Bonobo_Stream impl_GNOME_Camera_capturePreview (PortableServer_Servant servant, CORBA_Environment *ev) { BonoboStream *stream; CameraFile *file; GnoCamCamera *c; g_message ("impl_GNOME_Camera_capturePreview"); c = GNOCAM_CAMERA (bonobo_object_from_servant (servant)); CR (gp_file_new (&file), ev); CR (gp_camera_capture_preview (c->camera, file), ev); if (BONOBO_EX (ev)) { gp_file_unref (file); g_message ("Returning..."); return (CORBA_OBJECT_NIL); } stream = bonobo_stream_mem_create (file->data, file->size, TRUE, FALSE); gp_file_unref (file); return (CORBA_Object_duplicate (BONOBO_OBJREF (stream), ev)); }
static void _camera_process_job(const dt_camctl_t *c,const dt_camera_t *camera, gpointer job) { dt_camera_t *cam=(dt_camera_t *)camera; _camctl_camera_job_t *j = (_camctl_camera_job_t *)job; switch( j->type ) { case _JOB_TYPE_EXECUTE_CAPTURE: { dt_print (DT_DEBUG_CAMCTL,"[camera_control] executing remote camera capture job\n"); CameraFilePath fp; int res=GP_OK; if( (res = gp_camera_capture (camera->gpcam, GP_CAPTURE_IMAGE,&fp, c->gpcontext)) == GP_OK ) { CameraFile *destination; const char *output_path = _dispatch_request_image_path(c,camera); if( !output_path ) output_path="/tmp"; const char *fname = _dispatch_request_image_filename(c,fp.name,cam); if( !fname ) fname=fp.name; char *output = g_build_filename (output_path,fname,(char *)NULL); int handle = open (output, O_CREAT | O_WRONLY,0666); gp_file_new_from_fd (&destination , handle); gp_camera_file_get (camera->gpcam, fp.folder , fp.name, GP_FILE_TYPE_NORMAL, destination, c->gpcontext); close (handle); // Notify listerners of captured image _dispatch_camera_image_downloaded (c,camera,output); g_free (output); } else dt_print (DT_DEBUG_CAMCTL,"[camera_control] capture job failed to capture image: %s\n",gp_result_as_string(res)); } break; case _JOB_TYPE_EXECUTE_LIVE_VIEW: { CameraFile *fp = NULL; int res = GP_OK; const gchar* data = NULL; unsigned long int data_size = 0; gp_file_new(&fp); if( (res = gp_camera_capture_preview (cam->gpcam, fp, c->gpcontext)) != GP_OK ) { dt_print (DT_DEBUG_CAMCTL,"[camera_control] live view failed to capture preview: %s\n", gp_result_as_string(res)); } else if( (res = gp_file_get_data_and_size(fp, &data, &data_size)) != GP_OK ) { dt_print (DT_DEBUG_CAMCTL,"[camera_control] live view failed to get preview data: %s\n", gp_result_as_string(res)); } else { // everything worked GdkPixbufLoader *loader = gdk_pixbuf_loader_new(); if(gdk_pixbuf_loader_write(loader, (guchar*)data, data_size, NULL) == TRUE) { dt_pthread_mutex_lock(&cam->live_view_pixbuf_mutex); if(cam->live_view_pixbuf != NULL) g_object_unref(cam->live_view_pixbuf); cam->live_view_pixbuf = gdk_pixbuf_loader_get_pixbuf(loader); dt_pthread_mutex_unlock(&cam->live_view_pixbuf_mutex); } gdk_pixbuf_loader_close(loader, NULL); } if(fp) gp_file_free(fp); dt_pthread_mutex_unlock(&cam->live_view_synch); dt_control_queue_redraw_center(); } break; case _JOB_TYPE_SET_PROPERTY: { _camctl_camera_set_property_job_t *spj=(_camctl_camera_set_property_job_t *)job; dt_print(DT_DEBUG_CAMCTL,"[camera_control] executing set camera config job %s=%s\n",spj->name,spj->value); CameraWidget *config; // Copy of camera configuration CameraWidget *widget; gp_camera_get_config( cam->gpcam, &config, c->gpcontext ); if( gp_widget_get_child_by_name ( config, spj->name, &widget) == GP_OK) { gp_widget_set_value ( widget , spj->value); gp_camera_set_config( cam->gpcam, config, c->gpcontext ); } /* dt_pthread_mutex_lock( &cam->config_lock ); CameraWidget *widget; if( gp_widget_get_child_by_name ( camera->configuration, spj->name, &widget) == GP_OK) { gp_widget_set_value ( widget , spj->value); //gp_widget_set_changed( widget, 1 ); cam->config_changed=TRUE; } dt_pthread_mutex_unlock( &cam->config_lock);*/ } break; default: dt_print(DT_DEBUG_CAMCTL,"[camera_control] process of unknown job type %lx\n",(unsigned long int)j->type); break; } g_free(j); }
int main(int argc, char **argv) { Camera *canon; int i, retval; GPContext *canoncontext = sample_create_context(); gp_log_add_func(GP_LOG_ERROR, errordumper, 0); gp_camera_new(&canon); /* When I set GP_LOG_DEBUG instead of GP_LOG_ERROR above, I noticed that the * init function seems to traverse the entire filesystem on the camera. This * is partly why it takes so long. * (Marcus: the ptp2 driver does this by default currently.) */ printf("Camera init. Takes about 10 seconds.\n"); retval = gp_camera_init(canon, canoncontext); if (retval != GP_OK) { printf(" Retval: %d\n", retval); exit (1); } canon_enable_capture(canon, TRUE, canoncontext); retval = camera_eosviewfinder(canon,canoncontext,1); if (retval != GP_OK) { fprintf(stderr,"camera_eosviewfinder(1): %d\n", retval); exit(1); } /*set_capturetarget(canon, canoncontext);*/ printf("Taking 100 previews and saving them to snapshot-XXX.jpg ...\n"); for (i=0;i<100;i++) { CameraFile *file; char output_file[32]; fprintf(stderr,"preview %d\n", i); retval = gp_file_new(&file); if (retval != GP_OK) { fprintf(stderr,"gp_file_new: %d\n", retval); exit(1); } /* autofocus every 10 shots */ if (i%10 == 9) { camera_auto_focus (canon, canoncontext, 1); /* FIXME: wait a bit and/or poll events ? */ camera_auto_focus (canon, canoncontext, 0); } else { camera_manual_focus (canon, (i/10-5)/2, canoncontext); } #if 0 /* testcase for EOS zooming */ { char buf[20]; if (i<10) set_config_value_string (canon, "eoszoom", "5", canoncontext); sprintf(buf,"%d,%d",(i&0x1f)*64,(i>>5)*64); fprintf(stderr, "%d - %s\n", i, buf); set_config_value_string (canon, "eoszoomposition", buf, canoncontext); } #endif retval = gp_camera_capture_preview(canon, file, canoncontext); if (retval != GP_OK) { fprintf(stderr,"gp_camera_capture_preview(%d): %d\n", i, retval); exit(1); } sprintf(output_file, "snapshot-%03d.jpg", i); retval = gp_file_save(file, output_file); if (retval != GP_OK) { fprintf(stderr,"gp_camera_capture_preview(%d): %d\n", i, retval); exit(1); } gp_file_unref(file); /* sprintf(output_file, "image-%03d.jpg", i); capture_to_file(canon, canoncontext, output_file); */ } retval = camera_eosviewfinder(canon,canoncontext,0); if (retval != GP_OK) { fprintf(stderr,"camera_eosviewfinder(0): %d\n", retval); exit(1); } sleep(10); gp_camera_exit(canon, canoncontext); return 0; }