简体   繁体   English

从缓冲区读取Png图像

[英]Read Png image from buffer

I try to load a PNG from assets and i successfully get it using AssetManager and this my function : 我尝试从资产加载PNG,然后使用AssetManager和我的函数成功获取它:

const char* ShaderManager::getPNGSource(const char* src,GLint *length)
{

AAsset* shaderAsset = AAssetManager_open(mgr,src, AASSET_MODE_UNKNOWN);

if (mgr == NULL) {
    LOGE("mgr is null");
}

*length = AAsset_getLength(shaderAsset);

char* buffer = (char*) malloc((*length));

AAsset_read(shaderAsset, buffer, *length);

LOGI("buffer source : %s\n", buffer);

AAsset_close(shaderAsset);

return (buffer);
}

the problem is when i want to decode this png format i have to use an other methode that accept FILE* type this the beginning of my function : 问题是,当我想解码此png格式时,我必须使用其他接受FILE *的方法来键入此函数的开头:

GLuint ShaderManager::png_texture_load(const char * file_name, int * width, int * height)
{
    png_byte header[8];

    FILE *fp = fopen(file_name, "rb");
    if (fp == 0)
    {
        perror(file_name);
        return 0;
    }
    .....
    .....
    .....

this is any solution to convert o char* to FILE* or any other solution or suggestion? 这是将char *转换为FILE *的任何解决方案,还是任何其他解决方案或建议?

You can use AAsset_openFileDescriptor() if the asset is not compressed 如果资产未压缩,则可以使用AAsset_openFileDescriptor()

Also, You can use fopen fwrite, etc.. to write your buffer to a file. 另外,您可以使用fopen fwrite等将缓冲区写入文件。 Then you can read from that file. 然后,您可以从该文件中读取。 You will probably need a path to the your file folder where you have read/write access. 您可能需要一个指向您具有读写访问权限的文件夹的路径。 I am not sure if you can count on the current directory being set to one with read/write access. 我不确定您是否可以指望将当前目录设置为具有读/写访问权限的目录。

Use funopen to open the file like so. 像这样使用funopen打开文件。 You need to save off the AAssetManager* from your android_main() and stick it in global g_pAssetManager (from you {android_app}->activity->assetManager). 您需要从android_main()中保存AAssetManager *,并将其粘贴到全局g_pAssetManager中(从{android_app}-> activity-> assetManager中获取)。 Please note that once the file is opened all normal fread, fwrite and fclose calls work on _File. 请注意,一旦打开文件,所有正常的fread,fwrite和fclose调用都将对_File起作用。

In module file.h 在file.h模块中

struct File{
  FILE* _File;
  AAsset* _A;
  File()
    : _File( nullptr )
    , _A( nullptr )
  {}
  bool open( const char* path, const char* mode );
  void close(){
    fclose( _File );
  }
  bool read( void* buf, size_t size ){
    size_t res = fread( _File, buf, size, 1 );
    if( res == size ){
      return true;
    }
    return false;
  }
};

In module file.cpp 在file.cpp模块中

extern"C"{
  extern AAssetManager* g_pAssetManager;
}

static int androidRead( void* cookie, char* buf, int size ){
  return AAsset_read( (AAsset*)cookie, buf, size );
}

static int androidWrite( void* cookie, const char* buf, int size ){
  return -1;//can't provide write access to the apk
}

static fpos_t androidSeek( void* cookie, fpos_t offset, int whence ){
  return AAsset_seek( (AAsset*)cookie, offset, whence );
}

static int androidClose( void* cookie ){
  AAsset_close( (AAsset*)cookie );
  return 0;
}

bool File::open( const char* path, const char* mode ){
  if( strchr( mode, 'w' )){
    _File = fopen( path, mode );
    return true;
  }
  _A = AAssetManager_open( g_pAssetManager, path, 0 );
  if( _A ){
    _File = funopen( _A,
      androidRead,
      androidWrite,
      androidSeek,
      androidClose );
    }
    if( _File != nullptr ){
      return true;
    }
  }
  return false;
}

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

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