linux下opencv读取图片并存储到mysql数据库中
2014-09-29 14:32
295 查看
弄了好久的一段代码,ide用的eclipse,数据库连接不会的自己去找吧,网上蛮多的;功能虽然不是很强,但还是蛮有借鉴意义的。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <mysql.h> #include <iostream> #include <string> #include <opencv/cv.h> #include <opencv/highgui.h> using namespace std; string IntToStr(int num) { stringstream ss; ss.clear(); ss<<num; return ss.str(); } int main() { //定义数据库连接信息 const char user[] = "root"; //username const char pswd[] = "phancie"; //password const char host[] = "localhost"; //or"127.0.0.1" const char table[] = "phancie_vision"; //database unsigned int port = 3306; //server port MYSQL myCont; MYSQL_RES *result; //MYSQL_ROW sql_row; //MYSQL_FIELD *fd; //char column[32][32]; mysql_init(&myCont); if(mysql_real_connect(&myCont,host,user,pswd,table,port,NULL,0)) { cout<<"connect succeed!"<<endl; //设置编码格式,否则在cmd下无法显示中文 mysql_query(&myCont, "SET NAMES GBK"); int image_id = 1; char filename[256]; //IplImage *image=0; sprintf(filename , "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg" , image_id ); IplImage* src = cvLoadImage(filename); cout<<"\n开始存储图片信息 \n"; while(src) { int orgExists = 1; int orgWidth = src->width; int orgHeight = src->height; int orgDepth = src->depth; int orgWidthStep = src->widthStep; int thumbExists = 0; string path = "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/"+IntToStr(image_id)+".jpg"; const char* orgRelativaPath = path.c_str(); //定义数据库操作语句 //将图片信息插入数据库中 string orgsql = "insert into phvImages(orgExists,orgWidth,orgHeigth,orgDepth,orgRelativePath,orgWidthStep,thumbExists)"; orgsql += " values("+IntToStr(orgExists)+","+IntToStr(orgWidth)+","+IntToStr(orgHeight)+","+IntToStr(orgDepth)+",'"+orgRelativaPath+"',"+IntToStr(orgWidthStep)+","+IntToStr(thumbExists)+")"; //将std::string类型转换为const char* const char *sql_insert = orgsql.c_str(); cout<<sql_insert<<endl; int res = mysql_query(&myCont,sql_insert); if(!res) { printf("插入成功,受影响行数:%lu\n",(ulong)mysql_affected_rows(&myCont)); } else { cout<<"query sql failed!"<<endl; } image_id++; sprintf(filename , "/phancie/phv_images/imgs_orgsrc/imgs_natural/imgs_people/wuqilong/%d.jpg" , image_id ); src= cvLoadImage(filename); } } else { cout<<"connect failed!"<<endl; } if(result!=NULL) mysql_free_result(result);//释放结果资源 mysql_close(&myCont);//断开连接 return 0; }
相关文章推荐
- Linux下Basler GigE相机拍图、opencv读取并存储的实现
- mysql数据库blob图片的存储和读取
- opencv实现对图片的批量读取、处理、存储
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- linux 环境下 c语言实现mysql数据库图片的存储以及多数据库直接的转存
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- Linux平台下用OpenCV读取文件夹内图片并显示
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- linux 环境下 c语言实现mysql数据库图片的存储以及多数据库直接的转存
- C# 向MySQL数据库存储及读取图片、音乐等文件
- 利用Opencv读取图片RGB值并存储在txt文件中
- 利用Opencv读取图片RGB值并存储在txt文件中
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果
- opencv读取彩色/灰度图片像素值并存储在本地文件中c++代码实例及运行结果