【问题标题】:Android - Load image on the native sideAndroid - 在本机端加载图像
【发布时间】:2011-03-11 03:31:59
【问题描述】:

我正在尝试加载图像,以便将它们用作纹理。我有 libpng,但是如何找到图像的路径?将其放入 .apk 是不是一个坏主意?有没有更好的办法?

【问题讨论】:

    标签: opengl-es android-ndk


    【解决方案1】:
    【解决方案2】:

    似乎有两种方法可以做到这一点。我可以按照 Simon N. 的建议进行操作,也可以将图像转换为 C 数组并将它们编译到源代码中。我选择做后者,因为我可以在本地轻松地做到这一点。我什至创建了一个 python (v3.2) 用于将 png 文件转换为 c 数组头和源文件。它需要PyPNG

    #!/usr/bin/env python
    
    import png
    import re
    import itertools
    
    def grouper(n, iterable, fillvalue=None):
        args = [iter(iterable)] * n
        return itertools.zip_longest(fillvalue=fillvalue, *args)
    
    def expand(array):
        new_array = [];
        for row in array:
            for v in row:
                new_array.append(v)
        return new_array
    
    def c_array_name(filename):
        '''converts the filename to the c array name'''
        return re.sub(r'\W', '_', filename)
    
    def c_array_header_filename(filename):
        '''converts the filename to the c array header filename'''
        return "{0}.h".format(filename)
    
    def c_array_source_filename(filename):
        '''converts the filename to the c array source filename'''
        return "{0}.cpp".format(filename)
    
    def c_array_header(filename):
        '''returns a string that is the c array header,
        where
            filename is the png file'''
        name = c_array_name(filename)
        return """
    #ifndef __{0}__
    #define __{0}__
    
    #include <stdint.h>
    
    extern uint_least32_t const {1}[];
    
    #endif /* __{0}__ */
    """.format(name.upper(), name)
    
    def c_array_source(filename, header_filename, array_string):
        '''returns a string that is the c array source,
        where
            name is the value from c_array_name
            array_string'''
        name = c_array_name(filename)
        return """
    #include "{0}"
    
    uint_least32_t const {1}[] = {{
    {2}
    }};
    """.format(header_filename, name, array_string)
    
    def convert_data_array_string(data):
        '''returns a string of hexes of bytes, 
        where
            data is a map of bytes'''
        return ", ".join(["0x{:02x}{:02x}{:02x}{:02x}".format(a, b, g, r)
            for r, g, b, a in grouper(4, expand(data), 0)])
    
    def png_data_from_file(path):
        '''returns a map of bytes of the png file'''
        with open(path, 'rb') as file:
            reader = png.Reader(file = file)
            data = reader.read();
            return list(data[2]);
    
    if __name__ == '__main__':
        import sys
        import os
        if len(sys.argv) != 2:
            sys.stdout.write("{0} image_path".format(sys.argv[0]))
            exit()
        path = sys.argv[1]
        filename = os.path.split(path)[1]
        header_filename = c_array_header_filename(filename)
        sys.stdout.write("Creating header file '{}'... ".format(header_filename))
        with open(header_filename, 'w') as header_file:
            header_file.write(c_array_header(filename))
        sys.stdout.write("done\n")
        source_filename = c_array_source_filename(filename)
        sys.stdout.write("Creating source file '{}'... ".format(source_filename))
        data = png_data_from_file(path)
        with open(source_filename, 'w') as source_file:
            source_file.write(c_array_source(filename, header_filename, convert_data_array_string(data)))
        del data
        sys.stdout.write("done\n")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多