Created
November 9, 2014 05:32
-
-
Save sheimi/993fd6b155cc745b69d5 to your computer and use it in GitHub Desktop.
code in blog.sheimi.me: 2012-09-05-a-summary-of-using-jni (2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//cvjni.cpp | |
#include "cvjni.h" | |
#include <opencv2/opencv.hpp> | |
#include <vector> | |
#include <iostream> | |
using namespace std; | |
using namespace cv; | |
/* | |
* Class: CVJNI | |
* Method: toBMP | |
* Signature: ([B)[B | |
*/ | |
JNIEXPORT jbyteArray JNICALL Java_CVJNI_toBMP (JNIEnv * env, jclass jc, jbyteArray jba) { | |
cout << "This is in JNI" << endl; | |
//convert jbyteArray to vector<char> | |
unsigned char * isCopy; | |
jbyte* jbae = env->GetByteArrayElements(jba, isCopy); | |
jsize len = env->GetArrayLength(jba); | |
char * imageSource = (char *)jbae; | |
vector<char> imageSourceV; | |
for (int i = 0; i < len; i++) { | |
imageSourceV.push_back(imageSource[i]); | |
} | |
//convert format | |
Mat image = imdecode(imageSourceV, CV_LOAD_IMAGE_COLOR); | |
vector<unsigned char> imageDesV; | |
imencode(".bmp", image, imageDesV); | |
//convert vector<char> to jbyteArray | |
jbyte* result_e = new jbyte[imageDesV.size()]; | |
jbyteArray result = env->NewByteArray(imageDesV.size()); | |
for (int i = 0; i < imageDesV.size(); i++) { | |
result_e[i] = (jbyte)imageDesV[i]; | |
} | |
env->SetByteArrayRegion(result, 0, imageDesV.size(), result_e); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment