java端:
File file = new File("1.jpg");
BufferedImage bi = null;
try {
bi = ImageIO.read(file);
} catch (Exception e) {
e.printStackTrace();
}
int width = bi.getWidth();
int height = bi.getHeight();
int minx = bi.getMinX();
int miny = bi.getMinY();
int length = (width - minx) * (height - miny);
byte[] r = new byte[length];
byte[] g = new byte[length];
byte[] b = new byte[length];
int size = 0;
for (int j = miny; j < height; j++) {
for (int i = minx; i < width; i++) {
int pixel = bi.getRGB(i, j);
b[size] = (byte)((pixel & 0xff0000) >> 16);
g[size]= (byte)((pixel & 0xff00) >> 8);
r[size] = (byte)(pixel & 0xff);
size++;
}
}
//调整RGB,假设出问题
String s = getIdByByte(r,g,b,height,width);
c++头文件:
JNIEXPORT jstring JNICALL Java_com_maxvision_orc_getIdByByte
(JNIEnv *, jclass, jbyteArray, jbyteArray, jbyteArray, jint, jint);
c++实现:
extern "C"
JNIEXPORT jstring JNICALL Java_com_maxvision_orc_getIdByByte(
JNIEnv *env,jclass obj /* this */, jbyteArray R , jbyteArray G, jbyteArray B, jint h, jint w)
{
jsize R_len = env->GetArrayLength(R);
jbyte * R_data = env->GetByteArrayElements(R, 0);
jsize G_len = env->GetArrayLength(G);
jbyte * G_data = env->GetByteArrayElements(G, 0);
jsize B_len = env->GetArrayLength(B);
jbyte * B_data = env->GetByteArrayElements(B, 0);
/*
*
* First Check Image Data
*
*
*/
if (R_data == NULL || G_data ==NULL || B_data ==NULL) {
std::ostringstream stringStream;
stringStream <<" can not open image...";
return env->NewStringUTF(stringStream.str().c_str());
}
cv::Mat imgData_R(h, w, CV_8UC1, (unsigned char *) R_data);
cv::Mat imgData_G(h, w, CV_8UC1, (unsigned char *) G_data);
cv::Mat imgData_B(h, w, CV_8UC1, (unsigned char *) B_data);
std::vector<cv::Mat> channels;
channels.push_back(imgData_R);
channels.push_back(imgData_G);
channels.push_back(imgData_B);
cv::Mat image(h, w, CV_8UC3);
cv::merge(channels, image);
cv::imwrite("test.jpg",image);
...//后续代码
}
这里RGB3个通道的顺序可能有点问题。请通过查看test.jpg进行查看确认。