简体   繁体   English

在Android中使用OpenCV的本机功能

[英]Using native functions in Android with OpenCV

I want to use OpenCV+Android, using native functions. 我想使用OpenCV + Android,使用原生函数。 However I am a little confused how to use bitmaps as parameters and how to return a value of an edited bitmap (or Mat). 但是我有点困惑如何使用位图作为参数以及如何返回已编辑的位图(或Mat)的值。

So for example I have a native function: 所以例如我有一个原生函数:

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>


JNIEXPORT ??? JNICALL Java_com_my_package_name_and_javaclass_myFunction(JNIEnv* env, jobject javaThis, cv::Mat mat1){
    //here will be code to perform filtering, blurring, canny edge detection or similar things.
        //so I want to input a bitmap, edit it and send it back to the Android class.

return ???
    }

So here I am using cv::Mat as a parameter. 所以这里我使用cv :: Mat作为参数。 I know this is wrong, but I am unsure what it should be, and what should be in the correpsonding java class. 我知道这是错的,但我不确定它应该是什么,以及在相应的java类中应该是什么。 Should it be a ByteArray? 它应该是ByteArray吗? And then in the above native function the parameter would be jByteArray (or similar)? 然后在上面的本机函数中,参数是jByteArray(或类似的)?

And for the return object, what should I put? 对于返回物体,我应该放什么? Should this be an array? 这应该是一个阵列吗?

Basically what I am looking for is in the Java class I have a Mat (or Bitmap) I send it to the native function for editing and return a nicely edited bitmap. 基本上我正在寻找的是在Java类中我有一个Mat(或Bitmap)我将它发送到本机函数进行编辑并返回一个编辑得很好的位图。

This is the OpenCV Tutorial code for Android. 这是Android的OpenCV教程代码。 I remember that it took a while for me to understand the JNI convention. 我记得我需要一段时间来理解JNI惯例。 Just look into JNI code first 先看看JNI代码

#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <vector>

using namespace std;
using namespace cv;

extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial3_Sample3View_FindFeatures(JNIEnv* env, jobject thiz, jint width, jint height, jbyteArray yuv, jintArray bgra)
{
    jbyte* _yuv  = env->GetByteArrayElements(yuv, 0);
    jint*  _bgra = env->GetIntArrayElements(bgra, 0);

    Mat myuv(height + height/2, width, CV_8UC1, (unsigned char *)_yuv);
    Mat mbgra(height, width, CV_8UC4, (unsigned char *)_bgra);
    Mat mgray(height, width, CV_8UC1, (unsigned char *)_yuv);

    //Please make attention about BGRA byte order
    //ARGB stored in java as int array becomes BGRA at native level
    cvtColor(myuv, mbgra, CV_YUV420sp2BGR, 4);

    vector<KeyPoint> v;

    FastFeatureDetector detector(50);
    detector.detect(mgray, v);
    for( size_t i = 0; i < v.size(); i++ )
        circle(mbgra, Point(v[i].pt.x, v[i].pt.y), 10, Scalar(0,0,255,255));

    env->ReleaseIntArrayElements(bgra, _bgra, 0);
    env->ReleaseByteArrayElements(yuv, _yuv, 0);
}
}

and then Java code 然后是Java代码

package org.opencv.samples.tutorial3;

import android.content.Context;
import android.graphics.Bitmap;

class Sample3View extends SampleViewBase {

    public Sample3View(Context context) {
        super(context);
    }

    @Override
    protected Bitmap processFrame(byte[] data) {
        int frameSize = getFrameWidth() * getFrameHeight();
        int[] rgba = new int[frameSize];

        FindFeatures(getFrameWidth(), getFrameHeight(), data, rgba);

        Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
        bmp.setPixels(rgba, 0/* offset */, getFrameWidth() /* stride */, 0, 0, getFrameWidth(), getFrameHeight());
        return bmp;
    }

    public native void FindFeatures(int width, int height, byte yuv[], int[] rgba);

    static {
        System.loadLibrary("native_sample");
    }
}

You'd better read default OpenCV for Android samples (native). 您最好阅读Android样本的默认OpenCV (本机)。

Of course, you cannot use cv::Mat as parameter, because this is c++ class not java. 当然,你不能使用cv::Mat作为参数,因为这是c ++类而不是java。 However if I'm not mistaken you can call c++ class methods from java source (it's also part of JNI). 但是如果我没弄错的话,你可以从java源代码中调用c ++类方法(它也是JNI的一部分)。

In your situation you have to use a pointer to image data (it may be uchar* or int* in c++, it's the same as byte[] or int[] in java). 在您的情况下,您必须使用指向图像数据的指针(它可能是c ++中的uchar*int* ,它与java中的byte[]int[]相同)。 For example, you can get pixels from Android Bitmap using method getPixels . 例如,您可以使用方法getPixels从Android Bitmap获取像素。 And in c++ you can use a specific mat constructor that takes a pointer to image data: 在c ++中,您可以使用特定的mat构造函数来获取指向图像数据的指针:

// constructor for matrix headers pointing to user-allocated data
Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP);
Mat(Size _size, int _type, void* _data, size_t _step=AUTO_STEP);

Hope, it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM