1
我只想將Java對象從Java類傳遞給jni,並且需要對jni進行的更改並返回到java類。這是我的樣本項目,並得到了一些問題......如何將Mat對象從java類傳遞給jni cpp。
public class MainActivity extends Activity {
Button btn;
ImageView img;
Mat m;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView1);
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
selectImage();
}
});
}
protected void selectImage() {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
public native Mat image_mat(long matAddr);
/** Load the native library where the native method
* is stored.
*/
static {
System.loadLibrary("image-mat");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory()
.toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bm;
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),
btmapOptions);
// bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
// img.setImageBitmap(bm);
String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "mk";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System
.currentTimeMillis()) + ".jpg");
Bitmap imgMat;
BitmapFactory.Options bMat = new BitmapFactory.Options();
imgMat = BitmapFactory.decodeFile(file.getAbsolutePath(),bMat);
m = new Mat();
Mat ret = new Mat();
Utils.bitmapToMat(imgMat, m);
ret = image_mat(m.getNativeObjAddr());
Bitmap bmp = Bitmap.createBitmap(640, 480, Bitmap.Config.ARGB_8888);
Utils.matToBitmap(ret, bmp);
}
}
}
這是我的JNI
JNIEXPORT jlong JNICALL
Java_com_example_matusingnative_MainActivity_image_mat
(JNIEnv *env, jobject obj, jlong matimage)
{
cv::Mat *jni_image = (cv::Mat*) matimage;
// Mat *retval;
return (jlong)jni_image;
}
我建立上面的代碼中,我得到的問題
d:/OpenCV-2.4.9 -android -sdk \ sdk \ native \ jni/include/opencv2 \ core \ core.hpp:56:21:致命錯誤:算法:沒有這樣的文件或目錄 #include ^ 編譯終止。錯誤1