简体   繁体   English

如何在c中打开几个jpg图像?

[英]How to open several jpg images in c?

Update : Thanks for the help, I figured it out! 更新 :感谢您的帮助,我想通了! I should have incremented i when I used 我使用时应该增加i

        putc(input_char, output_file);
        input_char = chars[i+1];
        putc(input_char, output_file);
        input_char = chars[i+2];
        putc(input_char, output_file);
        input_char = chars[i+3];
        putc(input_char, output_file);

I have a file with several jpeg images. 我有一个包含几个jpeg图像的文件。 I need to put each image in a separate file. 我需要将每个图像放在一个单独的文件中。 I've already put the file given into: 我已经将文件放入:

unsigned char* chars;

which I made equal to: 我所说的相同:

chars = (unsigned char*) malloc (sizeof(unsigned char)*count+1);

(count is the total number of bytes) (count是总字节数)

Here is a large portion of my code. 这是我代码的很大一部分。 I am trying to just get one image to work before I go through the entire file. 在我浏览整个文件之前,我试图让一个图像工作。 I thought this would work, but I can't get an image. 我认为这会起作用,但我无法获得图像。 I get a file called image.jpg with a lot of bytes in it, but no picture. 我得到一个名为image.jpg的文件,里面有很多字节,但没有图片。 It seems like the very beginning of the file is what is wrong, but I'm not for sure about that. 似乎文件的开头是错误的,但我不确定。 Any advice or tips would be appreciated. 任何建议或提示将不胜感激。 Thanks in advance. 提前致谢。

FILE* output_file;
unsigned char input_char;


for(i=0; i <=count-3 && flag2==0; i++)
{
    input_char = chars[i];
    if ((chars[i] == 0xff ) && 
        (chars[i+1] == 0xd8) && 
        (chars[i+2] == 0xff) && 
        (chars[i+3] == 0xe0))
    {
        if (flag==1) {
            puts("closing file I");
            fclose(output_file);
            puts("closed I");
            flag2 = 1;
            break;
        }

        pictures++;
        puts("New pic! I");
        puts("Opening file I!");
        output_file = fopen("image.jpg", "wb");
        putc(input_char, output_file);
        input_char = chars[i+1];
        putc(input_char, output_file);
        input_char = chars[i+2];
        putc(input_char, output_file);
        input_char = chars[i+3];
        putc(input_char, output_file);

        flag = 1;//there is at least one image, so it will need to be closed
    }
    else if ((chars[i] == 0xff) && 
             (chars[i+1] == 0xd8) && 
             (chars[i+2] == 0xff) && 
             (chars[i+3] == 0xe1))
    {
        if (flag==1) {
            puts("closing file II");
            fclose(output_file);
            puts("closed II");
            flag2 = 1;
            break;
        }

        pictures++;
        puts("New pic II!");
        puts("Opening file! II");
        output_file = fopen("Image.jpg", "wb");
        putc(input_char, output_file);
        input_char = chars[i+1];
        putc(input_char, output_file);
        input_char = chars[i+2];
        putc(input_char, output_file);
        input_char = chars[i+3];
        putc(input_char, output_file);

        flag = 1;//there is at least one image, so it will need to be closed
    }
    else
    {
        if (flag==1) //There is an image opened
        {
            putc(input_char, output_file);
        }
    }                
}

First things first, I think you'd have more success with a complete re-write using the memmem(3) function ( memmem(3) is a GNU extension like strstr(3) , but for arbitrary memory, not just C-strings. It wouldn't be difficult to write your own version if memmem(3) isn't already available to you.). 首先,我认为使用memmem(3)函数进行完全重写会有更多的成功( memmem(3)是像strstr(3)这样的GNU扩展,但对于任意内存,不仅仅是C字符串如果你还没有memmem(3)那么编写自己的版本并不困难。) Of course, using memmem(3) would still be a problem if the embedded JPEG file included data that matched these header bytes. 当然,如果嵌入的JPEG文件包含与这些头字节匹配的数据,则使用memmem(3) 仍然是个问题。 I don't know that it is possible, but I also don't know that it is impossible. 我不知道这是可能的,但我也不知道这是不可能的。 Be careful. 小心。 :) :)

This loop looks like nothing but trouble to maintain over time, which is why I suggested a completely different approach. 这个循环看起来只是随着时间的推移而保持麻烦,这就是为什么我提出了一种完全不同的方法。 But if you're committed to this approach, note that you'll be copying your header bytes several times; 但是,如果你正在致力于这种方法,请注意,您可以复制你的头字节数次 ; I expect the first few bytes of your output file will actually be: 0xff 0xd8 0xff 0xe0 0xd8 0xff 0xe0 -- you haven't stepped i forward to account for all the written characters in your loop. 我希望你的输出文件的前几个字节实际上是: 0xff 0xd8 0xff 0xe0 0xd8 0xff 0xe0 - 你没有让i前进到你的循环中的所有书面字符。

I think your code would be easier to read and maintain if you replace this code: 我认为如果您替换此代码,您的代码将更容易阅读和维护:

  putc(input_char, output_file); input_char = chars[i+1]; putc(input_char, output_file); input_char = chars[i+2]; putc(input_char, output_file); input_char = chars[i+3]; putc(input_char, output_file); 

with this code: 使用此代码:

            putc(chars[i++], output_file);
            putc(chars[i++], output_file);
            putc(chars[i++], output_file);
            putc(chars[i++], output_file);

(The i++ is the bugfix , in that you're not moving i forward for each character; removing the input_char variable is just cleanup, as I found it more difficult to read with the variable than without it.) (在i++修正错误 ,因为你不动i前进的每一个字符;除去input_char 。变量只是清理,因为我发现它没有它的变量更难以阅读)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM