Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load pictures and upload textures from Assets folder in Android NDK development

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to load pictures and upload textures from Assets folder in Android NDK development? for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

In OpenGL development, we render an image, usually first get a picture corresponding to the Bitmap, and then upload the Bitmap as a texture to the OpenGL. There is an encapsulated texImage2D method of the GLUtils class in Android for us to call.

1 public static void texImage2D (int target, int level, int internalformat

2 Bitmap bitmap, int type, int border)

three

The underlying principle of this method is actually parsing the Bitmap and getting all the pixel data of Bitmap, which is similar to Android NDK's AndroidBitmap_lockPixels method on Bitmap operation. If you don't know much about this method, you can refer to this article: Bitmap operation of Android JNI.

After getting all the pixel data, the glTexImage2D of OpenGL is finally called to upload the texture. Of course, if you can get all the data directly, and you don't need to parse the Bitmap, the most common scenario is to use the camera as input.

Next, we will render an image through Android NDK development, as above, from image parsing to texture uploading, except that we will parse the image in the Assets folder instead of a picture that has been saved on the phone's SDCard.

Compared to the former, the picture on SDCard already has an absolute address, which can be parsed directly by sending the address to the stb_image database (refer to the previous article introduction to the easy-to-use image decoding library-stb_image), while the content of the Assets folder does not have an absolute address on the phone, oh, if you think about it carefully, have you ever seen the corresponding content of the Assets folder after APK installation?

At first, I fell into a misunderstanding, thinking about how to get the absolute address of the file. I saw AssetManager's AAsset_openFileDescriptor method and thought everything would be fine if I got the file descriptor. As a result, the printed address is like this / proc/9941/fd/79, which is basically unavailable.

To change the way of thinking, load the pictures in the Assets directory in Java:

1InputStream is = getAssets () .open (fileName)

Get the input stream of the file directly through the open method of AssertManager.

The same approach doesn't work in NDK development, but it all means the same thing:

1 / / AssetManager in NDK

2 AAssetManager * mgr = AAssetManager_fromJava (env, assetManager)

3 / / Open the file under the Asset folder

4 AAsset * pathAsset = AAssetManager_open (mgr, assetPath, AASSET_MODE_UNKNOWN)

5 / / get the length of the file

6 off_t assetLength = AAsset_getLength (pathAsset)

7 / / get the Buffer corresponding to the file

8 unsigned char * fileData = (unsigned char *) AAsset_getBuffer (pathAsset)

9 / / stb_image method to load pictures from memory

10 unsigned char * contnet = stbi_load_from_memory (fileData, assetLength, & w, & h, & n, 0)

You can't get an input stream like Java in NDK, but you can get the contents of the file through AssetManager's AAsset_getBuffer or AAsset_read method.

See the above two API basically stable, coupled with the method introduced by stb_image, stbi_load_from_memory loads the pixel data of the image from memory, and finally is the glTexImage2D method to achieve texture upload.

This is the answer to the question about how to load pictures and upload textures from the Assets folder in Android NDK development. I hope the above content can be of some help to you. If you still have a lot of doubts to solve, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report