首页 > 其他分享 >ndk集成stb_image.h

ndk集成stb_image.h

时间:2024-09-05 18:05:26浏览次数:5  
标签:stbi imgSize stb int image ndk height width

一、概述

  使用步骤:

     1.在ndk入口cpp中加入一个宏。ps:最好加最上面

#include <jni.h>
#include <string>
#define STB_IMAGE_IMPLEMENTATION

     2.在使用的时候导入头文件 

//导入stb_image头文件
#include "stb_image.h"

 

二、代码示例

  stbi_load方法,传入图像的路径,可以拿到图像的宽、高和通道数(channel)

void loadDataFromFile(const char *fileName) {
        int type = 0;
        int width = 0;
        int height = 0;

        //stbi_set_flip_vertically_on_load(true);//如果图像有镜像可以用开启这个翻转
        unsigned char *picData = stbi_load((char const *) fileName, &width, &height, &type,
                                           STBI_rgb_alpha);

        int imgSize = width * height * 4;

        if (imgSize > 0 && picData != nullptr) {
            mBitmapPixels = malloc(imgSize);
            memcpy(mBitmapPixels, picData, imgSize);
            mBitmapWidth = width;
            mBitmapHeight = height;
            mChannel = type;
        }

        stbi_image_free(picData);
    }

 

标签:stbi,imgSize,stb,int,image,ndk,height,width
From: https://www.cnblogs.com/tony-yang-flutter/p/18398965

相关文章