- added missing loadLibrary for inner classes
- added zero check for pointers in utils.cpp
This commit is contained in:
parent
3511bf81de
commit
eea62ca6fb
@ -624,6 +624,7 @@ JNIEXPORT $rtype JNICALL Java_org_opencv_${module}_$fname
|
|||||||
)
|
)
|
||||||
|
|
||||||
self.java_code.write(indent_m + "// native stuff\n\n")
|
self.java_code.write(indent_m + "// native stuff\n\n")
|
||||||
|
self.java_code.write(indent_m + 'static { System.loadLibrary("opencv_java"); }\n')
|
||||||
self.java_code.write( jn_code.getvalue() )
|
self.java_code.write( jn_code.getvalue() )
|
||||||
self.java_code.write(
|
self.java_code.write(
|
||||||
"""
|
"""
|
||||||
|
@ -1,5 +1,9 @@
|
|||||||
#include <jni.h>
|
#include <jni.h>
|
||||||
|
|
||||||
|
#include "opencv2/core/core.hpp"
|
||||||
|
|
||||||
|
#include <android/bitmap.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
@ -9,44 +13,25 @@ extern "C" {
|
|||||||
* Method: nBitmapToMat(Bitmap b)
|
* Method: nBitmapToMat(Bitmap b)
|
||||||
* Signature: (L)J
|
* Signature: (L)J
|
||||||
*/
|
*/
|
||||||
JNIEXPORT jlong JNICALL Java_org_opencv_utils_nBitmapToMat
|
|
||||||
(JNIEnv *, jclass, jobject);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Class: org_opencv_utils
|
|
||||||
* Method: nBitmapToMat(long m, Bitmap b)
|
|
||||||
* Signature: (JL)Z
|
|
||||||
*/
|
|
||||||
JNIEXPORT jboolean JNICALL Java_org_opencv_utils_nMatToBitmap
|
|
||||||
(JNIEnv *, jclass, jlong, jobject);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include "opencv2/core/core.hpp"
|
|
||||||
|
|
||||||
#include <android/bitmap.h>
|
|
||||||
|
|
||||||
JNIEXPORT jlong JNICALL Java_org_opencv_utils_nBitmapToMat
|
JNIEXPORT jlong JNICALL Java_org_opencv_utils_nBitmapToMat
|
||||||
(JNIEnv * env, jclass cls, jobject bitmap)
|
(JNIEnv * env, jclass cls, jobject bitmap)
|
||||||
{
|
{
|
||||||
AndroidBitmapInfo info;
|
AndroidBitmapInfo info;
|
||||||
void* pixels;
|
void* pixels;
|
||||||
cv::Mat* m = NULL;
|
cv::Mat* m = new cv::Mat();
|
||||||
|
|
||||||
if ( AndroidBitmap_getInfo(env, bitmap, &info) < 0 )
|
if ( AndroidBitmap_getInfo(env, bitmap, &info) < 0 )
|
||||||
return 0; // can't get info
|
return (jlong)m; // can't get info
|
||||||
|
|
||||||
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
|
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
|
||||||
return 0; // incompatible format
|
return (jlong)m; // incompatible format
|
||||||
|
|
||||||
if ( AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0 )
|
if ( AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0 )
|
||||||
return 0; // can't get pixels
|
return (jlong)m; // can't get pixels
|
||||||
|
|
||||||
m = new cv::Mat(info.height, info.width, CV_8UC4);
|
m->create(info.height, info.width, CV_8UC4);
|
||||||
|
if(m->data && pixels)
|
||||||
memcpy(m->data, pixels, info.height * info.width * 4);
|
memcpy(m->data, pixels, info.height * info.width * 4);
|
||||||
|
|
||||||
AndroidBitmap_unlockPixels(env, bitmap);
|
AndroidBitmap_unlockPixels(env, bitmap);
|
||||||
@ -54,6 +39,11 @@ JNIEXPORT jlong JNICALL Java_org_opencv_utils_nBitmapToMat
|
|||||||
return (jlong)m;
|
return (jlong)m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_opencv_utils
|
||||||
|
* Method: nBitmapToMat(long m, Bitmap b)
|
||||||
|
* Signature: (JL)Z
|
||||||
|
*/
|
||||||
JNIEXPORT jboolean JNICALL Java_org_opencv_utils_nMatToBitmap
|
JNIEXPORT jboolean JNICALL Java_org_opencv_utils_nMatToBitmap
|
||||||
(JNIEnv * env, jclass cls, jlong m, jobject bitmap)
|
(JNIEnv * env, jclass cls, jlong m, jobject bitmap)
|
||||||
{
|
{
|
||||||
@ -61,7 +51,7 @@ JNIEXPORT jboolean JNICALL Java_org_opencv_utils_nMatToBitmap
|
|||||||
void* pixels;
|
void* pixels;
|
||||||
cv::Mat* mat = (cv::Mat*) m;
|
cv::Mat* mat = (cv::Mat*) m;
|
||||||
|
|
||||||
if ( m == 0 )
|
if ( mat == 0 || mat->data == 0)
|
||||||
return false; // no native Mat behind
|
return false; // no native Mat behind
|
||||||
|
|
||||||
if ( AndroidBitmap_getInfo(env, bitmap, &info) < 0 )
|
if ( AndroidBitmap_getInfo(env, bitmap, &info) < 0 )
|
||||||
@ -73,9 +63,14 @@ JNIEXPORT jboolean JNICALL Java_org_opencv_utils_nMatToBitmap
|
|||||||
if ( AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0 )
|
if ( AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0 )
|
||||||
return false; // can't get pixels
|
return false; // can't get pixels
|
||||||
|
|
||||||
|
if(mat->data && pixels)
|
||||||
memcpy(pixels, mat->data, info.height * info.width * 4);
|
memcpy(pixels, mat->data, info.height * info.width * 4);
|
||||||
|
|
||||||
AndroidBitmap_unlockPixels(env, bitmap);
|
AndroidBitmap_unlockPixels(env, bitmap);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user