Merge pull request #1242 from asmorkalov:android_native_cam

This commit is contained in:
Roman Donchenko 2013-08-12 14:47:48 +04:00 committed by OpenCV Buildbot
commit 57c1e8f333
29 changed files with 137 additions and 42 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
3rdparty/lib/mips/libnative_camera_r4.3.0.so vendored Executable file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
3rdparty/lib/x86/libnative_camera_r4.3.0.so vendored Executable file

Binary file not shown.

View File

@ -1,5 +1,8 @@
#if !defined(ANDROID_r2_2_0) && !defined(ANDROID_r2_3_3) && !defined(ANDROID_r3_0_1) && !defined(ANDROID_r4_0_0) && !defined(ANDROID_r4_0_3) && !defined(ANDROID_r4_1_1) && !defined(ANDROID_r4_2_0) #if !defined(ANDROID_r2_2_0) && !defined(ANDROID_r2_3_3) && !defined(ANDROID_r3_0_1) && \
# error Building camera wrapper for your version of Android is not supported by OpenCV. You need to modify OpenCV sources in order to compile camera wrapper for your version of Android. !defined(ANDROID_r4_0_0) && !defined(ANDROID_r4_0_3) && !defined(ANDROID_r4_1_1) && \
!defined(ANDROID_r4_2_0) && !defined(ANDROID_r4_3_0)
# error Building camera wrapper for your version of Android is not supported by OpenCV.\
You need to modify OpenCV sources in order to compile camera wrapper for your version of Android.
#endif #endif
#include <camera/Camera.h> #include <camera/Camera.h>
@ -16,17 +19,18 @@
//Include SurfaceTexture.h file with the SurfaceTexture class //Include SurfaceTexture.h file with the SurfaceTexture class
# include <gui/SurfaceTexture.h> # include <gui/SurfaceTexture.h>
# define MAGIC_OPENCV_TEXTURE_ID (0x10) # define MAGIC_OPENCV_TEXTURE_ID (0x10)
#else // defined(ANDROID_r3_0_1) || defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) #elif defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0)
//TODO: This is either 2.2 or 2.3. Include the headers for ISurface.h access # include <gui/ISurface.h>
#if defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) # include <gui/BufferQueue.h>
#include <gui/ISurface.h> #elif defined(ANDROID_r4_3_0)
#include <gui/BufferQueue.h> # include <gui/IGraphicBufferProducer.h>
# include <gui/BufferQueue.h>
#else #else
# include <surfaceflinger/ISurface.h> # include <surfaceflinger/ISurface.h>
#endif // defined(ANDROID_r4_1_1) #endif
#endif // defined(ANDROID_r3_0_1) || defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3)
#include <string> #include <string>
#include <fstream>
//undef logging macro from /system/core/libcutils/loghack.h //undef logging macro from /system/core/libcutils/loghack.h
#ifdef LOGD #ifdef LOGD
@ -45,7 +49,6 @@
# undef LOGE # undef LOGE
#endif #endif
// LOGGING // LOGGING
#include <android/log.h> #include <android/log.h>
#define CAMERA_LOG_TAG "OpenCV_NativeCamera" #define CAMERA_LOG_TAG "OpenCV_NativeCamera"
@ -60,7 +63,7 @@ using namespace android;
void debugShowFPS(); void debugShowFPS();
#if defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) #if defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
class ConsumerListenerStub: public BufferQueue::ConsumerListener class ConsumerListenerStub: public BufferQueue::ConsumerListener
{ {
public: public:
@ -73,6 +76,29 @@ public:
}; };
#endif #endif
std::string getProcessName()
{
std::string result;
std::ifstream f;
f.open("/proc/self/cmdline");
if (f.is_open())
{
std::string fullPath;
std::getline(f, fullPath, '\0');
if (!fullPath.empty())
{
int i = fullPath.size()-1;
while ((i >= 0) && (fullPath[i] != '/')) i--;
result = fullPath.substr(i+1, std::string::npos);
}
}
f.close();
return result;
}
void debugShowFPS() void debugShowFPS()
{ {
static int mFrameCount = 0; static int mFrameCount = 0;
@ -280,7 +306,7 @@ public:
} }
virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr virtual void postData(int32_t msgType, const sp<IMemory>& dataPtr
#if defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) || defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) #if defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) || defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
,camera_frame_metadata_t* ,camera_frame_metadata_t*
#endif #endif
) )
@ -361,7 +387,9 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
typedef sp<Camera> (*Android22ConnectFuncType)(); typedef sp<Camera> (*Android22ConnectFuncType)();
typedef sp<Camera> (*Android23ConnectFuncType)(int); typedef sp<Camera> (*Android23ConnectFuncType)(int);
typedef sp<Camera> (*Android3DConnectFuncType)(int, int); typedef sp<Camera> (*Android3DConnectFuncType)(int, int);
typedef sp<Camera> (*Android43ConnectFuncType)(int, const String16&, int);
const int ANY_CAMERA_INDEX = -1;
const int BACK_CAMERA_INDEX = 99; const int BACK_CAMERA_INDEX = 99;
const int FRONT_CAMERA_INDEX = 98; const int FRONT_CAMERA_INDEX = 98;
@ -372,14 +400,24 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
CAMERA_SUPPORT_MODE_ZSL = 0x08 /* Camera Sensor supports ZSL mode. */ CAMERA_SUPPORT_MODE_ZSL = 0x08 /* Camera Sensor supports ZSL mode. */
}; };
// used for Android 4.3
enum {
USE_CALLING_UID = -1
};
const char Android22ConnectName[] = "_ZN7android6Camera7connectEv"; const char Android22ConnectName[] = "_ZN7android6Camera7connectEv";
const char Android23ConnectName[] = "_ZN7android6Camera7connectEi"; const char Android23ConnectName[] = "_ZN7android6Camera7connectEi";
const char Android3DConnectName[] = "_ZN7android6Camera7connectEii"; const char Android3DConnectName[] = "_ZN7android6Camera7connectEii";
const char Android43ConnectName[] = "_ZN7android6Camera7connectEiRKNS_8String16Ei";
int localCameraIndex = cameraId; int localCameraIndex = cameraId;
if (cameraId == ANY_CAMERA_INDEX)
{
localCameraIndex = 0;
}
#if !defined(ANDROID_r2_2_0) #if !defined(ANDROID_r2_2_0)
if (cameraId == BACK_CAMERA_INDEX) else if (cameraId == BACK_CAMERA_INDEX)
{ {
LOGD("Back camera selected"); LOGD("Back camera selected");
for (int i = 0; i < Camera::getNumberOfCameras(); i++) for (int i = 0; i < Camera::getNumberOfCameras(); i++)
@ -450,6 +488,12 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
LOGD("Connecting to CameraService v 3D"); LOGD("Connecting to CameraService v 3D");
camera = Android3DConnect(localCameraIndex, CAMERA_SUPPORT_MODE_2D); camera = Android3DConnect(localCameraIndex, CAMERA_SUPPORT_MODE_2D);
} }
else if (Android43ConnectFuncType Android43Connect = (Android43ConnectFuncType)dlsym(CameraHALHandle, Android43ConnectName))
{
std::string currentProcName = getProcessName();
LOGD("Current process name for camera init: %s", currentProcName.c_str());
camera = Android43Connect(localCameraIndex, String16(currentProcName.c_str()), USE_CALLING_UID);
}
else else
{ {
dlclose(CameraHALHandle); dlclose(CameraHALHandle);
@ -471,7 +515,7 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
handler->camera = camera; handler->camera = camera;
handler->cameraId = localCameraIndex; handler->cameraId = localCameraIndex;
if (prevCameraParameters != 0) if (prevCameraParameters != NULL)
{ {
LOGI("initCameraConnect: Setting paramers from previous camera handler"); LOGI("initCameraConnect: Setting paramers from previous camera handler");
camera->setParameters(prevCameraParameters->flatten()); camera->setParameters(prevCameraParameters->flatten());
@ -560,26 +604,25 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
} }
} }
status_t pdstatus; status_t bufferStatus;
#if defined(ANDROID_r2_2_0) #if defined(ANDROID_r2_2_0)
pdstatus = camera->setPreviewDisplay(sp<ISurface>(0 /*new DummySurface*/)); bufferStatus = camera->setPreviewDisplay(sp<ISurface>(0 /*new DummySurface*/));
if (pdstatus != 0) if (bufferStatus != 0)
LOGE("initCameraConnect: failed setPreviewDisplay(0) call; camera migth not work correctly on some devices"); LOGE("initCameraConnect: failed setPreviewDisplay(0) call (status %d); camera might not work correctly on some devices", bufferStatus);
#elif defined(ANDROID_r2_3_3) #elif defined(ANDROID_r2_3_3)
/* Do nothing in case of 2.3 for now */ /* Do nothing in case of 2.3 for now */
#elif defined(ANDROID_r3_0_1) || defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) #elif defined(ANDROID_r3_0_1) || defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3)
sp<SurfaceTexture> surfaceTexture = new SurfaceTexture(MAGIC_OPENCV_TEXTURE_ID); sp<SurfaceTexture> surfaceTexture = new SurfaceTexture(MAGIC_OPENCV_TEXTURE_ID);
pdstatus = camera->setPreviewTexture(surfaceTexture); bufferStatus = camera->setPreviewTexture(surfaceTexture);
if (pdstatus != 0) if (bufferStatus != 0)
LOGE("initCameraConnect: failed setPreviewTexture call; camera migth not work correctly"); LOGE("initCameraConnect: failed setPreviewTexture call (status %d); camera might not work correctly", bufferStatus);
#elif defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) #elif defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
sp<BufferQueue> bufferQueue = new BufferQueue(); sp<BufferQueue> bufferQueue = new BufferQueue();
sp<BufferQueue::ConsumerListener> queueListener = new ConsumerListenerStub(); sp<BufferQueue::ConsumerListener> queueListener = new ConsumerListenerStub();
bufferQueue->consumerConnect(queueListener); bufferQueue->consumerConnect(queueListener);
pdstatus = camera->setPreviewTexture(bufferQueue); bufferStatus = camera->setPreviewTexture(bufferQueue);
if (pdstatus != 0) if (bufferStatus != 0)
LOGE("initCameraConnect: failed setPreviewTexture call; camera migth not work correctly"); LOGE("initCameraConnect: failed setPreviewTexture call; camera might not work correctly");
#endif #endif
#if (defined(ANDROID_r2_2_0) || defined(ANDROID_r2_3_3) || defined(ANDROID_r3_0_1)) #if (defined(ANDROID_r2_2_0) || defined(ANDROID_r2_3_3) || defined(ANDROID_r3_0_1))
@ -595,9 +638,9 @@ CameraHandler* CameraHandler::initCameraConnect(const CameraCallback& callback,
#endif //!(defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3)) #endif //!(defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3))
LOGD("Starting preview"); LOGD("Starting preview");
status_t resStart = camera->startPreview(); status_t previewStatus = camera->startPreview();
if (resStart != 0) if (previewStatus != 0)
{ {
LOGE("initCameraConnect: startPreview() fails. Closing camera connection..."); LOGE("initCameraConnect: startPreview() fails. Closing camera connection...");
handler->closeCameraConnect(); handler->closeCameraConnect();
@ -620,9 +663,11 @@ void CameraHandler::closeCameraConnect()
} }
camera->stopPreview(); camera->stopPreview();
#if defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) || defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
#endif
camera->disconnect(); camera->disconnect();
camera.clear(); camera.clear();
camera=NULL; camera=NULL;
// ATTENTION!!!!!!!!!!!!!!!!!!!!!!!!!! // ATTENTION!!!!!!!!!!!!!!!!!!!!!!!!!!
// When we set // When we set
@ -863,14 +908,60 @@ void CameraHandler::applyProperties(CameraHandler** ppcameraHandler)
if (*ppcameraHandler == 0) if (*ppcameraHandler == 0)
{ {
LOGE("applyProperties: Passed null *ppcameraHandler"); LOGE("applyProperties: Passed NULL *ppcameraHandler");
return; return;
} }
LOGD("CameraHandler::applyProperties()"); CameraParameters curCameraParameters((*ppcameraHandler)->params.flatten());
CameraHandler* previousCameraHandler=*ppcameraHandler;
CameraParameters curCameraParameters(previousCameraHandler->params.flatten());
#if defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3) || defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
CameraHandler* handler=*ppcameraHandler;
handler->camera->stopPreview();
handler->camera->setPreviewCallbackFlags(CAMERA_FRAME_CALLBACK_FLAG_NOOP);
status_t reconnectStatus = handler->camera->reconnect();
if (reconnectStatus != 0)
{
LOGE("applyProperties: failed to reconnect camera (status %d)", reconnectStatus);
return;
}
handler->camera->setParameters(curCameraParameters.flatten());
handler->params.unflatten(curCameraParameters.flatten());
status_t bufferStatus;
# if defined(ANDROID_r4_0_0) || defined(ANDROID_r4_0_3)
sp<SurfaceTexture> surfaceTexture = new SurfaceTexture(MAGIC_OPENCV_TEXTURE_ID);
bufferStatus = handler->camera->setPreviewTexture(surfaceTexture);
if (bufferStatus != 0)
LOGE("applyProperties: failed setPreviewTexture call (status %d); camera might not work correctly", bufferStatus);
# elif defined(ANDROID_r4_1_1) || defined(ANDROID_r4_2_0) || defined(ANDROID_r4_3_0)
sp<BufferQueue> bufferQueue = new BufferQueue();
sp<BufferQueue::ConsumerListener> queueListener = new ConsumerListenerStub();
bufferQueue->consumerConnect(queueListener);
bufferStatus = handler->camera->setPreviewTexture(bufferQueue);
if (bufferStatus != 0)
LOGE("applyProperties: failed setPreviewTexture call; camera might not work correctly");
# endif
handler->camera->setPreviewCallbackFlags( CAMERA_FRAME_CALLBACK_FLAG_ENABLE_MASK | CAMERA_FRAME_CALLBACK_FLAG_COPY_OUT_MASK);//with copy
LOGD("Starting preview");
status_t previewStatus = handler->camera->startPreview();
if (previewStatus != 0)
{
LOGE("initCameraConnect: startPreview() fails. Closing camera connection...");
handler->closeCameraConnect();
handler = NULL;
}
else
{
LOGD("Preview started successfully");
}
#else
CameraHandler* previousCameraHandler=*ppcameraHandler;
CameraCallback cameraCallback=previousCameraHandler->cameraCallback; CameraCallback cameraCallback=previousCameraHandler->cameraCallback;
void* userData=previousCameraHandler->userData; void* userData=previousCameraHandler->userData;
int cameraId=previousCameraHandler->cameraId; int cameraId=previousCameraHandler->cameraId;
@ -879,7 +970,6 @@ void CameraHandler::applyProperties(CameraHandler** ppcameraHandler)
previousCameraHandler->closeCameraConnect(); previousCameraHandler->closeCameraConnect();
LOGD("CameraHandler::applyProperties(): after previousCameraHandler->closeCameraConnect"); LOGD("CameraHandler::applyProperties(): after previousCameraHandler->closeCameraConnect");
LOGD("CameraHandler::applyProperties(): before initCameraConnect"); LOGD("CameraHandler::applyProperties(): before initCameraConnect");
CameraHandler* handler=initCameraConnect(cameraCallback, cameraId, userData, &curCameraParameters); CameraHandler* handler=initCameraConnect(cameraCallback, cameraId, userData, &curCameraParameters);
LOGD("CameraHandler::applyProperties(): after initCameraConnect, handler=0x%x", (int)handler); LOGD("CameraHandler::applyProperties(): after initCameraConnect, handler=0x%x", (int)handler);
@ -892,6 +982,7 @@ void CameraHandler::applyProperties(CameraHandler** ppcameraHandler)
} }
} }
(*ppcameraHandler)=handler; (*ppcameraHandler)=handler;
#endif
} }

View File

@ -21,3 +21,7 @@ native_camera_r4.2.0; armeabi-v7a; 14; /home/alexander/Projects/AndroidSource/4.
native_camera_r4.2.0; armeabi; 14; /home/alexander/Projects/AndroidSource/4.2 native_camera_r4.2.0; armeabi; 14; /home/alexander/Projects/AndroidSource/4.2
native_camera_r4.2.0; x86; 14; /home/alexander/Projects/AndroidSource/4.2 native_camera_r4.2.0; x86; 14; /home/alexander/Projects/AndroidSource/4.2
native_camera_r4.2.0; mips; 14; /home/alexander/Projects/AndroidSource/4.2 native_camera_r4.2.0; mips; 14; /home/alexander/Projects/AndroidSource/4.2
native_camera_r4.3.0; armeabi; 14; /home/alexander/Projects/AndroidSource/4.3
native_camera_r4.3.0; armeabi-v7a; 14; /home/alexander/Projects/AndroidSource/4.3
native_camera_r4.3.0; x86; 14; /home/alexander/Projects/AndroidSource/4.3
native_camera_r4.3.0; mips; 14; /home/alexander/Projects/AndroidSource/4.3