最近在程式中遇到要把圖片的二進位制string存成圖片到本地,其實就是寫檔案
structCameraInfo tmpCameraInfo; string strData = tmpCalibrateReq.images(i).data(); string strCameraName = tmpCalibrateReq.images(i).camera_name(); QFile file; QString strImgPath = qApp->applicationDirPath() + "/" + QString::fromStdString(strCameraName) + ".jpg"; file.setFileName(strImgPath); if(file.open(QIODevice::WriteOnly)) { //QByteArray tmpByteArray = QString::fromStdString(strData).toLatin1();這種寫法是錯誤的
QByteArray tmpByteArray(strData.c_str(), strData.length());//這才是正確的 file.write(tmpByteArray); file.close(); }
https://cloud.tencent.com/developer/ask/32018
這個連結解釋的很清楚