本文主要是介绍安卓opengles使用ndk方式读取png图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本博客主要是通过使用libpng来进行加载png图片
1libpng图片配置
(1)在官网下载最新的libpng压缩包和zlib压缩包,然后解压,分别放入如下图所示的png目录和zlib目录
(2)将jni\png\scripts目录下的pnglibconf.h.prebuilt文件复制到jni\png目录下,并且重命名为pnglibconf.h。
(3)在jni\png目录下新建Android.mk文件,内容如下:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)LOCAL_MODULE := libpng
LOCAL_SRC_FILES :=\png.c \pngerror.c \pngget.c \pngmem.c \pngpread.c \pngread.c \pngrio.c \pngrtran.c \pngrutil.c \pngset.c \pngtest.c \pngtrans.c \pngwio.c \pngwrite.c \pngwtran.c \pngwutil.cLOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/.include $(BUILD_STATIC_LIBRARY)
(4)在jni\zlib目录下新建Android.mk文件,内容如下:
include $(CLEAR_VARS)
LOCAL_MODULE:= zlib
LOCAL_SRC_FILES:= \
adler32.c \
compress.c \
crc32.c \
deflate.c \
gzclose.c \
gzlib.c \
gzread.c \
gzwrite.c \
infback.c \
inflate.c \
inftrees.c \
inffast.c \
trees.c \
uncompr.c \
zutil.c LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(BUILD_STATIC_LIBRARY)
(4)在jni\目录下新建Android.mk文件,文件目录内容如下:
LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:=libmygles
LOCAL_SRC_FILES:=opengl.cpp
LOCAL_STATIC_LIBRARIES := libpng
LOCAL_STATIC_LIBRARIES += zlib
LOCAL_C_INCLUDES := $(LOCAL_PATH)/png
LOCAL_C_INCLUDES += $(LOCAL_PATH)/zlibLOCAL_LDLIBS:=-llog -lGLESv2 -landroidinclude $(BUILD_SHARED_LIBRARY)include $(CLEAR_VARS)include $(LOCAL_PATH)/png/Android.mk $(LOCAL_PATH)/zlib/Android.mk
(5)修改jni/png/pngpriv.h文件中
# ifdef __ARM_NEON__
# define PNG_ARM_NEON_OPT 2
# else
# define PNG_ARM_NEON_OPT 0
# en
为
#define PNG_ARM_NEON_OPT 0
不然会报错 png_init_filter_fuction_neon
(6)使用ndk编译主文件,我的文件是jni目录下的opengl.cpp文件,该文件中需要包括#include “png.h”,然后编译成功。
(7)使用如下函数读取并创建纹理,试了网上好多函数都不能用,使用该函数编译后,将png图片拷贝到assets函数下,即可调用
“`
GLuint CreateTextureFromPng(const char *filename)
{
info(“enter into CreateTextureFromPng”);
int bitDepth;
int colorType;
int interlaceype;png_uint_32 width;
png_uint_32 height;
void *pixelData = nullptr;
AAsset *asset = nullptr;
unsigned char head[8];
// off_t size;
asset = AAssetManager_open(sAssetManager, filename, AASSET_MODE_UNKNOWN);
if (asset == nullptr)
{info("load file %s fail", filename);return 0;
}
AAsset_read(asset, head, 8);
if (png_sig_cmp(head, 0, 8))
{info("File %s, is not PNG", filename);return 0;
}info("enter into CreateTextureFromPng1");png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!pngPtr)
{info("Unable to create PNG structure: %s", filename);return 0;
}// Allocate/initialize the memory for image information. REQUIRED
png_infop infoPtr = png_create_info_struct(pngPtr);if (!infoPtr)
{png_destroy_read_struct(&pngPtr, NULL, NULL);info("Unable to create PNG info : %s", filename);return 0;
}
info("enter into CreateTextureFromPng2");
png_infop endInfo = png_create_info_struct(pngPtr);
if (!endInfo)
{png_destroy_read_struct(&pngPtr, &infoPtr, NULL);info("Unable to create PNG end info : %s", filename);return 0;
}
// Set error handling if you are using the setjmp/longjmp method (this is
// the normal method of doing things with libpng). REQUIRED unless you
// set up your own error handlers in the png_create_read_struct() earlier.
if (setjmp(png_jmpbuf(pngPtr)))
{// Free all of the memory associated with the png_ptr and info_ptrpng_destroy_read_struct(&pngPtr, &infoPtr, &endInfo);info("Error during setjmp : %s", filename);return 0;
}
png_set_read_fn(pngPtr, (void *)asset, readPngData);
info("enter into CreateTextureFromPng3");// If we have already read some of the signature
png_set_sig_bytes(pngPtr, 8);png_read_info(pngPtr, infoPtr);png_get_IHDR(pngPtr, infoPtr, &width, &height, &bitDepth, &colorType, &interlaceype, NULL, NULL);info("PNG width = %d, height = %d", width, height);// Update the png info struct.
png_read_update_info(pngPtr, infoPtr);// Allocate the memory to hold the image using the fields of info_ptrunsigned int rowBytes = png_get_rowbytes(pngPtr, infoPtr);
info("Row size: %d bytes", rowBytes);// Allocate the pixel data as a big block, to be given to openGL
pixelData = png_malloc(pngPtr, rowBytes * height);
if (!pixelData)
{png_destroy_read_struct(&pngPtr, &infoPtr, &endInfo);info("Unable to allocate PNG pixel data while loading %s", filename);return 0;
}
int numberPasses = png_set_interlace_handling(pngPtr);
info("interlacing passes = %d", numberPasses);
for (int pass = 0; pass < numberPasses; pass++)
{for (int row = 0; row < height; row++){png_read_row(pngPtr, ((unsigned char *)pixelData + (row * rowBytes)), NULL);}
}png_read_end(pngPtr, infoPtr);png_destroy_read_struct(&pngPtr, &infoPtr, &endInfo);info("enter into CreateTextureFromPng4");AAsset_close(asset);
//此时 pixelData 已经赋值结束 png_uint_32 width; png_uint_32 height;GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
glBindTexture(GL_TEXTURE_2D, 0);info("enter into CreateTextureFromPng5");
return texture;
}
2参考链接
(1)http://www.xuebuyuan.com/zh-hant/1894472.html
(2)http://blog.csdn.net/talkxin/article/details/50968313
(3)http://blog.sina.com.cn/s/blog_643634b80101hzi8.html
(4)https://stackoverflow.com/questions/19089014/iphone-device-linker-error
这篇关于安卓opengles使用ndk方式读取png图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!