⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ptpcam.c

📁 Linux平台。一个好用的ptp传输工具。用来从相机向pc传输照片
💻 C
📖 第 1 页 / 共 4 页
字号:
	PTP_USB ptp_usb;	PTPContainer event;	int ExposureTime=0;	struct usb_device *dev;	short ret;	printf("\nInitiating captue...\n");	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	if (!ptp_operation_issupported(&params, PTP_OC_InitiateCapture))	{	    printf ("Your camera does not support InitiateCapture operation!\nSorry, blame the %s!\n", params.deviceinfo.Manufacturer);	    goto out;	}	/* obtain exposure time in miliseconds */	if (ptp_property_issupported(&params, PTP_DPC_ExposureTime))	{	    PTPDevicePropDesc dpd;	    memset(&dpd,0,sizeof(dpd));	    ret=ptp_getdevicepropdesc(&params,PTP_DPC_ExposureTime,&dpd);	    if (ret==PTP_RC_OK) ExposureTime=(*(int32_t*)(dpd.CurrentValue))/10;	}	/* adjust USB timeout */	if (ExposureTime>USB_TIMEOUT) ptpcam_usb_timeout=ExposureTime;	CR(ptp_initiatecapture (&params, 0x0, 0), "Could not capture.\n");		ret=ptp_usb_event_wait(&params,&event);	if (ret!=PTP_RC_OK) goto err;	if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret);	if (event.Code==PTP_EC_CaptureComplete) {		printf ("Camera reported 'capture completed' but the object information is missing.\n");		goto out;	}			while (event.Code==PTP_EC_ObjectAdded) {		printf ("Object added 0x%08lx\n", (long unsigned) event.Param1);		if (ptp_usb_event_wait(&params, &event)!=PTP_RC_OK)			goto err;		if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret);		if (event.Code==PTP_EC_CaptureComplete) {			printf ("Capture completed successfully!\n");			goto out;		}	}	err:	printf("Events receiving error. Capture status unknown.\n");out:	ptpcam_usb_timeout=USB_TIMEOUT;	close_camera(&ptp_usb, &params, dev);}voidloop_capture (int busn, int devn, short force, int n,  int overwrite){	PTPParams params;	PTP_USB ptp_usb;	PTPContainer event;	struct usb_device *dev;	int file;	PTPObjectInfo oi;	uint32_t handle=0;	char *image;	int ret;	char *filename;	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	/* capture timeout should be longer */	ptpcam_usb_timeout=USB_CAPTURE_TIMEOUT;	printf("Camera: %s\n",params.deviceinfo.Model);	/* local loop */	while (n>0) {		/* capture */		printf("\nInitiating captue...\n");		CR(ptp_initiatecapture (&params, 0x0, 0),"Could not capture\n");		n--;		ret=ptp_usb_event_wait(&params,&event);		if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret);		if (ret!=PTP_RC_OK) goto err;		if (event.Code==PTP_EC_CaptureComplete) {			printf ("CANNOT DOWNLOAD: got 'capture completed' but the object information is missing.\n");			goto out;		}					while (event.Code==PTP_EC_ObjectAdded) {			printf ("Object added 0x%08lx\n",(long unsigned) event.Param1);			handle=event.Param1;			if (ptp_usb_event_wait(&params, &event)!=PTP_RC_OK)				goto err;			if (verbose) printf ("Event received %08x, ret=%x\n", event.Code, ret);			if (event.Code==PTP_EC_CaptureComplete)				goto download;		}download:			memset(&oi, 0, sizeof(PTPObjectInfo));		if (verbose) printf ("Downloading: 0x%08lx\n",(long unsigned) handle);		if ((ret=ptp_getobjectinfo(&params,handle, &oi))!=PTP_RC_OK){			fprintf(stderr,"ERROR: Could not get object info\n");			ptp_perror(&params,ret);			if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb);			continue;		}			if (oi.ObjectFormat == PTP_OFC_Association)				goto out;		filename=(oi.Filename);		file=open(filename, (overwrite==OVERWRITE_EXISTING?0:O_EXCL)|O_RDWR|O_CREAT|O_TRUNC,S_IRWXU|S_IRGRP);		if (file==-1) {			if (errno==EEXIST) {				printf("Skipping file: \"%s\", file exists!\n",filename);				goto out;			}			perror("open");			goto out;		}		lseek(file,oi.ObjectCompressedSize-1,SEEK_SET);		ret=write(file,"",1);		if (ret==-1) {			perror("write");			goto out;		}		image=mmap(0,oi.ObjectCompressedSize,PROT_READ|PROT_WRITE,MAP_SHARED,			file,0);		if (image==MAP_FAILED) {			perror("mmap");			close(file);			goto out;		}		printf ("Saving file: \"%s\" ",filename);		fflush(NULL);		ret=ptp_getobject(&params,handle,&image);		munmap(image,oi.ObjectCompressedSize);		close(file);		if (ret!=PTP_RC_OK) {			printf ("error!\n");			ptp_perror(&params,ret);			if (ret==PTP_ERROR_IO) clear_stall(&ptp_usb);		} else {			/* and delete from camera! */			printf("is done...\nDeleting from camera.\n");			CR(ptp_deleteobject(&params, handle,0),					"Could not delete object\n");			printf("Object 0x%08lx (%s) deleted.\n",(long unsigned) handle, oi.Filename);		}out:		;	}err:	ptpcam_usb_timeout=USB_TIMEOUT;	close_camera(&ptp_usb, &params, dev);}voidnikon_initiate_dc (int busn, int devn, short force){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t result;    	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	printf("Camera: %s\n",params.deviceinfo.Model);	printf("\nInitiating direct captue...\n");		if (params.deviceinfo.VendorExtensionID!=PTP_VENDOR_NIKON)	{	    printf ("Your camera is not Nikon!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	if (!ptp_operation_issupported(&params,PTP_OC_NIKON_DirectCapture)) {	    printf ("Sorry, your camera dows not support Nikon DirectCapture!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	/* perform direct capture */	result=ptp_nikon_directcapture (&params, 0xffffffff);	if (result!=PTP_RC_OK) {	    ptp_perror(&params,result);	    fprintf(stderr,"ERROR: Could not capture.\n");	    if (result!=PTP_RC_StoreFull) {		close_camera(&ptp_usb, &params, dev);		return;	    }	}	usleep(300*1000);out:		close_camera(&ptp_usb, &params, dev);}voidnikon_direct_capture (int busn, int devn, short force, char* filename,int overwrite){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t result;	uint16_t nevent=0;	PTPUSBEventContainer* events=NULL;	int ExposureTime=0;	/* exposure time in miliseconds */	int BurstNumber=1;	PTPDevicePropDesc dpd;	PTPObjectInfo oi;	int i;    	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	printf("Camera: %s\n",params.deviceinfo.Model);	if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))==PTP_RC_OK) {	    if (filename==NULL) filename=oi.Filename;	    save_object(&params, 0xffff0001, filename, oi, overwrite);	    goto out;	}	printf("\nInitiating direct captue...\n");		if (params.deviceinfo.VendorExtensionID!=PTP_VENDOR_NIKON)	{	    printf ("Your camera is not Nikon!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	if (!ptp_operation_issupported(&params,PTP_OC_NIKON_DirectCapture)) {	    printf ("Sorry, your camera dows not support Nikon DirectCapture!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	/* obtain exposure time in miliseconds */	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_ExposureTime,&dpd);	if (result==PTP_RC_OK) ExposureTime=(*(int32_t*)(dpd.CurrentValue))/10;	/* obtain burst number */	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_BurstNumber,&dpd);	if (result==PTP_RC_OK) BurstNumber=*(uint16_t*)(dpd.CurrentValue);/*	if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))==PTP_RC_OK)	{	    if (filename==NULL) filename=oi.Filename;	    save_object(&params, 0xffff0001, filename, oi, overwrite);	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	}*/	/* perform direct capture */	result=ptp_nikon_directcapture (&params, 0xffffffff);	if (result!=PTP_RC_OK) {	    ptp_perror(&params,result);	    fprintf(stderr,"ERROR: Could not capture.\n");	    if (result!=PTP_RC_StoreFull) {		close_camera(&ptp_usb, &params, dev);		return;	    }	}	if (BurstNumber>1) printf("Capturing %i frames in burst.\n",BurstNumber);	/* sleep in case of exposure longer than 1/100 */	if (ExposureTime>10) {	    printf ("sleeping %i miliseconds\n", ExposureTime);	    usleep (ExposureTime*1000);	}	while (BurstNumber>0) {#if 0	    /* Is this really needed??? */	    ptp_nikon_keepalive(&params);#endif	    result=ptp_nikon_checkevent (&params, &events, &nevent);	    if (result != PTP_RC_OK) {		fprintf(stderr, "Error checking Nikon events\n");		ptp_perror(&params,result);		goto out;	    }	    for(i=0;i<nevent;i++) {	    ptp_nikon_keepalive(&params);		void *prop;		if (events[i].code==PTP_EC_DevicePropChanged) {		    printf ("Checking: %s\n", ptp_prop_getname(&params, events[i].param1));		    ptp_getdevicepropvalue(&params, events[i].param1, &prop, PTP_DTC_UINT64);		}		printf("Event [%i] = 0x%04x,\t param: %08x\n",i, events[i].code ,events[i].param1);		if (events[i].code==PTP_EC_NIKON_CaptureOverflow) {		    printf("Ram cache overflow? Shooting to fast!\n");		    if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))!=PTP_RC_OK) {		        fprintf(stderr, "Could not get object info\n");		        ptp_perror(&params,result);		        goto out;		    }		    if (filename==NULL) filename=oi.Filename;		    save_object(&params, 0xffff0001, filename, oi, overwrite);		    BurstNumber=0;		    usleep(100);		} else		if (events[i].code==PTP_EC_NIKON_ObjectReady) 		{		    if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))!=PTP_RC_OK) {		        fprintf(stderr, "Could not get object info\n");		        ptp_perror(&params,result);		        goto out;		    }		    if (filename==NULL) filename=oi.Filename;		    save_object(&params, 0xffff0001, filename, oi, overwrite);		    BurstNumber--;		}	    }	    free (events);	}out:		ptpcam_usb_timeout=USB_TIMEOUT;	close_camera(&ptp_usb, &params, dev);}voidnikon_direct_capture2 (int busn, int devn, short force, char* filename, int overwrite){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t result;	PTPObjectInfo oi;	dev=find_device(busn,devn,force);	if (dev==NULL) {		fprintf(stderr,"could not find any device matching given "		"bus/dev numbers\n");		exit(-1);	}	find_endpoints(dev,&ptp_usb.inep,&ptp_usb.outep,&ptp_usb.intep);	init_ptp_usb(&params, &ptp_usb, dev);	if (ptp_opensession(&params,1)!=PTP_RC_OK) {		fprintf(stderr,"ERROR: Could not open session!\n");		close_usb(&ptp_usb, dev);		return ;	}/*	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_BurstNumber,&dpd);	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_ExposureTime,&dpd);*/	/* perform direct capture */	result=ptp_nikon_directcapture (&params, 0xffffffff);	if (result!=PTP_RC_OK) {	    ptp_perror(&params,result);	    fprintf(stderr,"ERROR: Could not capture.\n");	    if (result!=PTP_RC_StoreFull) {	        close_camera(&ptp_usb, &params, dev);	        return;	    }	}	if (ptp_closesession(&params)!=PTP_RC_OK)	{	    fprintf(stderr,"ERROR: Could not close session!\n");	    return;	}	usleep(300*1000);	if (ptp_opensession(&params,1)!=PTP_RC_OK) {    		fprintf(stderr,"ERROR: Could not open session!\n");    		close_usb(&ptp_usb, dev);    		return;    	}loop:    	if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))==PTP_RC_OK) {    	    if (filename==NULL) filename=oi.Filename;    	    save_object(&params, 0xffff0001, filename, oi, overwrite);    	} else {	    ptp_nikon_keepalive(&params);	    goto loop;	}/*out:	*/	close_camera(&ptp_usb, &params, dev);#if 0	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	uint16_t result;	uint16_t nevent=0;	PTPUSBEventContainer* events=NULL;	int ExposureTime=0;	/* exposure time in miliseconds */	int BurstNumber=1;	PTPDevicePropDesc dpd;	PTPObjectInfo oi;	int i;	char *filename=NULL;    	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	printf("Camera: %s\n",params.deviceinfo.Model);	printf("\nInitiating direct captue...\n");		if (params.deviceinfo.VendorExtensionID!=PTP_VENDOR_NIKON)	{	    printf ("Your camera is not Nikon!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	if (!ptp_operation_issupported(&params,PTP_OC_NIKON_DirectCapture)) {	    printf ("Sorry, your camera dows not support Nikon DirectCapture!\nDo not buy from %s!\n",params.deviceinfo.Manufacturer);	    goto out;	}	/* obtain exposure time in miliseconds */	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_ExposureTime,&dpd);	if (result==PTP_RC_OK) ExposureTime=(*(int32_t*)(dpd.CurrentValue))/10;	/* obtain burst number */	memset(&dpd,0,sizeof(dpd));	result=ptp_getdevicepropdesc(&params,PTP_DPC_BurstNumber,&dpd);	if (result==PTP_RC_OK) BurstNumber=*(uint16_t*)(dpd.CurrentValue);	if (BurstNumber>1) printf("Capturing %i frames in burst.\n",BurstNumber);#if 0	/* sleep in case of exposure longer than 1/100 */	if (ExposureTime>10) {	    printf ("sleeping %i miliseconds\n", ExposureTime);	    usleep (ExposureTime*1000);	}#endif	while (num>0) {	    /* perform direct capture */	    result=ptp_nikon_directcapture (&params, 0xffffffff);	    if (result!=PTP_RC_OK) {	        if (result==PTP_ERROR_IO) {	    	close_camera(&ptp_usb, &params, dev);	    	return;	        }	    }#if 0	    /* Is this really needed??? */	    ptp_nikon_keepalive(&params);#endif	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	    ptp_nikon_keepalive(&params);	    result=ptp_nikon_checkevent (&params, &events, &nevent);	    if (result != PTP_RC_OK) goto out;	    	    for(i=0;i<nevent;i++) {		printf("Event [%i] = 0x%04x,\t param: %08x\n",i, events[i].code ,events[i].param1);		if (events[i].code==PTP_EC_NIKON_ObjectReady) 		{		    num--;		    if ((result=ptp_getobjectinfo(&params,0xffff0001, &oi))!=PTP_RC_OK) {		        fprintf(stderr, "Could not get object info\n");		        ptp_perror(&params,result);		        goto out;		    }		    if (filename==NULL) filename=oi.Filename;		    save_object(&params, 0xffff0001, filename, oi, overwrite);		}		if (events[i].code==PTP_EC_NIKON_CaptureOverflow) {		    printf("Ram cache overflow, capture terminated\n");		    //BurstNumber=0;		}	    }	    free (events);	}out:		ptpcam_usb_timeout=USB_TIMEOUT;	close_camera(&ptp_usb, &params, dev);#endif}voidlist_files (int busn, int devn, short force){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	int i;	PTPObjectInfo oi;	struct tm *tm;	printf("\nListing files...\n");	if (open_camera(busn, devn, force, &ptp_usb, &params, &dev)<0)		return;	printf("Camera: %s\n",params.deviceinfo.Model);	CR(ptp_getobjecthandles (&params,0xffffffff, 0x000000, 0x000000,		&params.handles),"Could not get object handles\n");	printf("Handler:           Size: \tCaptured:      \tname:\n");	for (i = 0; i < params.handles.n; i++) {		CR(ptp_getobjectinfo(&params,params.handles.Handler[i],			&oi),"Could not get object info\n");		if (oi.ObjectFormat == PTP_OFC_Association)			continue;		tm=gmtime(&oi.CaptureDate);		printf("0x%08lx: %12u\t%4i-%02i-%02i %02i:%02i\t%s\n",

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -