图像数据写入到文件
来源:互联网 发布:淘宝企业店铺公示期 编辑:程序博客网 时间:2024/04/29 18:10
本文主要对图像的data 数据写入到文件(包括文本格式,和二进制文件)
//首先在桌面建立一个CR_Result1文件夹static char* dumpRoot = "C:\\Users\\seven\\Desktop\\CR_Result1\\";static char file2open[1024];//保存为二进制的文件//filename 保存文件的名字// buf 要保存的数据// size 要保存文件的大小int dump_temp(char* filename, char* buf, long long size){ FILE* file; sprintf(file2open, "%s%s", dumpRoot, filename); file = fopen(file2open, "wb"); if(file == NULL){ LOGE("can't open file: %s\n", file2open); return -1; } fwrite(buf, 1, (size_t)size, file); fclose(file); return 0;}// 保存float 类型的图片数据// filename 如1.txt// buf 数据指针// mWidth 数据宽// mHeight 数据高int DumpFloat(char * filename, char * buf, int mWidth, int mHeight){ FILE* file; int w, h; float * temp = (float *)buf; sprintf(file2open, "%s%s", dumpRoot, filename); file = fopen(file2open, "w+"); //fwrite(temp, mWidth * mHeight * sizeof(float), 1, file); for(h = 0; h < mHeight; h ++) { for(w = 0; w < mWidth - 1; w ++) { fprintf_s(file, "%f,", temp[h * mWidth + w]); } fprintf_s(file, "%f\n", temp[h * mWidth + w]); } fclose(file); return 0;}// 保存unsigned char 类型数据 // 参数上同int DumpChar(char * filename, char * buf, int mWidth, int mHeight){ FILE* file; int w, h; unsigned char * temp = (unsigned char *)buf; sprintf(file2open, "%s%s", dumpRoot, filename); file = fopen(file2open, "w+"); for(h = 0; h < mHeight; h ++) { for(w = 0; w < mWidth - 1; w ++) { fprintf_s(file, "%u,", temp[h * mWidth + w]); } fprintf_s(file, "%u\n", temp[h * mWidth + w]); } fclose(file); return 0;}int DumpInt(char * filename, char * buf, int mWidth, int mHeight){ FILE* file; int w, h; int * temp = (int *)buf; sprintf(file2open, "%s%s", dumpRoot, filename); file = fopen(file2open, "w+"); for(h = 0; h < mHeight; h ++) { for(w = 0; w < mWidth - 1; w ++) { fprintf_s(file, "%d,", temp[h * mWidth + w]); } fprintf_s(file, "%d\n", temp[h * mWidth + w]); } fclose(file); return 0;}//文件名格式为xxx{W=4}{H=4}// 将文件读到内存float * ReadFromFileFloat(char * fileName){ char * prt; char * prtTemp; int width = 0; int height = 0; FILE * fp; float * tempFloatPrt = NULL; char * tempCharPrt = NULL; int lengthChar = 0; int ret = 0; int i, j; char fName[1024]; int fNameLength = strlen(fileName); memcpy(fName, fileName, fNameLength); fName[fNameLength] = '\0'; prt = fName; prt = strstr(prt, "{W="); prt = prt + strlen("{W="); prtTemp = strstr(prt, "}"); * prtTemp = '\0'; sscanf(prt, "%d", &width); * prtTemp = '}'; prt = fName; prt = strstr(prt, "{H="); prt = prt + strlen("{H="); prtTemp = strstr(prt, "}"); * prtTemp = '\0'; sscanf(prt, "%d", &height); * prtTemp = '}'; fp = fopen(fileName, "rb"); if(NULL == fp) { printf("fopen err \n"); } fseek(fp, 0L, SEEK_END); lengthChar = ftell(fp); fseek(fp, 0L, SEEK_SET); tempCharPrt = (char *)calloc(lengthChar, sizeof(char)); ret = fread(tempCharPrt, lengthChar, 1, fp); if(1 != ret){ printf("fread err \n");} fclose(fp); tempFloatPrt = (float *)calloc(width * height, sizeof(float)); prt = tempCharPrt; prtTemp = tempCharPrt; for(i = 0; i < height; i ++) { for(j = 0; j < width - 1; j ++) { prtTemp = strstr(prt, ","); *prtTemp = '\0'; sscanf(prt, "%f", &tempFloatPrt[i * width + j]); prt = prtTemp + 1; } prtTemp = strstr(prt, "\n"); *prtTemp = '\0'; sscanf(prt, "%f", &tempFloatPrt[i * width + j]); if(i != (height - 1)) { prt = prtTemp + 1; } } free(tempCharPrt); return tempFloatPrt;}//将文件数据读到内存#define OS_Android 0#define OS_Windows 1#define OS_XXX OS_Windowsint readData(const char *filename, char ** sources, int * fileLengths){ int fileLength; int ret; FILE *file = NULL;#if OS_XXX == OS_Android file = fopen(filename, "rb");#endif#if OS_XXX == OS_Windows fopen_s(&file, filename, "rb");#endif if (file == NULL) return -4; fseek(file, 0, SEEK_END); fileLength = ftell(file); *fileLengths = fileLength; *sources = (char*)malloc(fileLength + 1); rewind(file); ret = fread(*sources, fileLength, 1, file); (*sources)[fileLength] = '\0'; if (ret == 0) return -5; fclose(file); return 0;}
0 0
- 图像数据写入到文件
- 写入json数据到文件
- 将图像数据写入文件的函数
- 写入数据到MP3文件中
- [C] 将数据写入到文件
- java将数据写入到csv文件
- ios 将数据写入到文件
- 将数据写入到CSV文件
- c# 写入数据到excel文件
- opencv将图像数据写入二进制(.dat)文件
- 【python图像处理】txt文件数据的读取与写入
- 数据持久化 将数据写入到本地 文件管理
- C# - 数据备份-datatable数据写入到CSV文件中
- C语言文件读写操作,写入数据到文件
- opencv输出图像数据到txt文件
- 使用Java读取xlxs文件和写入txt文件,并将数据写入到本地文件
- 用C#将数据写入到Excel文件的方法
- 从网络里面获取数据写入到文件
- 开启慢查询
- svn版本管理文件列表
- oracle读书笔记-----PL/SQL编程
- Servlet生命周期与工作原理
- ABAP语法示例
- 图像数据写入到文件
- 解决ViewPager嵌套ViewPager滑动冲突问题
- sdau三 1013
- 第一篇、概述3~大型网站核心架构要素
- 简单易用的进度条 基于HTML5实现
- Qt之手动布局
- 初识kafka
- kafka-系列1
- swift学习笔记之泛型