方块糖的工坊
方块糖的工坊

linux下opencv读取图片并存储到mysql数据库中

弄了好久的一段代码,ide用的eclipse,数据库连接不会的自己去找吧,网上蛮多的;功能虽然不是很强,但还是蛮有借鉴意义的。

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <mysql.h>  
  5. #include <iostream>  
  6.   
  7. #include <string>  
  8.   
  9. #include <opencv/cv.h>  
  10. #include <opencv/highgui.h>  
  11. using namespace std;  
  12.   
  13. string IntToStr(int num)  
  14. {  
  15.     stringstream ss;  
  16.     ss.clear();  
  17.     ss<<num;  
  18.     return ss.str();  
  19. }  
  20.   
  21. int main()  
  22. {  
  23.     //定义数据库连接信息  
  24.     const char user[] = “root”;         //username  
  25.     const char pswd[] = “phancie”;         //password  
  26.     const char host[] = “localhost”;    //or”127.0.0.1″  
  27.     const char table[] = “phancie_vision”;        //database  
  28.     unsigned int port = 3306;           //server port  
  29.     MYSQL myCont;  
  30.     MYSQL_RES *result;  
  31.     //MYSQL_ROW sql_row;  
  32.     //MYSQL_FIELD *fd;  
  33.     //char column[32][32];  
  34.   
  35.   
  36.     mysql_init(&myCont);  
  37.     if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0))  
  38.     {  
  39.         cout<<“connect succeed!”<<endl;  
  40.         //设置编码格式,否则在cmd下无法显示中文  
  41.         mysql_query(&myCont, “SET NAMES GBK”);  
  42.   
  43.   
  44.         int image_id = 1;  
  45.         char filename[256];  
  46.         //IplImage *image=0;  
  47.         sprintf(filename , “/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg” , image_id );  
  48.         IplImage* src = cvLoadImage(filename);  
  49.         cout<<“\n开始存储图片信息 \n”;  
  50.   
  51.         while(src)  
  52.         {  
  53.             int orgExists = 1;  
  54.             int orgWidth = src->width;  
  55.             int orgHeight = src->height;  
  56.             int orgDepth = src->depth;  
  57.             int orgWidthStep = src->widthStep;  
  58.             int thumbExists = 0;  
  59.             string path = “/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/”+IntToStr(image_id)+“.jpg”;  
  60.             const char* orgRelativaPath = path.c_str();  
  61.   
  62.             //定义数据库操作语句  
  63.             //将图片信息插入数据库中  
  64.             string orgsql = “insert into phvImages(orgExists,orgWidth,orgHeigth,orgDepth,orgRelativePath,orgWidthStep,thumbExists)”;  
  65.             orgsql += ” values(“+IntToStr(orgExists)+“,”+IntToStr(orgWidth)+“,”+IntToStr(orgHeight)+“,”+IntToStr(orgDepth)+“,'”+orgRelativaPath+“‘,”+IntToStr(orgWidthStep)+“,”+IntToStr(thumbExists)+“)”;  
  66.             //将std::string类型转换为const char*  
  67.             const char *sql_insert = orgsql.c_str();  
  68.             cout<<sql_insert<<endl;  
  69.   
  70.             int res = mysql_query(&myCont,sql_insert);  
  71.             if(!res)  
  72.             {  
  73.                 printf(“插入成功,受影响行数:%lu\n”,(ulong)mysql_affected_rows(&myCont));  
  74.             }  
  75.             else  
  76.             {  
  77.                 cout<<“query sql failed!”<<endl;  
  78.             }  
  79.   
  80.   
  81.             image_id++;  
  82.             sprintf(filename , “/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg” , image_id );  
  83.             src= cvLoadImage(filename);  
  84.         }  
  85.   
  86.   
  87.   
  88.   
  89.     }  
  90.     else  
  91.     {  
  92.         cout<<“connect failed!”<<endl;  
  93.     }  
  94.     if(result!=NULL) mysql_free_result(result);//释放结果资源  
  95.     mysql_close(&myCont);//断开连接  
  96.     return 0;  
  97.   
来自为知笔记(Wiz)
没有标签
首页      心得体会      linux下opencv读取图片并存储到mysql数据库中

发表回复

textsms
account_circle
email

方块糖的工坊

linux下opencv读取图片并存储到mysql数据库中
弄了好久的一段代码,ide用的eclipse,数据库连接不会的自己去找吧,网上蛮多的;功能虽然不是很强,但还是蛮有借鉴意义的。 #include <stdio.h>   #include &…
扫描二维码继续阅读
2015-05-19