Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
C++ 如何在c+中加载base64编码图像作为SDL纹理+;?_C++_Base64_Sdl - Fatal编程技术网

C++ 如何在c+中加载base64编码图像作为SDL纹理+;?

C++ 如何在c+中加载base64编码图像作为SDL纹理+;?,c++,base64,sdl,C++,Base64,Sdl,我从cpprestsdk获得一个base64编码的图像,并尝试在sdl窗口中显示它 void LoadImageJpeg(std::wstring theImage, int x, int y) { //Clear the window //SDL_RenderClear(renderer); std::vector<unsigned char> img_vec = base64_decode(theImage); SDL_RWops *rw = SD

我从cpprestsdk获得一个base64编码的图像,并尝试在sdl窗口中显示它

void LoadImageJpeg(std::wstring theImage, int x, int y)
{
    //Clear the window
    //SDL_RenderClear(renderer);
    std::vector<unsigned char> img_vec = base64_decode(theImage);
    SDL_RWops *rw = SDL_RWFromConstMem(&img_vec[0], img_vec.size());
    SDL_Surface *aSurface = IMG_LoadTyped_RW(rw, 1, "JPG");
    if (aSurface == nullptr) {
        logSDLError(std::cout, "base64ToSurface");
    }
    SDL_Texture *texture = nullptr;
    texture = SDL_CreateTextureFromSurface(renderer, aSurface);
    //Make sure converting went ok too
    if (texture == nullptr) {
        logSDLError(std::cout, "CreateTextureFromSurface");
    }
    renderTexture(texture, renderer, 0, 0);
    SDL_RenderPresent(renderer);
    //SDL_Delay(500);
}
我无法从base64字符串创建曲面,但它可以从磁盘上的文件中正常工作

我尝试了多次将字符串转换为字符数组和向量,但SDL_曲面的末尾总是空的

两个人引导我朝着SDL_RWops的方向前进:

SDL_Surface* base64ToSurface(std::string *image)
{
    SDL_RWops *rw = SDL_RWFromConstMem(image, sizeof(image));
    SDL_Surface *img = SDL_LoadBMP_RW(rw, 1);        
    if (img == nullptr)
    {
        logSDLError(std::cout, "base64ToSurface");
    }
    return img;
}

void convertBase64ToTexture()
{
    //base64 image string trimmed for a better readability
    std::string aImage = "R0lGODlhPQB...";

    SDL_Window *aWindow = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    SDL_Renderer *aRenderer = SDL_CreateRenderer(aWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
    SDL_Surface *aSurface = base64ToSurface(&aImage);
    SDL_Texture *texture = nullptr;
    texture = SDL_CreateTextureFromSurface(aRenderer, aSurface);
    //Make sure converting went ok too
    if (texture == nullptr) 
    {
        logSDLError(std::cout, "CreateTextureFromSurface");
    }
}

谢谢你的帮助,凯尔塔立刻帮了我一个大忙。如果你把你的评论添加为answear,我会投你的票

遵循将base64 jpg加载到sdl窗口的代码

void LoadImageJpeg(std::wstring theImage, int x, int y)
{
    //Clear the window
    //SDL_RenderClear(renderer);
    std::vector<unsigned char> img_vec = base64_decode(theImage);
    SDL_RWops *rw = SDL_RWFromConstMem(&img_vec[0], img_vec.size());
    SDL_Surface *aSurface = IMG_LoadTyped_RW(rw, 1, "JPG");
    if (aSurface == nullptr) {
        logSDLError(std::cout, "base64ToSurface");
    }
    SDL_Texture *texture = nullptr;
    texture = SDL_CreateTextureFromSurface(renderer, aSurface);
    //Make sure converting went ok too
    if (texture == nullptr) {
        logSDLError(std::cout, "CreateTextureFromSurface");
    }
    renderTexture(texture, renderer, 0, 0);
    SDL_RenderPresent(renderer);
    //SDL_Delay(500);
}
void LoadImageJpeg(std::wstring theImage,intx,inty)
{
//清理窗户
//SDL_渲染器(渲染器);
std::vector img_vec=base64_解码(图像);
SDL_RWops*rw=SDL_rwfromfromconstmem(&img_vec[0],img_vec.size());
SDL_表面*aSurface=IMG_载荷类型化_RW(RW,1,“JPG”);
if(aSurface==nullptr){
logSDLError(标准:cout,“基准面至表面”);
}
SDL_纹理*纹理=nullptr;
纹理=SDL_CreateTextureFromSurface(渲染器,aSurface);
//确保转换也正常进行
如果(纹理==nullptr){
logSDLError(标准::cout,“CreateTextureFromSurface”);
}
renderTexture(纹理,渲染器,0,0);
SDL_渲染器呈现(渲染器);
//SDL_延迟(500);
}

谢谢。

那么……base64解码程序在哪里?现在看来,您只是将base64字节传递给
SDL\u LoadBMP\u RW()
,并希望得到最好的结果。即使如此,您也要求SDL将
std::string
对象(不是它所持有的字符串,而是对象本身)的前4/8字节解释为图像。这是行不通的。我对C++和SDL是新的,尝试了多种解码字符串的方法。此示例旨在简化设置。我没有从谷歌的大量研究中找到帮助。我甚至不确定是否可以从base64字符串在sdl中显示图像。@Simon您需要将该数组(连同它的长度!您的
sizeof(image)
提供指针的大小,它与图像数据的大小无关)传递到
sdl\u FromConstmem
。例如,使用链接示例的解码(我认为这不是最好的方法)
std::vector img_vec=base64_decode(aImage);SDL_RWops*rw=SDL_rwfromfromconstmem(&img_vec[0],img_vec.size())