网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
unsigned int imageSize; //图片字节数
char \*imageBin;
char \*imageBase64;
char \*imageOutput;
size_t result;
char \*ret;
unsigned int base64StrLength;
fp = fopen("test.jpg","rb"); //待编码图片
if (NULL == fp)
{
printf("file open file");
return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK\_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK\_SET);
//分配内存存储整个图片
imageBin = (char \*)malloc(sizeof(char)\*imageSize);
if (NULL == imageBin)
{
printf("malloc failed");
return -1;
}
//读取图片
result = fread(imageBin, 1, imageSize, fp);
if (result != imageSize)
{
printf("file read failed");
return -1;
}
fclose(fp);
//分配编码后图片所在buffer
imageBase64 = (char \*)malloc(sizeof(char)\*imageSize\*2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
//base64编码
base64\_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("%s\n", imageBase64);
//分配存储解码数据buffer
imageOutput = (char \*)malloc(sizeof(char)\*imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
base64\_decode(imageBase64, imageOutput);
fp = fopen("output.jpg","wb");
if (NULL == fp)
{
printf("file open file");
return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp);
free(imageBin);
free(imageBase64);
free(imageOutput);
return 0;
}
const char * base64char = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;
char *base64_encode( const unsigned char * bindata, char * base64, int binlength )
{
int i, j;
unsigned char current;
for ( i = 0, j = 0 ; i < binlength ; i += 3 )
{
current = (bindata[i] >> 2) ;
current &= (unsigned char)0x3F;
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i] << 4 ) ) & ( (unsigned char)0x30 ) ;
if ( i + 1 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+1] >> 4) ) & ( (unsigned char) 0x0F );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)(bindata[i+1] << 2) ) & ( (unsigned char)0x3C ) ;
if ( i + 2 >= binlength )
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ( (unsigned char)(bindata[i+2] >> 6) ) & ( (unsigned char) 0x03 );
base64[j++] = base64char[(int)current];
current = ( (unsigned char)bindata[i+2] ) & ( (unsigned char)0x3F ) ;
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;
}
int base64_decode( const char * base64, unsigned char * bindata )
{
int i, j;
unsigned char k;
unsigned char temp[4];
for ( i = 0, j = 0; base64[i] != ‘\0’ ; i += 4 )
{
memset( temp, 0xFF, sizeof(temp) );
for ( k = 0 ; k < 64 ; k ++ )
{
if ( base64char[k] == base64[i] )
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
https://bbs.csdn.net/topics/618668825)**
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!