c语言读取图片
1. c语言读取图片的函数是那些
#include <graphics.h>
int main()
{
int gdriver, gmode;
gdriver=VGA;
gmode=VGAHI;
initgraph(&gdriver, &gmode, "c:\\tc");
bar3d(100, 100, 300, 250, 50, 1); /*画一长方体*/
getch();
closegraph();
return 0;
}
有时编程者并不知道所用的图形显示器适配器种类, 或者需要将编写的程序 用于不同图形驱动器, Turbo C提供了一个自动检测显示器硬件的函数, 其调用
格式为:
void far detectgraph(int *gdriver, *gmode);
其中gdriver和gmode的意义与上面相同。
例5. 自动进行硬件测试后进行图形初始化
#include <graphics.h>
int main()
{
int gdriver, gmode;
detectgraph(&gdriver, &gmode); /*自动测试硬件*/
printf("the graphics driver is %d, mode is %d\n", gdriver, gmode); /*输出测试结果*/
getch();
initgraph(&gdriver, &gmode, "c:\\tc");
/* 根据测试结果初始化图形*/
bar3d(10, 10, 130, 250, 20, 1);
getch();
closegraph();
return 0;
}
上例程序中先对图形显示器自动检测, 然后再用图形初始化函数进行初始化设置, 但Turbo C提供了一种更简单的方法, 即用gdriver= DETECT 语句后再跟 initgraph()函数就行了。采用这种方法后, 上例可改为:
例6.
#include <graphics.h>
int main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\tc");
bar3d(50, 50, 150, 30, 1);
getch();
closegraph();
return 0;
}
另外, Turbo C提供了退出图形状态的函数closegraph(), 其调用格式为:void far closegraph(void);调用该函数后可退出图形状态而进入文本方式(Turbo C 默认方式), 并释放用于保存图形驱动程序和字体的系统内存。
2. 用C语言怎么读取BMP格式的图片
没有标准函数读。
需要姿誉根据誉首BMP文件的结构定义,读出头部和每个颜色庆册数值。
struct
header
{
unsigned
short
int
bfType;
unsigned
int
bfSize;
unsigned
short
int
bfReserved1;
unsigned
short
int
bfReserved2;
unsigned
int
bfoffBits;
}__attribute__
((packed));
struct
tinfoheader
{
unsigned
int
biSize;
unsigned
int
biWidth;
unsigned
int
biHeight;
unsigned
short
int
biPlanes;
unsigned
short
int
biBitCount;
unsigned
int
biCompression;
unsigned
int
biSizeImage;
unsigned
int
biXPelsPerMeter;
unsigned
int
biYPelsPerMeter;
unsigned
int
biClrUsed;
unsigned
int
biClrImportant;
}__attribute__
((packed));
3. 用c语言如何读取和保存jpg图片文件
有这样一个二进制文件,大小在100M左右,里面存放了多张JPG图片,文件格式如下:
无关数据区
JPG图片数据区
无关数据区
JPG图片数据区
无关数据区
JPG图片数据区
......
已知JPG图片起始标志为:“FF
D8
FF
E0
00
10
4A”,结束标志为:“FF
D9”。
现想把这些JPG图片数据从该文件中读出来,生成一个个单独的图片文件,该如何做呢?
帮你写了个程序,
测试了只含有一个图片信息的文件
int
len=0;
int
filesn=0;
void
ReadJpg(CFile
*sfp,const
char
*dstfile,unsigned
char
a,unsigned
char
b)
{
CFile
nf;
nf.Open(dstfile,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary);
unsigned
t=0,t1=0;
nf.Write(&a,sizeof(char));
nf.Write(&b,sizeof(char));
while(1)
{
sfp->Read(&t,sizeof(char));
len++;
nf.Write(&t,sizeof(char));
if(t==0xFF)
{
sfp->Read(&t1,sizeof(char));
len++;
if(t1==0xD9)
break;
else
nf.Write(&t1,sizeof(char));
}
}
nf.Close();
}
调用:
void
test()
{
CFile
fp;
fp.Open("86b05621.jpg",CFile::modeRead|CFile::typeBinary);//文件名改一下
unsigned
char
a=0,b=0;
len=0;
while(1)
{
fp.Read(&a,sizeof(char));
len++;
if(len>=fp.GetLength())
break;
if(a==0xFF)
{
fp.Read(&b,sizeof(char));
len++;
if(b==0xD8)
{
filesn++;
char
filename[256];
sprintf(filename,"Jpg
%d.jpg",filesn);
ReadJpg(&fp,filename,a,b);
}
}
}
fp.Close();
}
思路是,先得到FF的值,然后再判断下一位是否是0xD8,
但这里要说明一下,如果你的其它数据信息里也含有FF
D8值的话就会出错了,
所以为保险起见,应该再多比较几位,
JPG的头是固定的,再往下就是EF
E0
00
1E,
建议楼主再往下比较四位比较保险(在程序中稍作修改即可)
你是不是在
分析某个
游戏的或程序的资源文件呀,如果是的话,这个资源文件应该配有一个
索引文件!或在其文件自身。索引可以列表索引,链表形示……
如果是你自已生成的文件包,最好加上索引,因为这样可以提高效率!
4. c语言,怎样读取一个BMP图片
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改变图像亮度
typedef struct BMP
{
//14字节
unsigned short bfType; //文件标识 2字节 必须为BM
unsigned int bfSize; //文件大小 4字节
unsigned short bfReserved1; //保留,每字节以"00"填写 2字节
unsigned short bfReserved2; //同上 2字节
unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节
//40字节
unsigned int biSize; //表示本结构的大小 4字节
int biWidth; //位图的宽度 4字节
int biHeight; //位图的高度 4字节
unsigned short biPlanes; //永远为1 , 2字节
unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节
unsigned int biCompression; //压缩说明 4字节
unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节
int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节
int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节
unsigned int biClrUsed; //位图使用的颜色索引数 4字节
unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//读取图像信息
fseek(file,0L,0); //读取图像文件类型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存图像
void image_save(FILE *file)
{
int times=3; //输入文件名次数。
char bmp_name[10]; //文件名
//int i; //记录数据区个数
printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//写文件头
printf("\n%s",bmp_name);
fseek(file,0L,0); //图像文件类型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //图像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //图像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //图像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//数据区的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件头结构大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//图像的宽度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//图像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//图像的面数
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//图像一个像素的字节数
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//图像压缩信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//图像数据区的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平分辨率
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直分辨率
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//颜色索引数
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要颜色索引数
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改变图像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{
if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //创建一幅24位BMP图像文件。
//{
//main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已经分配了动态内存,但是没有释放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
5. 如何使用C语言实现JPEG图像格式的读取与写入
1.图片也是属于文件类型的一种,图片属于二进制文件。使用fopen函数的二进制模式“rb”就可以打开。
2.例程:
#include<stdlib.h>
#include<stdio.h>
intmain()
{
FILE*fpPhoto,*fpText,*fpTarget;
intiRead;
charszBuf[100];
printf("请输入第一个文件名(bmp): ");
gets(szBuf);
fpPhoto=fopen(szBuf,"rb");
printf("请输入第二个文件名(txt): ");
gets(szBuf);
fpText=fopen(szBuf,"rb");
printf("请输入目的文件名(bmp): ");
gets(szBuf);
fpTarget=fopen(szBuf,"wb");
if(!fpPhoto||!fpText||!fpTarget)
{
printf("打开文件失败! ");
system("pause");
return-1;
}
while((iRead=fread(szBuf,1,sizeof(szBuf),fpPhoto))>0)
fwrite(szBuf,1,iRead,fpTarget);
while((iRead=fread(szBuf,1,sizeof(szBuf),fpText))>0)
fwrite(szBuf,1,iRead,fpTarget);
fclose(fpPhoto);
fclose(fpText);
fclose(fpTarget);
return0;
}
6. 用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 ",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d== ",pFile);
printf("%d ",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
(6)c语言读取图片扩展阅读:
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] = '