当前位置: 首页 > news >正文

网站备案好弄吗wordpress去掉模板登录

网站备案好弄吗,wordpress去掉模板登录,买空间的网站,工业和信息化部发短信是什么意思写在前面#xff1a;Java相对于C/C来说是更高级的语言#xff0c;隐藏了指针#xff0c;可读性更高#xff0c;更容易学习#xff0c;但是无法直接操作硬件、运行速度较慢也是不可回避的硬伤。JNI就是Java官方定义的一套标准“接口”#xff0c;用于Java和C/C之间互相调用… 写在前面Java相对于C/C来说是更高级的语言隐藏了指针可读性更高更容易学习但是无法直接操作硬件、运行速度较慢也是不可回避的硬伤。JNI就是Java官方定义的一套标准“接口”用于Java和C/C之间互相调用注意这里是互相调用而不是只能用Java去调用C/C。搞Android开发的JNI是必须掌握的技术以前我一直逃避因为觉得比较难但是真正学习下来并没有那么难。Java运行速度较慢有一些对时间要求超高的场景就必须用到JNI。 每一节文末会附上Demo APP的GitHub链接。 在正式使用JNI之前我们必须搞清楚两个相关的概念   Android NDK这是Google官方提供的工具包用于将C/C代码链接它所需要的库编译成.so或者.a文件。大白话说就是没有它就不能在Android Studio 这个应用里面编译C/C代码。   JNIJNI不是包含于Android NDK里面的两者相互独立JNI只要是Java代码都能使用不局限于Android应用开发很多人容易把两者混为一潭。   下面开始介绍Android应用中如何使用JNI以Java调用C/C这种形式为例子分两种情况1、一开始就创建为C/C项目2、创建的是一般项目开发中途想调用C/C代码。 一、C/C项目 1.1 创建一个Native C项目 在new一个项目的时候选择Native C创建一个名为JNI的项目。   创建完之后的项目结构如下可以看到在java的同级目录Android Studio已经自动为我们生成了cpp目录cpp目录下自动生成了cpp文件和CMakeLists文件。 1.2 CMakeLists.txt 讲解 CMakeLists的作用就是告诉Android NDK这个CPP项目的源文件是哪些需要链接哪些库需要生成静态库还是动态库。自动生成的 CMakeLists.txt 如下 # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html. # For more examples on how to use CMake, see https://github.com/android/ndk-samples.# Sets the minimum CMake version required for this project. cmake_minimum_required(VERSION 3.22.1)# Declares the project name. The project name can be accessed via ${ PROJECT_NAME}, # Since this is the top level CMakeLists.txt, the project name is also accessible # with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level # build script scope). project(jni)# Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. # # In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define # the target library name; in the sub-modules CMakeLists.txt, ${PROJECT_NAME} # is preferred for the same purpose. # # In order to load a library into your app from Java/Kotlin, you must call # System.loadLibrary() and pass the name of the library defined here; # for GameActivity/NativeActivity derived applications, the same library name must be # used in the AndroidManifest.xml file. add_library(${CMAKE_PROJECT_NAME} SHARED# List C/C source files with relative paths to this CMakeLists.txt.native-lib.cpp)# Specifies libraries CMake should link to your target library. You # can link libraries from various origins, such as libraries defined in this # build script, prebuilt third-party libraries, or Android system libraries. target_link_libraries(${CMAKE_PROJECT_NAME}# List libraries link to the target libraryandroidlog)cmake_minimum_required用来说明现在使用的CMake的最低版本。   project(“jni”)告诉NDK这个CMake的项目名称叫jni与后续编译关系不大有点吉祥物的意思。   add_library这里其实是分三段来看——》${CMAKE_PROJECT_NAME}是规定最后编译产物的名字这里直接用了上一步的project name jni也就是说最后编译出来的库名字为 libjni当然你也可以取其它任意名字比如Xusu这样。——》SHARED 告诉NDK最后编译的是动态库生成的是libjni.so如果需要生成静态库把SHARED更换为STATIC即可最后生成 libjni.a文件。——》native-lib.cpp 是告诉NDK 需要编译的源文件这里可以列出来很多个.cpp文件或者.h头文件每个文件占一行。   target_link_libraries意思就是将NDK中现成的android库、log库链接到生成的libjni库中链接到 android 库可以让你的 C/C 代码调用一些底层的 Android API链接到 log 库就是为了打印日志嘛。写法也是固定的target_link_libraries(target_name library1 library2 …)。 1.3 native-lib.cpp 讲解 自动生成的native-lib.cpp如下 #include jni.h #include stringextern C JNIEXPORT jstring JNICALL Java_com_htc_jni_MainActivity_stringFromJNI(JNIEnv* env,jobject thiz) {std::string hello Hello from C; /*声明一个字符串*/return env-NewStringUTF(hello.c_str()); /*将C语言中的字符通过.c_str()和NewStringUTF方法传化为Java中的String返回给Java代码使用*/ }这里我们贴出MainActivity中声明的方法一起对照更容易理解 package com.htc.jni;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.widget.TextView;import com.htc.jni.databinding.ActivityMainBinding;public class MainActivity extends AppCompatActivity {// Used to load the jni library on application startup.static { //固定写法通过System.loadLibrary加载需要的C/C动态库System.loadLibrary(jni);}private ActivityMainBinding binding;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextView tv binding.sampleText;tv.setText(stringFromJNI());}/*** A native method that is implemented by the jni native library,* which is packaged with this application.*/public native String stringFromJNI(); }可以看到我们在java代码中通过System.loadLibrary(“jni”) 去加载了libjni.so库这是JNI的固定写法然后我们用public native String stringFromJNI()声明了一个JNI方法并通过tv.setText(stringFromJNI())去调用了它Java声明JNI方法一定要用native修饰这也是固定写法。   我们回到native-lib.cpp开始就是常规的include头文件不用说从extern C开始说   extern “C”是为了确保NDK按照 C 语言的方式来处理函数名称。C 和 C 语言在编译时对函数名称的处理方式有所不同C 会进行名称修饰name mangling而 C 不会。下面举个例子——》如果没有 extern “C”C 编译器会对函数名进行名称修饰编译器会把函数名 Java_com_example_myapp_MainActivity_stringFromJNI 转换为一个复杂的符号比如_ZN12MainActivity13stringFromJNIEv 。 这就是 C 的名称修饰。Java 层的 JNI 机制无法识别这个符号会导致无法正确调用该方法。   JNIEXPORT jstring JNICALLJNIEXPORT 、JNICALL都是固定写法它们中间夹着的jstring 是函数的返回值还记得我们之前声明的native函数吗public native String stringFromJNI() 这里的jstring对应的就是java中的String。JNI为了实现java和C/C的通信规定了一套基本数据类型的对照表   Java_com_htc_jni_MainActivity_stringFromJNI(JNIEnv* env, jobject thiz)这个是stringFromJNI对应的JNI函数的名称Java_是固定前缀com_htc_jni_MainActivity_stringFromJNI是Java中声明的native方法所在的包名类名native方法名的组合组合的顺序也是固定的。方法参数有两个JNIEnv* env, jobject这里有同学就会问了我们Java里声明的native方法明明没有参数怎么这里有参数public native String stringFromJNI() 是没参数所以这里的JNIEnv* env, jobject thiz其实固定写法JNIEnv* env代表的是Java环境后续可以通过env创建Java中的数据对象访问Java中的函数jobect thiz表示调用这个方法的 Java 对象实例在这个例子中指的就是MainActivity如果声明成静态方法 public static native String stringFromJNI() jobject就需要更换成 jclass。 1.4 总结 如果从一开始就创建一个Native C 项目Android Studio会帮我们把JNI需要的环境和文件都准备好我们只需要学习它的语法即可其中有几个需要关注的点1、cpp目录和java目录是同级。2、Java代码中通过System.loadLibrary加载so动态库、通过native关键字声明JNI方法。3、cpp文件JNI方法的命名规则和参数规范。 点击下载Native C项目Demohttps://github.com/xuhao120833/JN 二、一般的项目中途想使用C/C代码 上面我们讲了用Android Studio创建一般的Native C项目Android Studio 已经帮我们做好了准备工作那么我们APP开发到一半突然加了个需求对时效性要求很高必须用C/C实现这个时候项目肯定不能推倒重来了那怎么办按下面的操作即可 2.1 自己创建cpp目录和相关文件 选中main目录按右键在java的同级目录创建cpp目录   自己创建CMakeLists.txt、编写用到的cpp文件和头文件   CMakeLists.txt的文件内容如下 # For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.10.2) #说明CMake的最低要求版本# Declares and names the project.project(Xctouch) #CMake项目名称取为Xctouch随意取的# Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.PxScale #最后生成的库的名字取为PxScale# Sets the library as a shared library.SHARED #设置最后生成库为动态库即.so文件# Provides a relative path to your source file(s).scdefine.h #列出所有的cpp文件和头文件每个文件占一行jz_scale.cpptp_savedata_check.cppscJNIfun.cpp)# Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build.find_library( # Sets the name of the path variable. log-lib #找到NDK里面现成的log库并给它取一个别名log-lib# Specifies the name of the NDK library that# you want CMake to locate.log )# Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library. #将log库链接到最后生成的libPxScale.so库中PxScale# Links the target library to the log library# included in the NDK.${log-lib} )scJNIfun.cpp的文件内容如下定义了三个方法前两个相对复杂一些extern 导入了两个其它cpp文件的方法。 #include jni.h #include string #include android/log.h #include linux/agpgart.h #include scdefine.hJNIEXPORT/// extern void ratio_tra_point(int *pRet, int *px4, int *py4, int oldRatio, int newRatio, float scale, int w, int h); extern int check_bd_data(char *pBuf); xtouch/////该方法的功能是将 Java 层的两个 int[] 数组px4 和 py4传递到 C 层经过 ratio_tra_point 函数处理后返回一个计算结果数组。 extern C JNIEXPORT jintArray JNICALL Java_com_htc_server_PxScale_getpxRatioxy(JNIEnv *env, jobject thiz, jintArray px4, jintArray py4, jint oldRatio, jint newRatio, jfloat scale, jint w, jint h) {jintArray intArray env-NewIntArray(20);jint *intdata env-GetIntArrayElements(intArray, NULL);jint *tpx4 NULL;jint *tpy4 NULL;if(px4!NULL){tpx4 (jint *) env-GetIntArrayElements(px4, 0);}else{tpx4 NULL;}if(py4!NULL){tpy4 (jint *) env-GetIntArrayElements(py4, 0);}else{tpy4 NULL;}LOGD(CPP: Java_com_htc_server_PxScale_getpxRatioxy);memset(intdata,0,sizeof(int)*20);ratio_tra_point(intdata, tpx4, tpy4, oldRatio,newRatio, scale, w, h);env-ReleaseIntArrayElements(intArray, intdata, 0);return intArray; }//Java 层接收一个字符串将其进行字符过滤只保留字母和数字然后调用 check_bd_data 函数对过滤后的数据进行进一步处理最终返回一个整型结果。 extern C JNIEXPORT jint JNICALL Java_com_htc_server_PxScale_checkbddata(JNIEnv *env, jobject thiz, jstring data) {jint ret 0;int i;char c,checkbuf[2048];const char *str;//LOGD(CPP: Java_com_htc_server_PxScale_checkbddata);str env-GetStringUTFChars(data, NULL);if(str NULL){return 0;}else{memset(checkbuf, 0, 2048);for(i0;i2048;i){c str[i];if((c0 c9) || (ca cz) || (cA cZ)){checkbuf[i] c;}else{break;}}ret check_bd_data(checkbuf);}return ret; }extern C JNIEXPORT jstring JNICALL Java_com_htc_server_PxScale_sayHello(JNIEnv* env,jobject thiz) {// 创建 C 字符串std::string message Im JNI, Hello Java.;// 返回一个 Java 字符串jstringreturn env-NewStringUTF(message.c_str()); }2.2 java目录下创建PxScale.java文件 package com.htc.server;public class PxScale {static {System.loadLibrary(PxScale);}/*** A native method that is implemented by the duRYXtp native library,* which is packaged with this application.**/public native int[] getpxRatioxy(int[] px4, int[] py4, int oldRatio, int newRatio, float scale, int w, int h);public native int checkbddata(String data);public native String sayHello(); }2.3 build.gradle下添加NDK、JNI配置 我们需要手动指定CMakeLists.txt的位置和版本、ndk的版本、ndk最后编译产物的信息。   defaultConfig标签范围内添加如下标签信息 externalNativeBuild {cmake {cppFlags }}ndk {ldLibs logmoduleName PxScale //生成的so名字。abiFilters arm64-v8a, armeabi-v7a //输出指定abi体系结构下的so库。}android标签范围内添加如下标签信息 externalNativeBuild {cmake {path src/main/cpp/CMakeLists.txt //指定CMakeLists的绝对路径。version 3.10.2 //指定CMake的版本。}}ndkVersion 21.0.6113669 //指定ndk的版本。注意build.gradle中不要打开代码混淆开关打开会导致Java函数可以找到C/C函数但是C/C函数执行完毕返回值给Java函数的时候找不到Java函数导致执行报错。解决办法暂时未知。 buildTypes {release {minifyEnabled false //true混淆打开//zipAlignEnabled true //优化代码//shrinkResources true //优化资源proguardFiles getDefaultProguardFile(proguard-android-optimize.txt), proguard-rules.pro}}2.4 MainActivity调用JNI测试 MainActivity的代码如下 package com.htc.server;import android.os.Bundle; import android.widget.Toast;import androidx.activity.EdgeToEdge; import androidx.annotation.Px; import androidx.appcompat.app.AppCompatActivity; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat;public class MainActivity extends AppCompatActivity {private PxScale pxScale;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);pxScale new PxScale();pxScale.checkbddata(zojdoifjaoidj);String hello pxScale.sayHello();Toast.makeText(this, hello, Toast.LENGTH_SHORT).show();} }2.5 总结 一般项目和NativeC项目相比想要使用JNI需要自己去创建cpp目录、cpp文件、CMakeLists.txt等这些文件的组成语法都是大同小异的唯一值得注意的就是需要自己在build.gradle中去声明项目JNI的相关信息不然无法运行。 点击下载 一般项目使用JNI 的 Demo APPhttps://github.com/xuhao120833/Server 三、C/C调用Java方法 前文举例子一直用的都是Java通过JNI去调用C/C那么反过来怎么弄呢 从C/C使用JNI调用Java方法分两种情况一种是调用static静态方法一种是调用普通方法下面就分这两种情况分别说。以创建Native C项目为例子。 3.1 调用静态方法 首先我们按照上文的方法创建一个Native C项目然后创建一个ArrayProcessor类供C/C代码调用最后的代码结构如下   在ArrayProcessor中首先定义一个简单的静态方法 package com.htc.jni2;public class ArrayProcessor {// 静态方法打印日志public static void printLog(String message) {System.out.println(Log from Java: message);} } MainActivity中定义一个native方法callSumArray并调用它 package com.htc.jni2;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.widget.TextView;import com.htc.jni2.databinding.ActivityMainBinding;public class MainActivity extends AppCompatActivity {// Used to load the jni2 library on application startup.static {System.loadLibrary(jni2);}/*** A native method that is implemented by the jni2 native library,* which is packaged with this application.*/public native String stringFromJNI();public native int callSumArray(int[] array);private ActivityMainBinding binding;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);binding ActivityMainBinding.inflate(getLayoutInflater());setContentView(binding.getRoot());// Example of a call to a native methodTextView tv binding.sampleText;tv.setText(stringFromJNI());// 创建一个整数数组int[] array {1, 2, 3, 4, 5};// 调用 JNI 方法int sum callSumArray(array);// 打印结果System.out.println(Sum of array: sum);} }JNI中对应定义一个callSumArray方法 #include jni.h #include string #include android/log.h#define LOG_TAG JNI_LOG #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)extern C JNIEXPORT jstring JNICALL Java_com_htc_jni2_MainActivity_stringFromJNI(JNIEnv* env,jobject thiz) {std::string hello Hello from C;return env-NewStringUTF(hello.c_str()); }extern C JNIEXPORT jint JNICALL Java_com_htc_jni2_MainActivity_callSumArray(JNIEnv* env,jobject thiz,jintArray array) {// 获取数组长度jsize length env-GetArrayLength(array);// 获取数组元素jint *elements env-GetIntArrayElements(array, 0);// 调用静态方法 printLogjclass arrayProcessorClass env-FindClass(com/htc/jni2/ArrayProcessor);if (arrayProcessorClass nullptr) {LOGI(Class not found!);return -1; // 错误返回}jmethodID printLogMethod env-GetStaticMethodID(arrayProcessorClass, printLog, (Ljava/lang/String;)V);if (printLogMethod nullptr) {LOGI(Method not found!);return -1; // 错误返回}// 调用静态方法 printLogjstring logMessage env-NewStringUTF(Hello from JNI!);env-CallStaticVoidMethod(arrayProcessorClass, printLogMethod, logMessage). . . . . . }要想在C/C代码中调用Java的静态方法总共分如下三步   1、通过env-FindClass找到指定路径的Java类。   2、通过env-GetStaticMethodID指定Java类中的静态方法的jmethodID 。   解释一下函数中的参数意义GetStaticMethodID(arrayProcessorClass, “printLog”, “(Ljava/lang/String;)V”)arrayProcessorClass是第一步找到的jclass——》 “printLog是Java中对应函数的名字。——》”(Ljava/lang/String;)V) 对应方法的签名说人话就是告诉JNI要使用的Java函数的参数是什么返回类型是什么(Ljava/lang/String;)指printLog的参数是StringV指printLog的返回类型是void。对照表如下    3、通过env-CallStaticVoidMethod调用Java中的printLog函数。有一个参数就是env-CallStaticVoidMethod(arrayProcessorClass, printLogMethod, logMessage)多个参数就是env-CallStaticVoidMethod(arrayProcessorClass, printLogMethod, 参数1参数2参数3. . . . . .) 到这里C/C如何调用静态Java方法就说完了。 3.2 调用普通实例方法 我们在ArrayProcessor中再定义两个普通方法 // 非静态方法计算数组的和public int sumArray(int[] array) {int sum 0;for (int num : array) {sum num;}return sum;}// 非静态方法打印数组public void printArray(int[] array) {System.out.print(Array: );for (int num : array) {System.out.print(num );}System.out.println();}JNI cpp文件中如下调用 // 获取数组长度jsize length env-GetArrayLength(array);// 获取数组元素jint *elements env-GetIntArrayElements(array, 0);// 获取构造函数 IDjmethodID constructor env-GetMethodID(arrayProcessorClass, init, ()V);if (constructor nullptr) {LOGI(Constructor not found!);return -1; // 错误返回}// 创建 ArrayProcessor 类的实例对象jobject arrayProcessorObject env-NewObject(arrayProcessorClass, constructor);// 获取实例方法 sumArray 的 IDjmethodID sumArrayMethod env-GetMethodID(arrayProcessorClass, sumArray, ([I)I);if (sumArrayMethod nullptr) {LOGI(sumArray method not found!);return -1; // 错误返回}// 调用实例方法 sumArrayjint result env-CallIntMethod(arrayProcessorObject, sumArrayMethod, array);// 调用实例方法 printArrayjmethodID printArrayMethod env-GetMethodID(arrayProcessorClass, printArray, ([I)V);if (printArrayMethod nullptr) {LOGI(printArray method not found!);return -1; // 错误返回}env-CallVoidMethod(arrayProcessorObject, printArrayMethod, array);// 释放数组元素env-ReleaseIntArrayElements(array, elements, 0);return result;1、我们都知道实例方法需要实例对象才能调用所以第一步就是要构建ArrayProcessor的实例对象通过env-GetMethodID(arrayProcessorClass, init , “()V”);找到ArrayProcessor的构造方法 init JNI中固定代表构造方法()V就是说构造函数没有参数返回类型为void也就是Java中默认的构造函数。   2、通过 env-NewObject(arrayProcessorClass, constructor);获取到ArrayProcessor的实例对象。 JNI中用jobject 表示。   3、通过env-GetMethodID分别获取到sumArray和printArray的jmethodID。   4、通过CallIntMethod、CallVoidMethod调用Java中的静态方法。   最后把我们创建的指针释放掉env-ReleaseIntArrayElements。第四步中用的CallIntMethod、CallVoidMethod是根据Java函数的返回类型来的有一个对照表如下 3.3 总结 C/C使用JNI调用Java方法过程是相似的1、静态方法——》先找到类然后调用。2、普通方法——》先创建Java实例对象再通过Java对象调用。调用方法前都需要找到 jmethodID流程都是固定的重点在于正确使用JNI规定的语法。 点击下载C/C通过JNI调用Java代码的Demo APP https://github.com/xuhao120833/JNI2 写在最后 通篇看下来其实JNI没有想象中那么难更多的是学习现有的规则和语法然后再正确使用即可。生活中很多事也是这样我们大多数时候在自己吓自己。不管你怕不怕生活最重要的是勇敢出发。
http://www.hkea.cn/news/14449424/

