1.考虑到硬件设备接口,第三方软件接口,图像接口,OPENCV接口,希望能够开发出通用的算法库,一劳永逸的解决各种复杂的使用场景,因此数据要支持YUV,支持ARGB,支持MAT
进入BIN目录的classes文件夹使用java -classpath . -jni 生成C头文件
#include <stdio.h>#include <jni.h>#include<Android/log.h> #include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp> using namespace std;using namespace cv; #define TAG "Camera XXXXX" // 锟斤拷锟斤拷锟斤拷远锟斤拷锟斤拷LOG锟侥憋拷识#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG,__VA_ARGS__) // 锟斤拷锟斤拷LOGD锟斤拷锟斤拷 #ifdef __cplusplusextern "C" {#endif/* * Class: ImgProc_ImageProc3 * Method: YuvNv21ToGray * Signature: ([BII[I)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21ToGray (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){ jbyte * pNV21FrameData = jenv->GetByteArrayElements(YuvNv21, 0); jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); Mat mNV(height, width, CV_8UC1, (unsigned char*) pNV21FrameData); Mat mBgra(height, width, CV_8UC4, (unsigned char*) poutPixels); cvtColor(mNV, mBgra, CV_YUV420sp2RGBA); jenv->ReleaseByteArrayElements(YuvNv21, pNV21FrameData, 0); jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: YuvNv21ToRGBA * Signature: ([BII[I)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21ToRGBA (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){ jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0); jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf); Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels); Mat tmp(height, width, CV_8UC4); cvtColor(image, tmp, CV_YUV420sp2RGBA); vector <Mat> channels; split(tmp, channels); Mat r = channels.at(0); Mat g = channels.at(1); Mat b = channels.at(2); Mat a = channels.at(3); vector <Mat> mbgr(4); mbgr[0] = b; mbgr[1] = g; mbgr[2] = r; mbgr[3] = a; merge(mbgr, rgba); jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0); jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: RgbaUndistort * Signature: ([III[I)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistort (JNIEnv *jenv, jclass jclassz, jintArray argb, jint width, jint height, jintArray pixels){ jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); jint * pinPixels = jenv->GetIntArrayElements(argb, 0); Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels); Mat in(height, width, CV_8UC4, (unsigned char*) pinPixels); double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); undistort(in, out, camMat, disMat); jenv->ReleaseIntArrayElements(argb, pinPixels, 0); jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: RgbaUndistort2 * Signature: ([[III[I)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistort2(JNIEnv *jenv, jclass jclassz, jobjectArray argb, jint width, jint height, jintArray pixels) { jint i, j; int row = jenv->GetArrayLength(argb); jintArray myarray = (jintArray)(jenv->GetObjectArrayElement(argb, 0)); int col = jenv->GetArrayLength(myarray); jint jniData[row][col]; LOGD("jiaXXX %s", "Java_ImgProc_ImageProc_convertRGB3"); for (i = 0; i < row; i++) { myarray = (jintArray)(jenv->GetObjectArrayElement(argb, i)); jint *coldata = jenv->GetIntArrayElements(myarray, 0); for (j = 0; j < col; j++) { jniData[i][j] = coldata[j]; LOGD("jiaXXX %d", jniData[i][j]); } jenv->ReleaseIntArrayElements(myarray, coldata, 0); } Mat img = Mat(row, col, CV_8UC4, jniData); LOGD("jiaXXX %x", img.at<unsigned int>(1, 1)); double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels); undistort(img, out, camMat, disMat); jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: RgbaUndistortMat * Signature: ([IIIJ)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbaUndistortMat (JNIEnv *jenv, jclass jclassz, jintArray argb, jint width, jint height, jlong pArgbOutMatAddr){ //jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); jint * pinPixels = jenv->GetIntArrayElements(argb, 0); //Mat out(height, width, CV_8UC4, (unsigned char*) poutPixels); Mat in(height, width, CV_8UC4, (unsigned char*) pinPixels); Mat out = *((Mat*)pArgbOutMatAddr); double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); undistort(in, out, camMat, disMat); jenv->ReleaseIntArrayElements(argb, pinPixels, 0); //jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: RgbMatUndistortMat * Signature: (JIIJ)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_RgbMatUndistortMat (JNIEnv *jenv, jclass jclassz, jlong pArgbMatAddr, jint width, jint height, jlong pArgbOutMatAddr){ Mat in=*((Mat*)pArgbMatAddr); Mat out = *((Mat*)pArgbOutMatAddr); double cam[] = {width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); undistort(in, out, camMat, disMat); return true;} /* * Class: ImgProc_ImageProc3 * Method: YuvNv21UndistortRgba * Signature: ([BII[I)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21UndistortRgba (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jintArray pixels){ jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0); jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf); Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels); Mat tmp(height, width, CV_8UC4); cvtColor(image, tmp, CV_YUV420sp2RGBA); double cam[] = { width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); undistort(tmp, tmp, camMat, disMat); vector < Mat > channels; split(tmp, channels); Mat r = channels.at(0); Mat g = channels.at(1); Mat b = channels.at(2); Mat a = channels.at(3); vector < Mat > mbgr(4); mbgr[0] = b; mbgr[1] = g; mbgr[2] = r; mbgr[3] = a; merge(mbgr, rgba); jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0); jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} /* * Class: ImgProc_ImageProc3 * Method: YuvNv21UndistortRgbaMat * Signature: ([BIIJ)Z */JNIEXPORT jboolean JNICALL Java_ImgProc_ImageProc3_YuvNv21UndistortRgbaMat (JNIEnv *jenv, jclass jclassz, jbyteArray YuvNv21, jint width, jint height, jlong pMatAddr){ jbyte * pBuf = (jbyte*) jenv->GetByteArrayElements(YuvNv21, 0); //jint * poutPixels = jenv->GetIntArrayElements(pixels, 0); Mat image(height + height / 2, width, CV_8UC1, (unsigned char *) pBuf); //Mat rgba(height, width, CV_8UC4, (unsigned char*) poutPixels); Mat rgba = *((Mat*) pMatAddr); Mat tmp(height, width, CV_8UC4); cvtColor(image, tmp, CV_YUV420sp2RGBA); double cam[] = { width, 0, width / 2, 0, height, height / 2, 0, 0, 1 }; double distort[] = { 0.1, 0.35, 0.0, 0.0, 0.01 }; Mat camMat = Mat(3, 3, CV_64FC1, cam); Mat disMat = Mat(5, 1, CV_64FC1, distort); undistort(tmp, tmp, camMat, disMat); vector < Mat > channels; split(tmp, channels); Mat r = channels.at(0); Mat g = channels.at(1); Mat b = channels.at(2); Mat a = channels.at(3); vector < Mat > mbgr(4); mbgr[0] = b; mbgr[1] = g; mbgr[2] = r; mbgr[3] = a; merge(mbgr, rgba); jenv->ReleaseByteArrayElements(YuvNv21, pBuf, 0); //jenv->ReleaseIntArrayElements(pixels, poutPixels, 0); return true;} #ifdef __cplusplus}#endif