将linux下的QT作为生成和调用so的IDE,这使得我们省去了编写makefile或者cmake的步骤。

1、生成so

代码如下:(生成so的工程代码请到这下载

main.cpp

#include "main.h"

//调用该so内的下面函数时,传进的是Mat图像的data,传出的是处理后的Mat图像的data,而不是Mat格式的传递
void drawCircle_Mat2Arry(int rows, int cols, unsigned char *src_data , unsigned char *dst_data)
{
    //将unsigned char转换成Mat
    Mat src = Mat(rows, cols, CV_8UC3, src_data); //8:每个像素点在内存空间所占的空间大小8bite;3:带Alpha通道的RGB彩色图像
    //在图像上画一个红色的圆
    circle(src, Point(50, 50), 20, Scalar(0, 0, 255), 5);
    //将Mat转换成unsigned char
    memcpy(dst_data, src.data, rows*cols * 3);  //3:带Alpha通道的RGB彩色图像
}

//Mat格式的图片作为参数传递
void drawCircle_passMat(Mat& srcImg, Mat& dstImg)
{
    //将图片转为灰度图
    cvtColor(srcImg,dstImg,COLOR_BGR2GRAY);
    //在传进的彩色图像上画一个红色的圆
    circle(srcImg, Point(50, 50), 20, Scalar(0, 0, 255), 5);
    imwrite("save.jpg",srcImg);
}

//test
int hello(int a, int b)
{
    printf("hello!!!!!,test the shared library!!\n");
    return a+b;
}

main.h

#ifndef MAIN_H_INCLUDED
#define MAIN_H_INCLUDED

#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;

extern "C"
{
    void drawCircle_Mat2Arry(int rows, int cols, unsigned char *src_data , unsigned char *dst_data);
    void drawCircle_passMat(Mat& srcImg, Mat& dstImg);
    int hello(int a, int b);
}

#endif // MAIN_H_INCLUDED

2、调用so

调用so的工程代码请到这下载

main.cpp

#include <iostream>
//#include "qtfaultprj.h"
// #include "MSFRecg.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace std;
using namespace cv;

extern "C"
{
    void drawCircle_Mat2Arry(int rows, int cols, unsigned char *src_data , unsigned char *dst_data);
    void drawCircle_passMat(Mat& srcImg, Mat& dstImg);
    int hello(int a, int b);
}

int main()
{
    Mat src = imread("123.jpg");

    if(src.empty())
        cout<<"can not open 123.jpg"<<endl;
    else {
        imshow("src",src);
        //测试传递进so里的是Mat格式的data,,,传出参也是Mat格式的data
        unsigned char* getImgFromArry = (unsigned char*)malloc(src.rows * src.cols * 3 * sizeof(unsigned char)); //3:带Alpha通道的RGB彩色图像
        memset(getImgFromArry, 0, src.rows * src.cols * 3 * sizeof(unsigned char));
        drawCircle_Mat2Arry(src.rows, src.cols, src.data, getImgFromArry);
        //将unsigned char转换成Mat
        Mat src1 = Mat(src.rows,src.cols, CV_8UC3, getImgFromArry);//8:每个像素点在内存空间所占的空间大小8bite;3:带Alpha通道的RGB彩色图像
        imshow("getImgFromArry",src1);

        //测试传递进so函数里的是Mat格式,传递出来的也是Mat
        Mat dstImg;
        drawCircle_passMat(src, dstImg);//这里将src传递进去后会画一个圆圈到src上,通过显示该src可知这个处理后的src是处理后也就是画上圈后的src
        imshow("after src",src);
        imshow("getDstImg",dstImg);

        int result = hello(3,9);
        cout<<result<<endl;

        waitKey(0);
        src.release();
        src1.release();
        dstImg.release();
        free(getImgFromArry);
    }

    return 0;
}

3、原图

4、执行效果

 

 

Logo

更多推荐