I am trying to draw a form using the OpenCV library in native code (i.e. in the jni folder in an Android app) and call my own method to return the image to Java code and display it.
How can I do that? I cannot find a simple Android application using OpenCV. I also set up the environment and samples presented for Android using OpenCV, for example, cvcamera, calibrations work fine. They used SWIG for the JNI interface. Is SWIG required? I did not use SWIG. I tried a lot and still try.
In Java, the code is as follows:
public class HelloJni extends Activity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ShapeView(this));
}
static {
System.loadLibrary("hello-jni");
}
}
class ShapeView extends View
{
private Bitmap mBitmap;
private static native Object drawingShape();
public ShapeView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
mBitmap = (Bitmap) drawingShape();
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
And in jni's own class
#include "ShapeView.h"
#include <string.h>
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
JNIEXPORT jobject JNICALL Java_ShapeView_drawingShape
(JNIEnv *env, jclass obj) {
IplImage* img = cvCreateImage(cvSize(300, 300), IPL_DEPTH_8U, 3);
cvRectangle(img, cvPoint(100, 100), cvPoint(200, 200), cvScalar(255, 0, 0), 1);
cvCircle(img, cvPoint(100, 100), 20, cvScalar(0, 255, 0), 1);
return (jobject)img;
}
, ndk-build. .so
. Java .
AVD, , .
?
SWIG , ? OpenCV SWIG.