今天给各位分享c语言批量读取图片的知识,其中也会对如何用c语言读取一张图片进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
1、用c语言如何读取和保存jpg图片文件?2、如何在c语言中批量读取文件3、用C++如何读取图片4、请问如何使用纯C语言读取文件中的图片,并将图片存储在二维数组中?5、如何用c语言读取图片
用c语言如何读取和保存jpg图片文件?
#include stdio.h
#include stdlib.h
#include windows.h
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, “rb”);//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}
int main ()
{
int size=0;
size=file_size(“qw”);
printf(“%d\n”,size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen(“qw”,”r”);
pFile = fopen ( “qwe” , “wb” );
printf(“%d==\n”,pFile);
printf(“%d\n”,size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename(“qwe”,”Groot.jpg”);
return 0;
}
扩展资料:
c语言读取TXT文件:
#include stdio.h
#include stdlib.h
#include string.h
#define MAX_LINE 1024
int main()
{
char buf[MAX_LINE]; /*缓冲区*/
FILE *fp; /*文件指针*/
int len; /*行字符个数*/
if((fp = fopen(“test.txt”,”r”)) == NULL)
{
perror(“fail to read”);
exit (1) ;
}
while(fgets(buf,MAX_LINE,fp) != NULL)
{
len = strlen(buf);
buf[len-1] = ‘\0’; /*去掉换行符*/
printf(“%s %d \n”,buf,len – 1);
}
return 0;
}
如何在c语言中批量读取文件
应当使用findfirst和findnext函数:
#include
dos.h
#include
dir.h
int
iMatch;
struct
ffblk
fFileStru;
iMatch
=
findfirst(“c:\\test\\*.*”,
fFileStru,
0)
while(!iMatch)
{
………..//找到的文件信息保存在结构fpFileStru里面,这里是处理语句
iMatch
=
findnext(fFileStru)
}
用C++如何读取图片
实现的方法和详细的操作步骤如下:
1、第一步,在图片采样软件中找到软件快捷方式,然后单击以打开该软件,如下图所示,然后进入下一步。
2、其次,完成上述步骤后,已经进入了取模软件,单击“载入图片”按钮以添加图片,如下图所示,然后进入下一步。
3、接着,完成上述步骤后,选择要添加的图片,单击以选择图片,然后单击“打开”按钮,如下图所示,进入下一步。
4、然后,完成上述步骤后,单击“参数”按钮以进入图片参数设置界面,如下图所示,然后进入下一步。
5、随后,完成上述步骤后,根据实际情况进行相关参数的设置,如下图所示,然后进入下一步。
6、接着,完成上述步骤后,单击“参数确认”按钮,如下图所示,然后进入下一步。
7、然后,完成上述步骤后,单击“数据保存”按钮,如下图所示,然后进入下一步。
8、随后,完成上述步骤后,选择文件保存路径,然后单击“保存”按钮,如下图所示,然后进入下一步。
9、最后,完成上述步骤后,可以看到刚刚生成的图片C文件,只需将代码添加到C语言中即可,如下图所示。这样,问题就解决了。
请问如何使用纯C语言读取文件中的图片,并将图片存储在二维数组中?
1、使用双层循环语句,就可以依次把数据顺序读入到一个二维数组当中了。2、例程:#include#include#defineMAXLINE3#defineMAXCOLUMN10voidmain(void){FILE*fp;//文件指针chararr[MAXLINE][MAXCOLUMN]={0};//定义3行10列的二维数组并初始化inti=-1;if((fp=fopen(“./test/filename.txt”,”r”))==NULL){//打开txt文件perror(“Fileopenerror!\n”);return;}while((fgets(arr[++i],MAXCOLUMN+1,fp))!=NULL)//读取一行并存到arr数组printf(“%d:”,i);//打印行号//puts(arr[i]);char*subarr=strtok(arr[i],””);//以空格为分隔符从arr[i]中获得字串while(subarr!=NULL){data[i][j]=atoi(subarr);//将字串转为int型数据存入data数组printf(“%d\t”,data[i][j]);//打印data[i][jsubarr=strtok(NULL,””);//继续获得arr[i]中的字串j++;//data数组列加一}printf(“\n”);}//循环完毕后,所有数据已在data数组中printf(“\n”);fclose(fp);//关闭指针}
如何用c语言读取图片
#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *img, int w, int h)
{img = new unsigned char[w*h];}
void delateimage(unsigned char*img)
{delete []img;}
void readimage(unsigned char*img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(fp,fname, “rb”);
if (fp == NULL){ cout “error” endl; return; }
size_t result;
result=fread(img , sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout “Reading error” endl;
return;
}
else
cout “Reading Ok!” endl;
fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])
{
for (int i = 0; i for (int j = 0; j if (iw – 3 || jh – 3)
image1[Twoto1(i,j,w)] = 0;
else
{
float temp = 0;
for (int m = 0; m5; m++)
for (int n = 0; n5; n++)
{
temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);
}
if (temp255) image1[Twoto1(i, j, w)] = 255;
else if (temp0) image1[Twoto1(i, j, w)] = 0;
else image1[Twoto1(i, j, w)] = temp;
}
}
void saveimage(unsigned char *img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(fp, fname, “wb”);
if (fp == NULL) { cout “error” endl; return; }
size_t result;
result = fwrite(img, sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout “Write error” endl;
return;
}
else
cout “Write Ok!” endl;
fclose(fp);
}
void main()
{
unsigned char *img;
unsigned char *img1;
float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
//float moban[5][5] = { 0 };
int w = 512, h = 512;
createimage(img, w, h);
createimage(img1, w, h);
readimage(img, w, h, “E:\ss.raw”);
mobanjuanji(img, img1,w, h, moban);
saveimage(img, w, h, “E:\ss_1.raw”);
saveimage(img1, w, h, “E:\ss_2.raw”);
delateimage(img);
delateimage(img1);
}
扩展资料
C语言实现一个图片的读出和写入
#include stdlib.h
#include windows.h
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, “rb”);//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}
int main ()
{
int size=0;
size=file_size(“qw”);
printf(“%d\n”,size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen(“qw”,”r”);
pFile = fopen ( “qwe” , “wb” );
printf(“%d==\n”,pFile);
printf(“%d\n”,size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename(“qwe”,”Groot.jpg”);
return 0;
}
c语言批量读取图片的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于如何用c语言读取一张图片、c语言批量读取图片的信息别忘了在本站进行查找喔。