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

📄 ptpcanon.c

📁 linux下佳能数码相机使用源码
💻 C
📖 第 1 页 / 共 3 页
字号:
	printf("\n");	printf("The property is ");	if (dpd.GetSet==PTP_DPGS_Get)		printf ("read only");	else		printf ("settable");	switch (dpd.FormFlag) {	case PTP_DPFF_Enumeration:		printf (", enumerated. Allowed values are:\n");		{			int i;			for(i=0;i<dpd.FORM.Enum.NumberOfValues;i++){				PRINT_PROPVAL_HEX(				dpd.FORM.Enum.SupportedValue[i]);				printf("\n");			}		}		break;	case PTP_DPFF_Range:		printf (", within range:\n");		PRINT_PROPVAL_DEC(dpd.FORM.Range.MinimumValue);		printf(" - ");		PRINT_PROPVAL_DEC(dpd.FORM.Range.MaximumValue);		printf("; step size: ");		PRINT_PROPVAL_DEC(dpd.FORM.Range.StepSize);		printf("\n");		break;	case PTP_DPFF_None:		printf(".\n");	}	if (value) {		printf("Setting property value to '%s'\n",value);		CRE(set_property(&params, property, value, dpd.DataType));	}	ptp_free_devicepropdesc(&dpd);	CR(ptp_closesession(&params), "Could not close session!\n"	"Try to reset the camera.\n");	usb_release_interface(ptp_usb.handle,		dev->config->interface->altsetting->bInterfaceNumber);}/* Added by Nikolai Kopanygin *//*    0. Sets up a connection with the camera.    1. Turns the shooting mode on.    2. Turns the viewfinder on.    3. Sets the minimum zoom.    4. Takes 3 viewfinder shots and saves them in the current directory.    5. Turns the viewfinder off.    6. Takes a photo an saves it in the current directory.    7. Increases zoom.    8. Repeats steps 3 - 6.    9. Turns the shooting mode off.    */voidtest_canon (int busn,int devn,int force){	PTPParams params;	PTP_USB ptp_usb;	struct usb_device *dev;	PTPDevicePropDesc dpd;    PTPUSBEventContainer event;    PTPContainer evc;    int isevent;    uint32_t entnum;    int i,k;        FILE *f;    uint16_t zoom;    uint16_t val16;    uint32_t dummy;    uint32_t val32;    uint16_t *pval16;    uint32_t *pval32;    char *str;    CR(connect_camera(busn, devn, force, &ptp_usb, &params, &dev),        "Could not connect camera!\n");    CR(enter_shooting_mode(&params),        "Could not enter shooting mode!\n");    for (zoom=0; zoom<3; zoom++) {        uint32_t handle;        char name[17];        char *image;        uint32_t size;        uint32_t dummy;        uint16_t res;        uint32_t bsize=5000;        uint32_t nblocks;         uint32_t tail;  /* remainder */        uint32_t pos;        uint8_t flash;	        /* setting flash off */	flash=0;        printf("Setting Flash mode to %d\n",flash);        CRE(ptp_setdevicepropvalue(&params, PTP_DPC_CANON_FlashMode, &flash, PTP_DTC_UINT8));	        /* setting zoom */		printf("Setting Zoom value to %d\n",zoom);        CRE(res = ptp_setdevicepropvalue(&params, PTP_DPC_CANON_Zoom, &zoom, PTP_DTC_UINT16));        if (res==PTP_RC_OK) {            uint16_t *props=NULL;            uint32_t propnum;            uint16_t *pval16;            int i;                        printf("Let's see what properties changed.\n");            CRE(res=ptp_canon_getchanges(&params, &props, &propnum));            if (res==PTP_RC_OK) {                for (i=0;(i<propnum)&&(i<20) ;i++) {                    printf("changed 0x%4X\n",props[i]);                }                if (propnum>20)                     printf("........\n");            }            free(props);            pval16=NULL;            CRE(res=ptp_getdevicepropvalue(&params, PTP_DPC_CANON_Zoom, (void **)&pval16, PTP_DTC_UINT16));            if (res==PTP_RC_OK)                printf("Zoom is actually set to %d\n",*pval16);            free(pval16);         }        /* turn viewfinder on */        printf("Turning viewfinder on\n");        CRE(res = ptp_canon_viewfinderon (&params));        if (res==PTP_RC_OK) {            int i;            /* take 3 VF shots */            for (i=0; i<3; i++) {                image=NULL;                CRE(res=ptp_canon_getviewfinderimage (&params, &image, &size));                if (res==PTP_RC_OK) {                    sprintf(name,"canon_vf_%d_%d.jpg",zoom,i);                    f = fopen(name, "w");                    if (f) {                        fwrite(image,1,size,f);                        fclose(f);                        printf("VF shot %s written\n",name);                    }                }                free(image);                usleep(500000);             }            /* turn viewfinder off */                    printf("Turning viewfinder off\n");            CRE(ptp_canon_viewfinderoff (&params));        }                        /* take a "real" shot */         val16=3;		CRE(ptp_setdevicepropvalue(&params, PTP_DPC_CANON_D029, &val16, PTP_DTC_UINT16));        printf("Initiating capture...\n");        CRE(res=ptp_canon_initiatecaptureinmemory (&params));        if (res!=PTP_RC_OK)            continue;                /* Catch event */        if (PTP_RC_OK==(val16=ptp_usb_event_wait (&params, &evc))) {            if (evc.Code==PTP_EC_CaptureComplete)                printf("Event: capture complete. \n");            else                printf("Event: 0x%X\n", evc.Code);                    } else            printf("No event yet, we'll try later.\n");        printf("Checking events in stack.\n");        for (i=0;i<100;i++) {            CRE(res=ptp_canon_checkevent(&params,&event,&isevent));            if (res!=PTP_RC_OK)                continue;            if (isevent  &&                 (event.type==PTP_USB_CONTAINER_EVENT) &&                (event.code==PTP_EC_CANON_RequestObjectTransfer) ) {                    handle=event.param1;                    printf("PTP_EC_CANON_RequestObjectTransfer, object handle=0x%X. \n",event.param1);                    printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                        event.length,event.type,event.code,event.trans_id,                        event.param1, event.param2, event.param3);                    int j;                    for (j=0;j<2;j++) {                        CRE(res=ptp_canon_checkevent(&params,&event,&isevent));                        if ((res==PTP_RC_OK) && isevent)                            printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                                event.length,event.type,event.code,event.trans_id,                                event.param1, event.param2, event.param3);                    }                    break;                }        }        /* Catch event, attempt  2 */        if (val16!=PTP_RC_OK) {            if (PTP_RC_OK==ptp_usb_event_wait (&params, &evc)) {                if (evc.Code==PTP_EC_CaptureComplete)                    printf("Event: capture complete. \n");                else                    printf("Event: 0x%X\n", evc.Code);            } else                printf("No expected capture complete event\n");        }                if (i==100)            fprintf(stderr,"ERROR: Capture timed out!\n");                else            download_object(&params, handle, name);    } /* next zoom */    printf("Test passed\n");    CR(exit_shooting_mode(&params),        "Could not exit shooting mode!\n");    CR(disconnect_camera(&ptp_usb, &params, dev),        "Disconnecting failed!\n");}uint16_t connect_camera(int busn, int devn, uint16_t force, PTP_USB *ptp_usb, PTPParams *params, struct usb_device **dev) {#ifdef DEBUG	printf("dev %i\tbus %i\n",devn,busn);#endif	*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);    printf("Opening session\n");	CR2(ptp_opensession (params,1),		"Could not open session!\nTry to reset the camera.\n");    return PTP_RC_OK;}uint16_t disconnect_camera(PTP_USB *ptp_usb, PTPParams *params, struct usb_device *dev){	CR2(ptp_closesession(params), "Could not close session!\n"	"Try to reset the camera.\n");	usb_release_interface(ptp_usb->handle,		dev->config->interface->altsetting->bInterfaceNumber);    return PTP_RC_OK;}uint16_t enter_shooting_mode(PTPParams *params){    PTPStorageIDs sids;    PTPStorageInfo sinfo;    PTPUSBEventContainer event;    PTPContainer evc;    int isevent;    int i,k;        uint16_t val16;    uint16_t *pval16;    uint32_t *pval32;    sids.Storage=NULL;    sinfo.StorageDescription=NULL;    sinfo.VolumeLabel=NULL;    printf("Getting storage IDs\n");    free(sids.Storage);    CR2(ptp_getstorageids (params, &sids),        "Could not get storage IDs.\n");    printf("Magic code begins.\n");    CRE2(ptp_getdevicepropvalue(params, 0xd045, (void **)&pval16, PTP_DTC_UINT16));    printf("prop 0xd045 value is 0x%4X\n",*pval16);    free(pval16);    val16=1;    CRE2(ptp_setdevicepropvalue(params, 0xd045, &val16, PTP_DTC_UINT16));    CRE2(ptp_getdevicepropvalue(params, 0xd02e, (void **)&pval32, PTP_DTC_UINT32));    printf("prop 0xd02e value is 0x%4X\n",*pval32);    free(pval32);    CRE2(ptp_getdevicepropvalue(params, 0xd02f, (void **)&pval32, PTP_DTC_UINT32));    printf("prop 0xd02f value is 0x%4X\n",*pval32);    free(pval32);	CR2(ptp_getdeviceinfo (params, &params->deviceinfo),		"Could not get device info\nTry to reset the camera.\n");	CR2(ptp_getdeviceinfo (params, &params->deviceinfo),		"Could not get device info\nTry to reset the camera.\n");    CRE2(ptp_getdevicepropvalue(params, 0xd02e, (void **)&pval32, PTP_DTC_UINT32));    printf("prop 0xd02e value is 0x%4X\n",*pval32);    free(pval32);    CRE2(ptp_getdevicepropvalue(params, 0xd02f, (void **)&pval32, PTP_DTC_UINT32));    printf("prop 0xd02f value is 0x%4X\n",*pval32);    free(pval32);	CR2(ptp_getdeviceinfo (params, &params->deviceinfo),		"Could not get device info\nTry to reset the camera.\n");    CRE2(ptp_getdevicepropvalue(params, 0xd045, (void **)&pval16, PTP_DTC_UINT16));    printf("prop 0xd045 value is 0x%4X\n",*pval16);    free(pval16);    printf("Magic code ends.\n");            printf("Setting prop. 0xd045 to 4\n");    val16=4;    CR2(ptp_setdevicepropvalue(params, PTP_DPC_CANON_D045, &val16, PTP_DTC_UINT16),        "Could not set prop. 0xd045 to 4.\n");            printf("Getting storage IDs\n");    free(sids.Storage);    CR2(ptp_getstorageids (params, &sids),        "Could not get storage IDs.\n");    printf("sids.n=%d\n",sids.n);    for (k=0; k<sids.n; k++) {        printf("Getting Storage 0x%X description.\n",sids.Storage[k]);        free(sinfo.StorageDescription);        free(sinfo.VolumeLabel);        CR2(ptp_getstorageinfo (params, sids.Storage[k],&sinfo),            "Could not get storage info.\n");        printf("Volume label is '%s'\n",sinfo.VolumeLabel);        }    free(sids.Storage);    printf("Entering shooting mode\n");    CR2(ptp_canon_startshootingmode (params),        "Could not start shooting mode.\n");    /* Catch event */    if (PTP_RC_OK==(val16=ptp_usb_event_wait (params, &evc))) {        if (evc.Code==PTP_EC_StorageInfoChanged)            printf("Event: entering  shooting mode. \n");        else             printf("Event: 0x%X\n", evc.Code);    } else {        printf("No event yet, we'll try later.\n");    }        /* Emptying event stack */    for (i=0;i<2;i++) {        CRE2(ptp_canon_checkevent(params,&event,&isevent));        if (isevent)            printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                event.length,event.type,event.code,event.trans_id,                event.param1, event.param2, event.param3);    }    /* Catch event, attempt  2 */    if (val16!=PTP_RC_OK) {        if (PTP_RC_OK==ptp_usb_event_wait (params, &evc)) {            if (evc.Code==PTP_EC_StorageInfoChanged)                printf("Event: entering  shooting mode.\n");            else                printf("Event: 0x%X\n", evc.Code);        } else            printf("No expected mode change event.\n");    }    return PTP_RC_OK;}uint16_t exit_shooting_mode(PTPParams *params) {    PTPUSBEventContainer event;    PTPContainer evc;    int isevent;    int i;        uint16_t val16;    uint16_t *pval16;    uint32_t *pval32;    CR2(ptp_canon_endshootingmode (params),        "Could not exit shooting mode!\n");    /* Catch event */    if (PTP_RC_OK==(val16=ptp_usb_event_wait (params, &evc))) {        if (evc.Code==PTP_EC_StorageInfoChanged)            printf("Event: exiting shooting mode. \n");        else             printf("Event: 0x%X\n", evc.Code);    } else        printf("No event yet, we'll try later.\n");    /* Emptying event stack */    for (i=0;i<2;i++) {        CRE2(ptp_canon_checkevent(params,&event,&isevent));        if (isevent)            printf("evdata: L=0x%X, T=0x%X, C=0x%X, trans_id=0x%X, p1=0x%X, p2=0x%X, p3=0x%X\n",                event.length,event.type,event.code,event.trans_id,                event.param1, event.param2, event.param3);    }

⌨️ 快捷键说明

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