- java文件
- MyInterface
public interface MyInterface { public void ShowMsg(String msg); }
- SumDemo
public class SumDemo { static { System.loadLibrary("native-lib"); } public native void CallRet(MyInterface myInterface); }
- Activity
package com.safeluck.facedetect.mycamera; import android.annotation.SuppressLint; import android.app.Activity; import android.app.ProgressDialog; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.FaceDetector; import android.net.Uri; import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.support.annotation.Nullable; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import com.google.gson.Gson; import com.safeluck.facedetect.mycamera.cpp.CustomInterface; import com.safeluck.facedetect.mycamera.cpp.MyInterface; import com.safeluck.facedetect.mycamera.cpp.SumDemo; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; /** * aaa * Created by lzw on 2018/5/25. 13:17:31 * 邮箱:[email protected] * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class SecondActivity extends Activity implements MyInterface { private static final int REQUECT_CODE_SEL_PIC = 120; private static final int MAX_FACES = 4; private Bitmap bm; @BindView(R.id.iv) ImageView image; private boolean hasDetected = false; //标记是否检测到人脸 private ProgressDialog progressDialog; private Paint paint; private int i = 0; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_layout); ButterKnife.bind(this); progressDialog = new ProgressDialog(this); progressDialog.setTitle("提示"); progressDialog.setMessage("正在检测,请稍等"); initPaint(); } private void initPaint() { paint = new Paint(); paint.setAntiAlias(true); paint.setColor(Color.BLUE); paint.setStrokeWidth(2); //设置画出的是空心方框而不是实心 paint.setStyle(Paint.Style.STROKE); } @OnClick({R.id.btn_select,R.id.btn_detect}) public void clickDDemo(View view) { switch (view.getId()) { case R.id.btn_detect: detectFace(); getIpWhere("123.145.106.87"); break; case R.id.btn_select: // selectPicture(); if (i<3){ image.setImageLevel(i++); }else{ i=0; } SumDemo sumDemo = new SumDemo(); sumDemo.CallRet(this); // Toast.makeText(this, SumDemo.helloFromJni(),Toast.LENGTH_SHORT).show(); break; default: break; } } private void detectFace() { if (bm == null) { Toast.makeText(this, R.string.pls_select, Toast.LENGTH_SHORT).show(); return; } if (hasDetected) { Toast.makeText(this, R.string.has_detected, Toast.LENGTH_SHORT).show(); return; } new futureTask().execute(); } @Override public void ShowMsg(String msg) { Log.i(TAG,"收到回调:"+msg); } class futureTask extends AsyncTask<Void, Void, FaceDetector.Face[]> { @Override protected FaceDetector.Face[] doInBackground(Void... voids) { FaceDetector faceDetector = new FaceDetector(bm.getWidth(), bm.getHeight(), MAX_FACES); FaceDetector.Face[] faces = new FaceDetector.Face[MAX_FACES]; faceDetector.findFaces(bm, faces); if (faces.length > 0) { return faces; }else{ return null; } } @Override protected void onPreExecute() { super.onPreExecute(); progressDialog.show(); } @Override protected void onPostExecute(FaceDetector.Face[] faces) { super.onPostExecute(faces); progressDialog.dismiss(); if (faces == null){ Toast.makeText(SecondActivity.this, R.string.sorry,Toast.LENGTH_SHORT).show(); }else{ drawFacesArea(faces); } } } private void drawFacesArea(FaceDetector.Face[] faces) { Canvas canvas = new Canvas(bm); float eyesDistance = 0f; for (int i=0; i<faces.length; i++){ FaceDetector.Face face = faces[i]; if (face != null){ PointF pointF = new PointF(); face.getMidPoint(pointF); eyesDistance = face.eyesDistance(); canvas.drawRect(pointF.x - eyesDistance,pointF.y-eyesDistance, pointF.x+eyesDistance,pointF.y+eyesDistance,paint); } } } private static final String TAG = "SecondActivity"; }
- cpp文件
#include <string> #include "mydemo.h" using namespace std; int sum(int32_t a, int32_t b){ string c = "123"; int cc =atoi(c.c_str())+90; LOGD("cc=%d",cc); return a+b; } JNIEXPORT void JNICALL Java_com_safeluck_facedetect_mycamera_cpp_SumDemo_CallRet(JNIEnv *env, jobject instance, jobject myInterface){ jclass jclass1 = env->GetObjectClass(myInterface); jmethodID jmethodID1 = env->GetMethodID(jclass1,"ShowMsg","(Ljava/lang/String;)V"); const char * msg= "Hello ,Kitty"; jstring jstring1=env->NewStringUTF(msg); env->CallVoidMethod(myInterface,jmethodID1,jstring1); env->DeleteLocalRef(jstring1); }
- h文件
#ifndef AAA_MYDEMO_H #define AAA_MYDEMO_H #include <jni.h> #include <string.h> #include <android/log.h> #include <stdlib.h> #include <stdio.h> #define LOG_TAG "System.out" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) int sum(int32_t a, int32_t b); #endif //AAA_MYDEMO_H extern "C" JNIEXPORT void JNICALL Java_com_safeluck_facedetect_mycamera_cpp_SumDemo_CallRet(JNIEnv *env, jobject instance, jobject myInterface) ;
- attention
- MyInterface
JNI开发--C调用java的方法
猜你喜欢
转载自www.cnblogs.com/endian11/p/9156548.html
今日推荐
周排行