相关文章:

  • php做网站需要的软件李江seo
  • 网站建设要花多少钱征信报告
  • 网站建设策划公司地址wordpress前台打开速度20秒
  • 福州做网站公司排名网络违法犯罪网站举报
  • 做食品怎样选网站绵阳网站建设网站建设
  • 怎么看出网站是dede做的上海微网站建设
  • 易语言如何做网站吗wordpress主题如何修改logo
  • 开发大型网站岳阳网站开发公司推荐
  • 成都网站seo费用搭建个人主页
  • 生产型或服务型企业网站有哪些需求分析 网站
  • 做什么网站哪个做网站公司好
  • 门户网站建设经济交流材料网站建设的简历范文
  • 网站后台界面 园林设计wordpress会员微信支付宝
  • 手机怎么做优惠券网站福步外贸论坛找客户
  • 一般网站 广告番禺网站建设报价
  • 公司网站建设会计你分录西安网站开发公司定制
  • 社交网站制作杭州的互联网公司有哪些
  • 做视频网站用什么服务器上海做营销网站哪个公司好
  • 做画册的网站谷歌google官方下载
  • 上海品牌网站建设公济南网站建设sdjy6
  • 义乌1688网站网页设计新手做电商卖什么好
  • 汕头制作网站关于做网站的书
  • 上海 专业网站建设wordpress相册火车头发布
  • 柳州市网站建设新乡市网站建设电脑培训班
  • 响应式网站建设推荐乐云践新丹江口网站建设
  • 微信公众号的微网站怎么做的c 网站开发实例教程
  • 建设一个电商网站的步骤重庆丰都建设局网站
  • 网站用什么做seo关键词优化推广哪家好
  • 免费手机图片编辑器重庆网站优化排名推广
  • 网站文章只被收录网站首页网站描述怎么写利于seo