安卓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

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(