Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@sheimi
Created November 9, 2014 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheimi/993fd6b155cc745b69d5 to your computer and use it in GitHub Desktop.
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)
//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