jpg图片的起始为FF D8,结束为FF D9。没细研究。
#include "stdio.h"
/***********************************************************************************
* fp: 源图片,jpg->bin数据
out: 输出图片的Ascii码
dstFile:最终生成的图片 bin数据->jpg
***********************************************************************************/
int main()
{
FILE *fp, *out,*dstFile;
long int size;
unsigned char ch,ch1,ch2;
unsigned char high,low;
long int i;
char pic_name[30];
for(i=0; i<30; i++)
{
pic_name = 0;
}
printf("\nPlease input the picture's name(such as vc.jpg)...\n");
scanf("%s", pic_name);
if((fp = fopen(pic_name, "rb")) == NULL)
{
printf("can't open the file\n");
return 0;
}
if((out = fopen("JpgToBin.txt", "wb+")) == NULL)
{
printf("can't open the txt file\n");
return 0;
}
if ((dstFile = fopen("Recv.jpg","wb+")) == NULL)
{
printf("can't open the dst file\n");
return 0;
}
fseek(fp,0,SEEK_END);
size = ftell(fp);
printf("size is %d\n", size);
rewind(fp);
for (i=0; i<size; i++)
{
ch = fgetc(fp);
high = ch >> 4;
low = ch & 0x0f;
if (high < 10)
high += '0';
else
high += 55;
if (low < 10)
low += '0';
else
low += 55;
fputc(high,out);
fputc(low, out);
fputc(' ',out);
if(i%16 == 0)
fprintf(out,"\r\n");
}
rewind(fp);
bool FormatRight = false;
for(i=0; i<size; i++)
{
ch1 = fgetc(fp);
if (ch1 == 0xff)
{
ch2 = fgetc(fp);
if (ch2 == 0xd8)
{
fputc(ch1,dstFile);
fputc(ch2,dstFile);
FormatRight = true;
break;
}
}
}
if (FormatRight)
{
for(; i<size; i++)
{
ch1 = fgetc(fp);
fputc(ch1,dstFile);
if (ch1 == 0xff)
{
ch2 = fgetc(fp);
if (ch2 == 0xd9)
break;
else
fputc(ch2,dstFile);
}
}
}
fclose(fp);
fclose(out);
fclose(dstFile);
printf("\n***********************************************************\n");
printf("Success!");
printf("\n***********************************************************\n");
return 0;
}
用户1834065 2015-4-5 08:38
用户377235 2013-10-15 18:03
用户377235 2013-8-5 21:39
wangxinfeng6666_749290079 2011-5-30 14:55
xucun915_925777961 2011-5-30 13:07