用c语言读一张图片

时间:2022-11-29 10:09:17

我觉得写的很好,网上有一模一样的代码,感谢分享。

/* *This program will open a image,and output to another file *Author:hujian in nankai 2016/3/29 */

#include <cstdio>
#include <iostream>

using namespace std;

#define __FILENAME "./img/in.jpg" //this is the default image,if no input filename,the image will be used

int main()
{
    //the fp is the file stream of reading data
    //the out is the file stream of writing to file(hex)
    //the dst file is the file stream of dest image
    //
    FILE *fp, *out, *dstFile;
    //this is the image size
    //we will use fseek and ftell to get the size
    //
    long int size;

    unsigned char ch, ch1, ch2;
    //get the higt bites and low bites
    //
    unsigned char high, low;
    //this is the image name buffer
    //
    char pic_name[30];
    memset(pic_name, 0, sizeof(pic_name));
    //we can input the file's name,and the program will open the image and do some
    //oprtation,such as read data,write from image data..
    //
//#define INPUT
#ifdef INPUT //if we want to input the filename ,just define the input.
    printf("\nPlease input the image's name:");
    cin >> pic_name;
#else //ok,you don't want to input
    strcpy_s(pic_name,__FILENAME);
#endif INPUT
    printf_s("The filename is:%s\n", pic_name);
    //the follow lines will open the file
    //
    {
        //open the src image for reading the image data
        //
        fopen_s(&fp, pic_name, "rb");
        //open the text stream(write)
        //
        fopen_s(&out,"res.txt", "wb+");
        //open the dest image file stream
        //
        fopen_s(&dstFile,"./img/res.jpg", "wb+");
        //ok,check the file stream
        //
        {
            if (!fp || !out || !dstFile){
                printf_s("Can not open the file you input,check the filename and re-try\n");
                //exit(EXIT_FAILURE);
            }
        }
    }//end of open file,now we get the file stream done.
    //we want to get the size of the image,we just use the fseek and ftell get the size
    //
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    //printf the size
    printf("get the size is: %d\n", size);
    //rewind means jump to start of the file
    //
    rewind(fp);
    //traversal the image data
    //
    long int i;
    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;
        //write to file stream out
        //
        fputc(high, out);
        fputc(low, out);
        fputc(' ', out);
        //16 one line
        //
        if (i % 16 == 0)
            fprintf(out, "\r\n");
    }
    //jump to start of the image file
    //
    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);
            }
        }
    }
    //close the image file stream
    //
    fclose(fp);
    fclose(out);
    fclose(dstFile);
    printf_s("--operation done--\n");
    return 0;
}