采用C++编写SO给懒人调用API
- #include <map>
- #include <string>
- #include <vector>
- #include <list>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <linux/if.h>
- #include <linux/sockios.h>
- #include <arpa/inet.h>
- #include <semaphore.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <android/log.h>
- #include <pthread.h>
- extern "C"
- {
- #include <math.h>
- #include <lua.h>
- #include <lauxlib.h>
- #include <lualib.h>
- #include <lr.h>
- };
- using namespace std;
- void LOGD(const char* fmt,...){
- char str[4096];
- va_list ap;
- int ret;
- va_start(ap, fmt);
- ret = vsnprintf(str, 4096, fmt, ap);
- if (ret >= 4096)
- return;
- __android_log_print(ANDROID_LOG_DEBUG, "LR", "%s",str);
- }
- static int lrGetPixel(lua_State *L){
- int ret = 0;
- luaL_checktype(L, 1, LUA_TNUMBER);//参数安全检查
- luaL_checktype(L, 2, LUA_TNUMBER);
- int x = (int)lua_tonumber(L,1);
- int y = (int)lua_tonumber(L,2);
- int w,h;
- int* ptr = (int*)lrGetScreenPixel(0,0,0,0,&w,&h);//获取屏幕数据
- LOGD("ret:%p %d %d",ptr,w,h);
- if(ptr){
- if(x < w && y < h){
- uint8_t* argb = (uint8_t*)&ptr[y * w + x];
- lua_pushinteger(L,argb[0]);
- lua_pushinteger(L,argb[1]);
- lua_pushinteger(L,argb[2]);
- ret = 3;
- }
- lrRelease(ptr);//一定要记得释放内存
- }
- return ret;
- }
- static const struct luaL_Reg R[] = {
- {"lrGetPixel" , lrGetPixel},
- {NULL, NULL}
- };
- extern "C"{
- LR_API int luaopen_liblrapi(lua_State*L)
- {
- luaL_newlib(L, R);
- LOGD("load lrapilib");
- return 1;
- }
- }
复制代码
|
|