我是 C 新手。我正在嘗試加載存盤在.binsql 資料庫檔案中的 blob 影像。該.bin檔案以字串的形式存盤 blob 影像,如BM6ect(每個.bin都有 3 到 4 個字符)。
這是將 blob影像(.bmp)保存在 db中的代碼
QPixmap p;
p.load(filename);
string a = "/root/QtApplication_1/file";
string b = boost::lexical_cast<std::string>(counter);
string c = ".bmp";
p.save(("/root/QtApplication_1/file" boost::lexical_cast<std::string>(counter) ".bmp").c_str(),"BMP");
std::string d = a b c ;
std::ifstream blob_file;
blob_file.open(d.c_str(), std::ios_base::binary | std::ifstream::in);
driver = get_driver_instance();
con = driver->connect("localhost","","");
con->setSchema("BitmapImagesSchema");
prep_stmt = con->prepareStatement("Insert into bitmapImagesTable(`ID`,`ImageDir`,`ImagesBitMap`) values(?,?,?)");
prep_stmt->setInt(1,counter);
//
prep_stmt->setString(2,d.c_str());
// byte *p = "" ;
prep_stmt->setBlob(3,&blob_file);
prep_stmt->executeQuery();
delete prep_stmt;
這是我的代碼(不作業),我試圖從 db 獲取 blob 影像并使用 pixmap 在 QgraphicsView 中顯示
driver = get_driver_instance();
con = driver->connect("localhost","","");
con->setSchema("BitmapImagesSchema");
stmt = con-> createStatement();
std::istream *blobData;
res = stmt->executeQuery("select `ImagesBitMap` from bitmapImagesTable where `ID`='" boost::lexical_cast<std::string>(counter) "'order by `ID` DESC");
while(res->last()){
blobData = res->getBlob("ImagesBitMap");
break;
}
std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
std::string blobString = std::string(isb,std::istreambuf_iterator<char>());
const char * image = blobString.c_str();
blobData->seekg(0,ios::end);
size_t imagesize = blobData->tellg();
cout<<"aaa="<<image<<"\n";
QPixmap p;
p.load(image);
if(!widget.graphicsView_2->scene()){
QGraphicsScene *scene = new QGraphicsScene(this);
widget.graphicsView_2->setScene(scene);
}
widget.graphicsView_2->scene()->addPixmap(p);
delete res;
delete stmt;
delete con;
使用 pixmap 的圖形視圖在執行上述代碼的按鈕單擊時不顯示 blob 影像。我從這個
這是變數的image輸出
cout<<"aaa="<<image<<"\n";

如何使用上面的代碼顯示 blob 影像?
uj5u.com熱心網友回復:
試試這個代碼
driver = get_driver_instance();
con = driver->connect("localhost","","");
con->setSchema("BitmapImagesSchema");
stmt = con-> createStatement();
std::istream *blobData;
res = stmt->executeQuery("select `ImagesBitMap` from bitmapImagesTable where `ID`='" boost::lexical_cast<std::string>(counter) "'order by `ID` DESC");
while(res->last()){
blobData = res->getBlob("ImagesBitMap");
break;
}
std::istreambuf_iterator<char> isb = std::istreambuf_iterator<char>(*blobData);
std::string blobString = std::string(isb,std::istreambuf_iterator<char>());
const char * image = blobString.c_str();
blobData->seekg(0,ios::end);
size_t imagesize = blobData->tellg();
cout<<"aaa="<<image<<"size_t="<<imagesize<<"\n";
QPixmap p;
p.loadFromData(( const uchar*)image,imagesize);
if(!widget.graphicsView_2->scene()){
QGraphicsScene *scene = new QGraphicsScene(this);
widget.graphicsView_2->setScene(scene);
}
widget.graphicsView_2->scene()->addPixmap(p);
delete res;
delete stmt;
delete con;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/509839.html
