{"id":8813,"date":"2024-05-25T23:01:04","date_gmt":"2024-05-25T15:01:04","guid":{"rendered":""},"modified":"2024-05-25T23:01:04","modified_gmt":"2024-05-25T15:01:04","slug":"java jmi\u7684\u57fa\u672c\u601d\u60f3_jmi: JMI \u662f JNI \u7684 C++11\/14\u5c01\u88c5\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316JNI\u4f7f\u7528\u300c\u5efa\u8bae\u6536\u85cf\u300d","status":"publish","type":"post","link":"https:\/\/mushiming.com\/8813.html","title":{"rendered":"java jmi\u7684\u57fa\u672c\u601d\u60f3_jmi: JMI \u662f JNI \u7684 C++11\/14\u5c01\u88c5\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316JNI\u4f7f\u7528\u300c\u5efa\u8bae\u6536\u85cf\u300d"},"content":{"rendered":"
JMI<\/p>\n
JNI Modern Interface in C++<\/p>\n
\u7279\u6027<\/p>\n
\u652f\u6301 Java \u65b9\u6cd5\u8f93\u5165\u3001\u8f93\u51fa\u53c2\u6570<\/p>\n
jclass\u3001jmethodID\u3001field \u81ea\u52a8\u7f13\u5b58<\/p>\n
C++ \u548c Java \u65b9\u6cd5\u5c5e\u6027\u4e00\u81f4\uff0c\u5982\u9759\u6001\u65b9\u6cd5\u5bf9\u5e94 C++ \u9759\u6001\u6210\u5458\u51fd\u6570<\/p>\n
\u65e0\u9700\u64cd\u5fc3\u5c40\u90e8\u5f15\u7528\u6cc4\u6f0f<\/p>\n
getEnv() \u652f\u6301\u4efb\u610f\u7ebf\u7a0b\uff0c\u65e0\u9700\u5173\u5fc3 detach<\/p>\n
\u7f16\u8bd1\u5668\u63a8\u5bfc java \u7c7b\u578b\u53ca\u65b9\u6cd5\u7b7e\u540d\uff0c\u5e76\u53ea\u751f\u6210\u4e00\u6b21<\/p>\n
\u652f\u6301 JNI \u539f\u751f\u7684\u5404\u79cd\u7c7b\u578b\u3001jmi \u7684 JObject\u3001C\/C++ string \u53ca\u4e0a\u8ff0\u7c7b\u578b\u7684\u6570\u7ec4\u5f62\u5f0f(vector, valarray, array\u7b49) \u4f5c\u4e3a\u51fd\u6570\u53c2\u6570\u3001\u8fd4\u56de\u503c\u53ca field \u7c7b\u578b<\/p>\n
\u63d0\u4f9b\u4e86\u65b9\u4fbf\u4f7f\u7528\u7684\u5e38\u7528\u51fd\u6570: to_string(std::string), from_string(jstring), android::application()<\/p>\n
\u7b80\u5355\u6613\u7528\uff0c\u7528\u6237\u4ee3\u7801\u6781\u7b80<\/p>\n
\u4f8b\u5b50:<\/p>\n
JNI_OnLoad \u4e2d\u8bbe\u7f6e java vm: jmi::javaVM(vm);<\/p>\n
\u521b\u5efa SurfaceTexture:<\/p>\n
\/\/ \u5728\u4efb\u610f jmi::JObject \u53ef\u89c1\u8303\u56f4\u5185\u5b9a\u4e49 SurfaceTexture tag \u7c7b<\/p>\n
struct SurfaceTexture : jmi::ClassTag { static std::string name() {return \"android\/graphics\/SurfaceTexture\";}};<\/p>\n
...<\/p>\n
GLuint tex = ...<\/p>\n
...<\/p>\n
jmi::JObject texture;<\/p>\n
if (!texture.create(tex)) {
\n <\/p>\n
\/\/ texture.error() ...<\/p>\n
}<\/p>\n
\u4ece SurfaceTexture \u6784\u9020 Surface:<\/p>\n
struct Surface : jmi::ClassTag { static std::string name() {return \"android.view.Surface\";}}; \/\/ '.' or '\/'<\/p>\n
...<\/p>\n
jmi::JObject surface;<\/p>\n
surface.create(texture);<\/p>\n
\u8c03\u7528 void \u65b9\u6cd5:<\/p>\n
texture.call(\"updateTexImage\");<\/p>\n
\u6216<\/p>\n
texture.call(\"updateTexImage\");<\/p>\n
\u8c03\u7528\u542b\u8f93\u51fa\u53c2\u6570\u7684\u65b9\u6cd5:<\/p>\n
float mat4[16]; \/\/ or std::array, valarray<\/p>\n
texture.call(\"getTransformMatrix\", std::ref(mat4)); \/\/ use std::ref() if parameter should be modified by jni method<\/p>\n
\u82e5\u51fa\u53c2\u7c7b\u578b\u662f JObject<...> \u6216\u5668\u5b50\u7c7b, \u5219\u53ef\u4e0d\u4f7f\u7528std::ref()\uff0c\u56e0\u4e3a\u5bf9\u8c61\u4e0d\u4f1a\u6539\u53d8\uff0c\u53ef\u80fd\u53ea\u662f\u67d0\u4e9bfields\u88ab\u65b9\u6cd5\u4fee\u6539\u4e86\uff0c\u5982<\/p>\n
MediaCodec::BufferInfo bi;<\/p>\n
bi.create();<\/p>\n
codec.dequeueOutputBuffer(bi, timeout); \/\/ bi is of type MediaCodec::BufferInfo&<\/p>\n
\u8c03\u7528\u6709\u8fd4\u56de\u503c\u7684\u65b9\u6cd5:<\/p>\n
jlong t = texture.call(\"getTimestamp\");<\/p>\n
jmethodID \u7f13\u5b58<\/p>\n
call\/callStatic(\"methodName\", ....) \u6bcf\u6b21\u90fd\u4f1a\u8c03\u7528 GetMethodID\/GetStaticMethodID(), \u800c call\/callStatic<...mtag>(...) \u53ea\u4f1a\u8c03\u7528\u4e00\u6b21, \u5176\u4e2d MTag \u662f jmi:MethodTag \u7684\u5b50\u7c7b\uff0c\u5b9e\u73b0\u4e86 static const char* name() { return \"methodName\";}.<\/p>\n
\/\/ GetMethodID() \u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684\u65b9\u6cd5\u53ea\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21<\/p>\n
struct UpdateTexImage : jmi::MethodTag { static const char* name() {return \"updateTexImage\";}};<\/p>\n
struct GetTimestamp : jmi::MethodTag { static const char* name() {return \"getTimestamp\";}};<\/p>\n
struct GetTransformMatrix : jmi::MethodTag { static const char* name() {return \"getTransformMatrix\";}};<\/p>\n
...<\/p>\n
texture.call(); \/\/ or texture.call();<\/p>\n
jlong t = texture.call();<\/p>\n
texture.call(std::ref(mat4)); \/\/ use std::ref() if parameter should be modified by jni method<\/p>\n
Field \u63a5\u53e3<\/p>\n
Field \u63a5\u53e3\u652f\u6301\u53ef\u7f13\u5b58\u548c\u65e0\u7f13\u5b58 jfieldID<\/p>\n
\u901a\u8fc7 FieldTag \u4f7f\u7528\u53ef\u7f13\u5b58 jfieldID<\/p>\n
JObject obj;<\/p>\n
...<\/p>\n
struct MyIntField : FieldTag { static const char* name() {return \"myIntFieldName\";} };<\/p>\n
auto ifield = obj.field();<\/p>\n
jfieldID ifid = ifield; \/\/ \u6216 ifield.id()<\/p>\n
ifield.set(1234);<\/p>\n
int ivalue = ifield; \/\/ \u6216 ifield.get();<\/p>\n
\/\/ \u9759\u6001 field \u4e5f\u4e00\u6837\uff0c\u9664\u4e86\u4f7f\u7528\u7684\u662f\u9759\u6001\u65b9\u6cd5 JObject::staticField<\/p>\n
struct MyStrFieldS : FieldTag { static const char* name() {return \"myStaticStrFieldName\";} };<\/p>\n
auto& ifields = JObject::staticField<:string myintfields>(); \/\/ it can be an ref<\/p>\n
jfieldID ifids = ifields; \/\/ \u6216 ifield.id()<\/p>\n
ifields.set(\"JMI static field test\");<\/p>\n
ifields = \"assign\";<\/p>\n
std::string ivalues = ifields; \/\/ \u6216 ifield.get();<\/p>\n
\u901a\u8fc7 field \u540d\u5b57\u4f7f\u7528\u65e0\u7f13\u5b58 jfieldID<\/p>\n
auto ifield = obj.field(\"myIntFieldName\");<\/p>\n
...<\/p>\n
\u7ed9 Java \u7c7b\u5199\u4e2a C++ \u7c7b<\/p>\n
\u521b\u5efa JObject \u6216\u628a\u5176\u5bf9\u8c61\u4f5c\u4e3a\u6210\u5458\u53d8\u91cf\uff0c\u6216\u4f7f\u7528 CRTP JObject\u3002 \u6bcf\u4e2a\u65b9\u6cd5\u662f\u60f3\u901a\u5e38\u4e0d\u8d85\u8fc73\u884c\u4ee3\u7801\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u5b8f\u4f7f\u6bcf\u4e2a\u65b9\u6cd5\u5b9e\u73b0\u53ea\u9700\u4e00\u884c\u4ee3\u7801\uff0c \u53c2\u89c1 JMITest \u53ca Project AND<\/p>\n
\u4f7f\u7528\u7f16\u8bd1\u5668\u751f\u6210\u7684\u7b7e\u540d<\/p>\n
\u6a21\u7248 string signature_of(T) \u8fd4\u56de\u7c7b\u578b T \u7684\u7b7e\u540d. T \u53ef\u4ee5\u662f JMI \u652f\u6301\u7684\u7c7b\u578b(\u9664\u4e86jobject\uff0c\u56e0\u4e3a\u5176\u7c7b\u578b\u662f\u8fd0\u884c\u65f6\u786e\u5b9a\u7684)\u3001reference_wrapper\u3001void \u53ca\u7531\u4ee5\u4e0a\u7c7b\u578b\u4f5c\u4e3a\u8fd4\u56de\u7c7b\u578b\u548c\u53c2\u6570\u7c7b\u578b\u7684\u51fd\u6570\u7c7b\u578b<\/p>\n
string signature_of(T) \u8fd4\u56de\u7684 string \u662f\u9759\u6001\u5b58\u50a8\u7684\uff0c\u4f7f\u7528 signature_of(...).data() \u662f\u5b89\u5168\u7684<\/p>\n
\u4f8b\u5b50:<\/p>\n
void native_test_impl(JNIEnv *env , jobject thiz, ...) {}<\/p>\n
staitc const JNINativeMethod gMethods[] = {
\n <\/p>\n
{\"native_test\", signature_of(native_test_impl).data(), native_test_impl},<\/p>\n
...<\/p>\n
};<\/p>\n
\u4e5f\u8bb8\u4f60\u53d1\u73b0\u4e86\u53ef\u4ee5\u7528\u5b8f\u6765\u7b80\u5316<\/p>\n
#define DEFINE_METHOD(M) {#M, signature_of(M##_impl).data(), M##_impl}<\/p>\n
staitc const JNINativeMethod gMethods[] = {
\n <\/p>\n
DEFINE_METHOD(native_test),<\/p>\n
...<\/p>\n
}<\/p>\n
\u5df2\u77e5\u95ee\u9898<\/p>\n
\u5982\u679c\u8fd4\u56de\u7c7b\u578b\u4e0e\u524d n \u4e2a\u53c2\u6570\u7c7b\u578b\u662f\u4e00\u6837\u7684\uff0c\u9700\u8981\u663e\u793a\u6307\u5b9a\u8fd9\u4e9b\u7c7b\u578b<\/p>\n
\u4e3a\u4ec0\u4e48 JObject \u662f\u4e2a\u6a21\u7248?<\/p>\n
\u4e3a\u4e86\u652f\u6301 jclass\u3001jmethodID\u3001jfieldID \u7f13\u5b58<\/p>\n
\u5df2\u6d4b\u7f16\u8bd1\u5668<\/p>\n
g++ >= 4.9, clang >= 3.4.2<\/p>\n
TODO<\/p>\n
modern C++ \u7c7b\u81ea\u52a8\u751f\u6210\u811a\u672c<\/p>\n
\u7b7e\u540d\u65e0\u9700\u8fd0\u884c\u65f6string<\/p>\n
MIT License<\/p>\n
Copyright (c) 2016-2019 WangBin<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"java jmi\u7684\u57fa\u672c\u601d\u60f3_jmi: JMI \u662f JNI \u7684 C++11\/14\u5c01\u88c5\uff0c\u76ee\u7684\u662f\u4e3a\u4e86\u7b80\u5316JNI\u4f7f\u7528\u300c\u5efa\u8bae\u6536\u85cf\u300dJMIJNIModernInterfaceinC++\u7279\u6027\u652f...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8813"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=8813"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/8813\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=8813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=8813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=8813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}