1 CvPoint pt; 2 pt.x = point.x - text_size.width/2; //这样的设置为字体(左右)居中对齐 3 pt.y = point.y + (text_size.height+baseline)/2; //这样的设置为字体(上下)居中对齐 4 5 if(i==0) pt.y = (AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT)+10; //设置字体输出的具体位置 6 else if(i==portion) pt.y=AVIFRAME_COLORBAR_HEIGHT-5; 7 //Stage x 8 CvFont font; 9 cvInitFont(&font,CV_FONT_HERSHEY_DUPLEX ,1.0f,1.0f,0,1,CV_AA); 10 11 char text[20]; 12 sprintf(text,"Stage%d",nIndex); 13 CvSize text_size; 14 int baseline; 15 cvGetTextSize(text, &font, &text_size, &baseline); 16 cvPutText(aviFrame,text,cvPoint(20,text_size.height+baseline),&font, CV_RGB(255,0,0)); 17 18 //makeTilte 19 CvFont font; 20 cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX,0.35f,0.7f,0,1,CV_AA); 21 char* text="Major Strain[%]"; 22 CvSize text_size; 23 int baseline; 24 cvGetTextSize(text, &font, &text_size, &baseline); 25 cvPutText(colorbar,text,cvPoint(2,text_size.height+baseline),&font,CV_RGB(0,0,0)); 26 27 //写刻度值 28 double gap=(maxLimit-minLimit)/double(portion); 29 CvPoint pt; 30 char buffer[30]; 31 double value; 32 cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX ,0.5f,0.5f,0,1,CV_AA); 33 for(i=0;i<=portion;i++) 34 { 35 value = (maxLimit- i*gap)*100; 36 sprintf(buffer,"%4.2f",value); 37 pt.x=AVIFRAME_BAR_WIDTH+10; 38 if(i==0) pt.y = (AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT)+10; 39 else if(i==portion) pt.y=AVIFRAME_COLORBAR_HEIGHT-5; 40 else pt.y=i*AVIFRAME_BAR_HEIGHT/portion+(AVIFRAME_COLORBAR_HEIGHT-AVIFRAME_BAR_HEIGHT); 41 cvPutText(colorbar,buffer,pt,&font, CV_RGB(0,0,0)); 42 } 43 44 void CYnPlot::MakeLabelImage(IplImage** pImage,const char* text,double scale) 45 { 46 CvFont font; 47 cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,0.5f*scale,0.5f*scale,0,1,CV_AA); 48 49 CvSize text_size; 50 cvGetTextSize(text, &font, &text_size, NULL); 51 52 if (*pImage) cvReleaseImage(pImage); 53 *pImage = cvCreateImage(cvSize(text_size.width+5,text_size.height+10),8,3); 54 cvSet(*pImage,CV_RGB(255,255,255)); 55 cvPutText(*pImage,text,cvPoint(2,text_size.height+3),&font, CV_RGB(0,0,0)); 56 } 57 58 void CYnPlot::MakeTitleImage(IplImage** pImage,const char* text) 59 { 60 CvFont font; 61 cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,0.7f,0.7f,0,1,CV_AA); 62 63 CvSize text_size; 64 cvGetTextSize(text, &font, &text_size, NULL); 65 66 if (*pImage) cvReleaseImage(pImage); 67 *pImage = cvCreateImage(cvSize(text_size.width+5,text_size.height+10),8,3); 68 cvSet(*pImage,CV_RGB(255,255,255)); 69 cvPutText(*pImage,text,cvPoint(2,text_size.height+3),&font, CV_RGB(0,0,0)); 70 } 71 72 CvFont font; 73 CvSize text_size; 74 cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX ,0.35f,0.35f,0,1,CV_AA); 75 76 CvFont font; 77 double hscale = 0.2f*_pointSize; 78 cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,hscale,hscale,0,1,CV_AA); 79 80 CvSize text_size; 81 int baseline; 82 cvGetTextSize(text, &font, &text_size,&baseline); 83 CvPoint pt; 84 pt.x = point.x - text_size.width/2; 85 pt.y = point.y + (text_size.height+baseline)/2; 86 cvPutText(m_pPlotImage,text,pt,&font, color); 87 88 CvFont font; 89 double hscale = 0.2f*_pointSize; 90 cvInitFont(&font,CV_FONT_HERSHEY_TRIPLEX ,hscale,hscale,0,1,CV_AA); 91 92 CvSize text_size; 93 int baseline; 94 cvGetTextSize(text, &font, &text_size,&baseline); 95 CvPoint pt; 96 pt.x = point.x - text_size.width/2; 97 pt.y = point.y + (text_size.height+baseline)/2; 98 cvPutText(m_pPlotImage,text,pt,&font, color);
主要是cvGetTextSize(text, &font, &text_size, &baseline);
cvPoint(2,text_size.height+baseline)
这样写出的字才对齐得更好些。