安卓opengles使用ndk方式读取png图片

2024-05-16 00:32

本文主要是介绍安卓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图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/993363

相关文章

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd

Vue3使用router,params传参为空问题

《Vue3使用router,params传参为空问题》:本文主要介绍Vue3使用router,params传参为空问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录vue3使用China编程router,params传参为空1.使用query方式传参2.使用 Histo

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核