博主QQ:1356438802
QQ群:473383394——UVC&OpenCV47
#!/bin/sh
cd `dirname $0`/..
mkdir -p build_android_arm
cd build_android_arm
cmake -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DCMAKE_TOOLCHAIN_FILE=../android/android.toolchain.cmake $@ ../..
-- Tests and samples:
-- Tests: YES
-- Performance tests: YES
-- C/C++ Examples: NO
if(DEFINED WITH_V4L)
if(HAVE_CAMV4L)
set(HAVE_CAMV4L_STR "YES")
else()
set(HAVE_CAMV4L_STR "NO")
endif()
if(HAVE_CAMV4L2)
set(HAVE_CAMV4L2_STR "YES")
elseif(HAVE_VIDEOIO)
set(HAVE_CAMV4L2_STR "YES(videoio)")
else()
set(HAVE_CAMV4L2_STR "NO")
endif()
status(" V4L/V4L2:" HAVE_LIBV4L
THEN "Using libv4l1 (ver ${ALIASOF_libv4l1_VERSION}) / libv4l2 (ver ${ALIASOF_libv4l2_VERSION})"
ELSE "${HAVE_CAMV4L_STR}/${HAVE_CAMV4L2_STR}")
endif(DEFINED WITH_V4L)
OCV_OPTION(WITH_V4L "Include Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_LIBV4L "Use libv4l for Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_V4L "Include Video 4 Linux support" ON IF (UNIX AND NOT ANDROID) )
OCV_OPTION(WITH_V4L "Include Video 4 Linux support" ON IF (UNIX))
# --- V4L ---
#luoyouren-20160629
status(" before seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
ocv_clear_vars(HAVE_LIBV4L HAVE_CAMV4L HAVE_CAMV4L2 HAVE_VIDEOIO)
if(WITH_V4L)
if(WITH_LIBV4L)
CHECK_MODULE(libv4l1 HAVE_LIBV4L1)
CHECK_MODULE(libv4l2 HAVE_LIBV4L2)
if(HAVE_LIBV4L1 AND HAVE_LIBV4L2)
set(HAVE_LIBV4L YES)
else()
set(HAVE_LIBV4L NO)
endif()
endif()
CHECK_INCLUDE_FILE(linux/videodev.h HAVE_CAMV4L)
CHECK_INCLUDE_FILE(linux/videodev2.h HAVE_CAMV4L2)
CHECK_INCLUDE_FILE(sys/videoio.h HAVE_VIDEOIO)
status(" seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
endif(WITH_V4L)
#luoyouren-20160629
#set(WITH_V4L ON)
#set(HAVE_CAMV4L2 ON)
status(" after seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
CHECK_INCLUDE_FILE(linux/videodev.h HAVE_CAMV4L)
CHECK_INCLUDE_FILE(linux/videodev2.h HAVE_CAMV4L2)
CHECK_INCLUDE_FILE(sys/videoio.h HAVE_VIDEOIO)
/opt/ndk/android-ndk-r9/platforms/android-8/arch-arm/usr/include/linux/videodev2.h
# --- V4L ---
#luoyouren-20160629
status(" before seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
ocv_clear_vars(HAVE_LIBV4L HAVE_CAMV4L HAVE_CAMV4L2 HAVE_VIDEOIO)
if(WITH_V4L)
if(WITH_LIBV4L)
CHECK_MODULE(libv4l1 HAVE_LIBV4L1)
CHECK_MODULE(libv4l2 HAVE_LIBV4L2)
if(HAVE_LIBV4L1 AND HAVE_LIBV4L2)
set(HAVE_LIBV4L YES)
else()
set(HAVE_LIBV4L NO)
endif()
endif()
CHECK_INCLUDE_FILE(linux/videodev.h HAVE_CAMV4L)
CHECK_INCLUDE_FILE(linux/videodev2.h HAVE_CAMV4L2)
CHECK_INCLUDE_FILE(sys/videoio.h HAVE_VIDEOIO)
status(" seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
endif(WITH_V4L)
#luoyouren-20160629
set(HAVE_CAMV4L2 ON)
status(" after seting ...")
status(" WITH_V4L:" ${WITH_V4L})
status(" HAVE_LIBV4L:" ${HAVE_LIBV4L})
status(" HAVE_CAMV4L:" ${HAVE_CAMV4L})
status(" HAVE_CAMV4L2:" ${HAVE_CAMV4L2})
status(" HAVE_VIDEOIO:" ${HAVE_VIDEOIO})
cd build_android_arm
make
make install
#include
#undef LOG_TAG
#undef LOGD
#undef LOGE
#undef LOGI
#define LOG_TAG "OpenCV::android_v4l2"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__))
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#博客案例:OpenCV4Android释疑:,透析Android以JNI调OpenCV的三种方式(让OpenCVManager永不困扰)
LIB_TYPE = STATIC
ifeq ($(LIB_TYPE), STATIC)
#STATIC链接时,可以不额外携带其依赖库
#当JNI里面调用了cvCaptureFromCAM去打开AndroidNativeCamera,就需要OPENCV_CAMERA_MODULES:=on
#来增加libnative_camera_r4.4.0.so
OPENCV_LIB_TYPE:=STATIC
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
else
#SHARED链接时,必须携带其依赖库,否则运行报错
OPENCV_LIB_TYPE:=SHARED
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=on
endif
#原始openCV4Android SDK
#include ../../openCV_2410_sdk/native/jni/OpenCV.mk
#添加V4L2,重新编译后的openCV4Android SDK
include ../../openCV_2410_sdk_v4l2/native/jni/OpenCV.mk
$(warning "****************************************")
$(warning $(LOCAL_C_INCLUDES))
LOCAL_LDLIBS += -llog
LOCAL_SHARED_LIBRARIES += \
libandroid_runtime\
liblog \
libcutils \
libnativehelper \
libcore/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_SRC_FILES := ImageProc.cpp
LOCAL_MODULE := image_proc
include $(BUILD_SHARED_LIBRARY)
01-01 02:15:19.843: I/luo.uvc.jni(6649): open uvc3 ......
01-01 02:15:19.844: I/OPENCV_LUO(6649): usb camera Java_luo_uvc_jni_ImageProc_connectCamera start ....
01-01 02:15:19.844: I/OPENCV_LUO(6649): usb camera (-1) Java_luo_uvc_jni_ImageProc_connectCamera
01-01 02:15:19.844: I/OpenCV::android_v4l2(6649): CvCaptureCAM_V4L_CPP::CvCaptureCAM_V4L_CPP(-1)
01-01 02:15:19.844: I/OpenCV::android_v4l2(6649): icvCaptureFromCAM_V4L::use /dev/video0
01-01 02:15:19.844: E/OPENCV_LUO(6649): usb camera 0 open success!
01-01 02:15:19.844: I/luo.uvc.jni(6649): open uvc success!!!
02:20:51.968: D/dalvikvm(6834): Trying to load lib /data/app-lib/CameraTouch/libNDKBase.so 0x41ce1eb0
01-01 02:20:51.986: E/ION_DEBUG(6834): [add_system_map_entry]so libNDKBase.so address 0x612b7000 size 3563520 has been load into memory ion_debug_lock 0x404ad01c
01-01 02:20:52.033: D/dalvikvm(6834): Added shared lib /data/app-lib/CameraTouch/libNDKBase.so 0x41ce1eb0
01-01 02:20:52.034: D/dalvikvm(6834): No JNI_OnLoad found in /data/app-lib/CameraTouch/libNDKBase.so 0x41ce1eb0, skipping init
01-01 02:20:52.035: D/NDKBASE(6834): CMPBase::CMPBase()
01-01 02:20:52.036: D/NDKBASE(6834): CHelper::Open END
01-01 02:20:52.036: D/NDKBASE(6834): Java_com_indesign_cameratouch_NDKBase_OpenTouchNDK
01-01 02:20:52.037: D/NDKBASE(6834): CImageBase::captureThread start
01-01 02:20:52.037: D/NDKBASE(6834): CImageBase::captureThread start111
01-01 02:20:52.038: D/NDKBASE(6834): CImageBase::captureThread start00
01-01 02:20:52.038: D/NDKBASE(6834): CImageBase::captureThread start11
01-01 02:20:52.040: D/NDKBASE(6834): CCamInfo::checkDevice start
01-01 02:20:52.040: D/NDKBASE(6834): CCamInfo::checkDevice 1111
01-01 02:20:52.041: D/NDKBASE(6834): usb_device_added, Name: /dev/bus/usb/001/004
01-01 02:20:52.042: D/NDKBASE(6834): usb_device_added, Name: /dev/bus/usb/001/001
01-01 02:20:52.042: D/NDKBASE(6834): CCamInfo::checkDevice END0
01-01 02:20:52.042: D/NDKBASE(6834): CImageBase::captureThread start22
07-01 02:41:07.644: D/NDKBASE(6805): CMPBase::CMPBase()
07-01 02:41:07.645: D/NDKBASE(6805): CHelper::Open END
07-01 02:41:07.645: D/NDKBASE(6805): Java_com_indesign_cameratouch_NDKBase_OpenTouchNDK
07-01 02:41:07.645: D/NDKBASE(6805): CImageBase::captureThread start
07-01 02:41:07.645: D/NDKBASE(6805): CImageBase::captureThread start111
07-01 02:41:07.645: D/NDKBASE(6805): CImageBase::captureThread start00
07-01 02:41:07.645: D/NDKBASE(6805): CImageBase::captureThread start11
07-01 02:41:07.645: D/NDKBASE(6805): CCamInfo::checkDevice start
07-01 02:41:07.645: D/NDKBASE(6805): CCamInfo::checkDevice 1111
07-01 02:41:07.646: D/NDKBASE(6805): usb_device_added, Name: /dev/bus/usb/001/002
07-01 02:41:07.646: D/NDKBASE(6805): usb_device_added, Name: /dev/bus/usb/001/001
07-01 02:41:07.646: D/NDKBASE(6805): CCamInfo::checkDevice END1
07-01 02:41:07.646: D/NDKBASE(6805): CImageBase::captureThread start333
07-01 02:41:07.646: D/NDKBASE(6805): CameraId: 0, checking.
07-01 02:41:07.646: D/NDKBASE(6805): CameraId:cvCaptureFromCAM star
07-01 02:41:07.647: D/OpenCV::camera(6805): CvCapture_Android::CvCapture_Android(0)
07-01 02:41:07.647: D/OpenCV::camera(6805): Library name: libNDKBase.so
07-01 02:41:07.647: D/OpenCV::camera(6805): Library base address: 0x61191000
07-01 02:41:07.691: D/OpenCV::camera(6805): Libraries folder found: /data/app-lib/CameraTouch/
07-01 02:41:07.691: D/OpenCV::camera(6805): CameraWrapperConnector::connectToLib: folderPath=/data/app-lib/CameraTouch/
07-01 02:41:07.706: V/ActivityManager(684): Broadcast: Intent { act=android.intent.action.DROPBOX_ENTRY_ADDED flg=0x10 (has extras) } ordered=false userid=0 callerApp=ProcessRecord{41d6b1a0 684:system/1000}
07-01 02:41:07.706: D/dalvikvm(684): threadid=83: exiting
07-01 02:41:07.714: D/dalvikvm(684): threadid=83: bye!
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.1.1.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.0.3.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r2.3.3.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.2.0.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r2.2.0.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r3.0.1.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.3.0.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.0.0.so
07-01 02:41:07.841: E/OpenCV::camera(6805): ||libnative_camera_r4.4.0.so
07-01 02:41:07.841: D/OpenCV::camera(6805): try to load library 'libnative_camera_r4.4.0.so'
07-01 02:41:07.852: E/ION_DEBUG(6805): [add_system_map_entry]so libnative_camera_r4.4.0.so address 0x614f9000 size 282624 has been load into memory ion_debug_lock 0x4048601c
07-01 02:41:07.853: D/OpenCV::camera(6805): Loaded library '/data/app-lib/CameraTouch/libnative_camera_r4.4.0.so'
07-01 02:41:07.854: D/OpenCV_NativeCamera(6805): CameraHandler::initCameraConnect(0x6122beed, 0, 0x6055fde8, 0x0)
07-01 02:41:07.855: D/OpenCV_NativeCamera(6805): Current process name for camera init: com.indesign.cameratouch
07-01 02:41:08.036: D/CameraService(132): CameraService::connect E (pid 6805 "com.indesign.cameratouch", id 0)
07-01 02:41:08.036: E/CameraService(132): CameraService::connect X (pid 6805) rejected (invalid cameraId 0).
07-01 02:41:08.037: W/CameraBase(6805): An error occurred while connecting to camera: 0
07-01 02:41:08.038: E/OpenCV_NativeCamera(6805): initCameraConnect: Unable to connect to CameraService
07-01 02:41:08.038: E/OpenCV::camera(6805): CameraWrapperConnector::connectWrapper ERROR: the initializing function returned false
07-01 02:41:08.038: E/OpenCV::camera(6805): Native_camera returned opening error: 6
07-01 02:41:08.038: D/NDKBASE(6805): CameraId: 0, fail.
07-01 02:41:08.038: D/NDKBASE(6805): CImageBase::captureThread start333
07-01 02:41:08.038: D/NDKBASE(6805): CameraId: 1, checking.
07-01 02:41:08.038: D/NDKBASE(6805): CameraId:cvCaptureFromCAM star
07-01 02:41:08.038: D/OpenCV::camera(6805): CvCapture_Android::CvCapture_Android(1)
07-01 02:41:08.038: D/OpenCV_NativeCamera(6805): CameraHandler::initCameraConnect(0x6122beed, 1, 0x6055fde8, 0x0)
07-01 02:41:08.038: D/OpenCV_NativeCamera(6805): Current process name for camera init: com.indesign.cameratouch
07-01 02:41:08.040: D/CameraService(132): CameraService::connect E (pid 6805 "com.indesign.cameratouch", id 1)
07-01 02:41:08.040: E/CameraService(132): CameraService::connect X (pid 6805) rejected (invalid cameraId 1).
07-01 02:41:08.041: W/CameraBase(6805): An error occurred while connecting to camera: 1
07-01 02:41:08.041: E/OpenCV_NativeCamera(6805): initCameraConnect: Unable to connect to CameraService
07-01 02:41:08.041: E/OpenCV::camera(6805): CameraWrapperConnector::connectWrapper ERROR: the initializing function returned false
07-01 02:41:08.041: E/OpenCV::camera(6805): Native_camera returned opening error: 6
07-01 02:41:08.041: D/NDKBASE(6805): CameraId: 1, fail.
frame = cvQueryFrame(capture); //取图片帧
case PALETTE_YUYV:
LOGI("icvRetrieveFrameCAM_V4L: capture->palette = PALETTE_YUYV");
yuyv_to_rgb24(capture->form.fmt.pix.width,
capture->form.fmt.pix.height,
(unsigned char*)(capture->buffers[capture->bufferIndex].start),
(unsigned char*)capture->frame.imageData);
break;
#include "ImageProc.h"
#include "cv.h"
#include "highgui.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
using namespace cv;
using namespace std;
#ifdef __cplusplus
extern "C" {
#pragma message("------------------------ ImageProc.cpp")
#endif
CvCapture *pCapture = NULL;
IplImage *frame = NULL;
int id;
JNIEXPORT jintArray JNICALL Java_luo_uvc_jni_ImageProc_grayProc(JNIEnv* env,
jclass obj, jintArray buf, jint w, jint h)
{
jint *cbuf;
cbuf = env->GetIntArrayElements(buf, false);
if (cbuf == NULL)
{
return 0;
}
Mat imgData(h, w, CV_8UC4, (unsigned char*) cbuf);
uchar* ptr = imgData.ptr(0);
for (int i = 0; i < w * h; i++)
{
//计算公式:Y(亮度) = 0.299*R + 0.587*G + 0.114*B
//对于一个int四字节,其彩色值存储方式为:BGRA
int grayScale = (int) (ptr[4 * i + 2] * 0.299 + ptr[4 * i + 1] * 0.587
+ ptr[4 * i + 0] * 0.114);
ptr[4 * i + 1] = grayScale;
ptr[4 * i + 2] = grayScale;
ptr[4 * i + 0] = grayScale;
}
int size = w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, cbuf);
env->ReleaseIntArrayElements(buf, cbuf, 0);
return result;
}
JNIEXPORT jint JNICALL Java_luo_uvc_jni_ImageProc_connectCamera(JNIEnv* env,
jclass obj, jint device)
{
id = device;
pCapture = NULL;
LOGI("usb camera id=%d Java_luo_uvc_jni_ImageProc_connectCamera start ....\n", id);
//查找UVC设备
pCapture = cvCaptureFromCAM(id);
if (NULL != pCapture)
{
LOGE("usb camera open success!\n");
return 0;
}
LOGI("usb camera Java_luo_uvc_jni_ImageProc_connectCamera end ....\n");
return -1;
}
JNIEXPORT jint JNICALL Java_luo_uvc_jni_ImageProc_releaseCamera(JNIEnv* env,
jclass obj)
{
LOGI("usb camera 0 Java_luo_uvc_jni_ImageProc_releaseCamera start ....\n");
if (NULL != pCapture)
{
cvReleaseCapture(&pCapture);
}
LOGI("usb camera 0 Java_luo_uvc_jni_ImageProc_releaseCamera end ....\n");
return 0;
}
struct FrameInfoClass
{
jfieldID width;
jfieldID heigth;
jfieldID imageSize;
jfieldID pixels;
};
JNIEXPORT jobject JNICALL Java_luo_uvc_jni_ImageProc_getFrame(JNIEnv* _env,
jclass obj)
{
LOGI("------------Java_luo_uvc_jni_ImageProc_getFrame\n");
if(NULL != pCapture)
{
LOGI("------------start cvQueryFrame\n");
frame = NULL;
frame = cvQueryFrame(pCapture); //取图片帧
if(NULL == frame)
{
LOGE("cvQueryFrame(capture) failed!!!!!!!!!!!!!!!!!!!!!!!");
return NULL;
}
struct FrameInfoClass frameInfoClass;
//内部类用$
//luo/uvc/jni/ImageProc$FrameInfo
jclass class2 = _env->FindClass("luo/uvc/jni/ImageProc$FrameInfo");
LOGI("1=GetFieldID\n");
frameInfoClass.width = _env->GetFieldID(class2, "mWidth", "I");
LOGI("2=GetFieldID\n");
frameInfoClass.heigth = _env->GetFieldID(class2, "mHeight", "I");
LOGI("3=GetFieldID\n");
frameInfoClass.imageSize = _env->GetFieldID(class2, "mImageSize", "I");
LOGI("4=GetFieldID\n");
frameInfoClass.pixels = _env->GetFieldID(class2, "mPixels", "[I");
LOGI("5=GetFieldID\n");
//
jobject joFrame = _env->AllocObject(class2);
LOGI("6=GetFieldID\n");
LOGI("frame->width = %d\n", frame->width);
LOGI("frame->height = %d\n", frame->height);
LOGI("frame->imageSize = %d\n", frame->imageSize);
_env->SetIntField(joFrame, frameInfoClass.width, frame->width);
_env->SetIntField(joFrame, frameInfoClass.heigth, frame->height);
_env->SetIntField(joFrame, frameInfoClass.imageSize, frame->imageSize);
int size = frame->width * frame->height;
//创建一个新的java数组(jarray),但是jarray不是C数组类型,不能直接访问jarray
jintArray jiArr = _env->NewIntArray(size);
jint *ji;
#if 1 //可用
LOGI("7=GetFieldID\n");
//RGB --> ARGB8888
Mat frameRGB(frame->height, frame->width, CV_8UC3, frame->imageData), frameARGB;
cvtColor(frameRGB, frameARGB, CV_RGB2RGBA);
//JNI支持一系列的Get/ReleaseArrayElement 函数,允许本地代码获取一个指向基本C类型数组的元素的指针。
ji = _env->GetIntArrayElements(jiArr, 0);
memcpy((jbyte *)ji, frameARGB.data, 4 * size);
_env->ReleaseIntArrayElements(jiArr, ji, 0); //可加,可不加
_env->SetObjectField(joFrame, frameInfoClass.pixels, jiArr);
LOGI("8=GetFieldID\n");
#else //可用
//可以使用GetIntArrayRegion函数来把一个 int数组中的所有元素复制到一个C缓冲区中
//SetIntArrayRegion则是逆过程
LOGI("13=GetFieldID\n");
_env->SetIntArrayRegion(jiArr, 0, 2, abc);
_env->SetObjectField(joFrame, company_class.money, jiArr);
#endif
LOGI("Java_luo_uvc_jni_ImageProc_getFrame end\n");
return joFrame;
}
LOGI("=================Java_luo_uvc_jni_ImageProc_getFrame failed\n");
return 0;
}
JNIEXPORT jint JNICALL Java_luo_uvc_jni_ImageProc_getWidth(JNIEnv* env,
jclass obj)
{
return 0;
}
JNIEXPORT jint JNICALL Java_luo_uvc_jni_ImageProc_getHeight(JNIEnv* env,
jclass obj)
{
return 0;
}
#ifdef __cplusplus
}
#endif
/* end of extern */