Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中,将img缓冲区保存为BMP文件正在变黑_Android_C++_Image_Android Ndk - Fatal编程技术网

在Android中,将img缓冲区保存为BMP文件正在变黑

在Android中,将img缓冲区保存为BMP文件正在变黑,android,c++,image,android-ndk,Android,C++,Image,Android Ndk,我有一个img缓冲区,我正试图将其保存到bmp文件中。 我将缓冲区强制转换为unsigned long,以便访问像素值。 我试了一下,把img漆成了红色,它工作正常 unsigned char* pTextureBuffer = static_cast<unsigned char*>((pTexture->GetBuffer())->lock(Ogre::HardwareBuffer::HBL_NORMAL)); unsigned long* pTextu

我有一个img缓冲区,我正试图将其保存到bmp文件中。
我将缓冲区强制转换为unsigned long,以便访问像素值。
我试了一下,把img漆成了红色,它工作正常

    unsigned char* pTextureBuffer = static_cast<unsigned char*>((pTexture->GetBuffer())->lock(Ogre::HardwareBuffer::HBL_NORMAL));
    unsigned long* pTextureBufferPixel = (unsigned long*)pTextureBuffer;


        int w=(int)uTextureWidthInPixels; 
        int h=(int)uTextureHeightInPixels;

        FILE *f;
        unsigned char *img = NULL;
        int filesize = 54 + 3*w*h;  //w is your image width, h is image height, both int
        if( img )
            free( img );
        img = (unsigned char *)malloc(3*w*h);
        memset(img,0,sizeof(img));

        for(int i=0; i<w; i++)
        {
            for(int j=0; j<h; j++)
            {
                int x=i; int y=(h-1)-j;

                unsigned long r = 255;
                unsigned long g = 0;
                unsigned long b = 0;

                img[(x+y*w)*3+2] = (unsigned char)(r);
                img[(x+y*w)*3+1] = (unsigned char)(g);
                img[(x+y*w)*3+0] = (unsigned char)(b);
            }
        }

        unsigned char bmpfileheader[14] = {'B','M', 0,0,0,0, 0,0, 0,0, 54,0,0,0};
        unsigned char bmpinfoheader[40] = {40,0,0,0, 0,0,0,0, 0,0,0,0, 1,0, 24,0};
        unsigned char bmppad[3] = {0,0,0};

        bmpfileheader[ 2] = (unsigned char)(filesize    );
        bmpfileheader[ 3] = (unsigned char)(filesize>> 8);
        bmpfileheader[ 4] = (unsigned char)(filesize>>16);
        bmpfileheader[ 5] = (unsigned char)(filesize>>24);

        bmpinfoheader[ 4] = (unsigned char)(       w    );
        bmpinfoheader[ 5] = (unsigned char)(       w>> 8);
        bmpinfoheader[ 6] = (unsigned char)(       w>>16);
        bmpinfoheader[ 7] = (unsigned char)(       w>>24);
        bmpinfoheader[ 8] = (unsigned char)(       h    );
        bmpinfoheader[ 9] = (unsigned char)(       h>> 8);
        bmpinfoheader[10] = (unsigned char)(       h>>16);
        bmpinfoheader[11] = (unsigned char)(       h>>24);

        f = fopen("//sdcard//tex//a.bmp","wb");
        fwrite(bmpfileheader,1,14,f);
        fwrite(bmpinfoheader,1,40,f);
        for(int i=0; i<h; i++)
        {
            fwrite(img+(w*(h-i-1)*3),3,w,f);
            fwrite(bmppad,1,(4-(w*3)%4)%4,f);
        }
        fclose(f);
与:

从缓冲区中保存img,但我得到的是全黑。
黑色img仅在android中出现,而在windows中img保存正确。

我错过了什么

你确定你创建了这个文件吗//SD卡//对于android来说不是有效的文件名,它是一个文件夹
一个img缓冲区,我正在尝试将其保存到bmp文件。
。字节缓冲区?里面有什么?你是怎么填的?Android不支持bmp文件,但如果您知道自己在做什么,这可能无关紧要。@OlegBogdanov-是的,我实际上是将文件写入“//sdcard//tex//”而不是“//sdcard//”,我的不好但相同issue@greenapps-不确定,我用食人魔填充它
“//sdcard//tex//”
。那还是一个文件夹。为什么不使用扩展名为roght的文件名呢?
unsigned long r = 255;
unsigned long g = 0;
unsigned long b = 0;
    unsigned long r = (pTextureBufferPixel[i*uTextureWidthInPixels + j]) & 0xff; 
    unsigned long g = (pTextureBufferPixel[i*uTextureWidthInPixels + j] >> 8) & 0xff;
    unsigned long b = (pTextureBufferPixel[i*uTextureWidthInPixels + j] >> 16) & 0xff;