Base64。我无法解码图像文件

时间:2014-12-10 10:59:10

标签: c

我的代码有问题。我得到了bmp图像并将其编码为base64。然后我想让它解码,但我的电脑说:"无法加载图像' OUTPUT.PNG'"。如何修复它?

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


const unsigned char base64[] = {
    // ASCII table 
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 64, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
    64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
    64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64  
};

void deblock(const unsigned char *in, const unsigned char *base64, FILE * OutputFile, int len)
{
 if (base64[(unsigned char)in[0]] < 64  && base64[(unsigned char)in[1]] < 64)   
    fprintf(OutputFile, "%c",(base64[(unsigned char)in[0]] << 2 | base64[(unsigned char)in[1]] >> 4));
 if (base64[(unsigned char)in[1]] < 64  && base64[(unsigned char)in[2]] < 64)       
    fprintf(OutputFile, "%c",(base64[(unsigned char)in[1]] << 4 | base64[(unsigned char)in[2]] >> 2));
 if (base64[(unsigned char)in[2]] < 64  && base64[(unsigned char)in[3]] < 64)   
    fprintf(OutputFile, "%c", base64[(unsigned char)in[2]] << 6 | base64[(unsigned char)in[3]]);
}   

void decode (FILE *InputFile, FILE *OutputFile)
{

    int len = 0;
    unsigned char buffer[4] = "\0"; 

    while ((len = fread(buffer,sizeof(unsigned char),4,InputFile)) > 0)
    {   
        deblock(buffer,base64,OutputFile,len);
    }   
}


int check_decode(FILE *file1, FILE *file2)
{
    int k = 0;

    fseek (file1, 0, SEEK_END);
    if (0 == ((ftell(file1)) % 4))
    {
        fseek(file1,0,SEEK_SET);
        decode(file1,file2);
    }
    else 
    {
        k = -1;
    }
    return k;
} 


int main() 
{
    FILE *file1 = NULL;
    FILE *file2 = NULL;
    if (NULL == (file1 = fopen("INPUT1.txt", "rb")))
    {   
        printf("Can't open INPUT file!");
        return -1;
    }
    if (NULL == (file2 = fopen("OUTPUT.png", "wb    ")))
    {
        printf("Can't create OUTPUT file");
        fclose(file1);
        return -1;
    }   
    if ( -1 == check_decode(file1, file2)       )
    {
        printf("Eto ne base64\n ");
        return -1;
    }
    assert(file1);
    assert(file2);
    fclose(file1);
    fclose(file2);
    return 0;
}

我使用此网站对我的图片进行编码http://www.base64-image.de/step-2.php

1 个答案:

答案 0 :(得分:0)

  

我得到了bmp图像并将其编码为base64。然后我想要它解码,   但我的电脑说:“无法加载图像'OUTPUT.PNG'”。我该如何解决?

由于您获得了bmp图片,只需创建OUTPUT.bmp而不是OUTPUT.png