从mySQL数据库中的blob列中获取图像尺寸

问题描述 投票:0回答:0
类型,我使用此代码读取列。当前,Blob图像转换为

char *array //Get the total number of fields int fieldCount = mysql_num_fields(result); //Get field information of a row of data MYSQL_FIELD *fields = mysql_fetch_fields(result); while (m_row = mysql_fetch_row(result)) { for (int i = 0;i < fieldCount; ++i) { if (fields[i].type == MYSQL_TYPE_BLOB) { unsigned long length = mysql_fetch_lengths(result)[i]; char* buffer = new char[length + 1]; memset(buffer, 0x00, sizeof(buffer)); memcpy(buffer, m_row[i], length); } } }

为了在图像上进行一些测试,我应该知道图像维度
没有在磁盘上编写图像并再次读取图像?
从MySQL数据库中读取数据的方法是:

res = stmt->executeQuery("MY QUERY TO DATABASE"); while (res->next()) { std::istream *blobData = res->getBlob("image"); std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData); std::string blobString = std::string(isb, std::istreambuf_iterator<char>()); tempFR.image = blobString; blobData->seekg(0, ios::end); tempFR.imageSize = blobData->tellg(); std::istream *blobIn; char buffer[tempFR.imageSize]; memset(buffer, '\0', tempFR.imageSize); blobIn = res->getBlob("image"); blobIn->read((char*)buffer, tempFR.imageSize); }

notice

Imagesize
length

是整体图像大小,例如1000.

update#1:同时将其写入磁盘的同时如何重建图像? 在第一种情况下,可以通过以下代码将blob_image写入磁盘: stringstream pic_name; pic_name << "car.jpeg"; ofstream outfile(pic_name.str(), ios::binary); outfile.write(buffer, length);

在第二个: std::ofstream outfile ("car.jpeg",std::ofstream::binary); outfile.write (buffer, tempFR.imageSize); outfile.close();

在两种情况下,图像都写入磁盘正确。但是我想找到

图像尺寸
而不将其写入磁盘并再次阅读

通过解码缓冲图像:

length = mysql_fetch_lengths(result)[i]; buffer = new char[length + 1]; memset(buffer, 0x00, sizeof(buffer)); memcpy(buffer, m_row[i], length); matImg = cv::imdecode(cv::Mat(1, length, CV_8UC1, buffer), cv::IMREAD_UNCHANGED); 首先,将数组复制到缓冲区,然后将其转换为cv::Mat,最后对其进行解码。它将是一个图像。

c++ mysql image blob
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.