#ifndef __PHONE_DEVICE_H__ #define __PHONE_DEVICE_H__ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "camera2/Camera2Helper.h" #include "camera2/ndkcamera.h" #include #include #include #define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "error", __VA_ARGS__)) #define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, "debug", __VA_ARGS__)) #if 0 void BitmapToMat2(JNIEnv *env, jobject& bitmap, cv::Mat& mat, jboolean needUnPremultiplyAlpha) { AndroidBitmapInfo info; void *pixels = 0; cv::Mat &dst = mat; // try { ALOGD("nBitmapToMat"); CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0); CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 || info.format == ANDROID_BITMAP_FORMAT_RGB_565); CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0); CV_Assert(pixels); dst.create(info.height, info.width, CV_8UC4); if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) { ALOGD("nBitmapToMat: RGBA_8888 -> CV_8UC4"); cv::Mat tmp(info.height, info.width, CV_8UC4, pixels); if (needUnPremultiplyAlpha) cvtColor(tmp, dst, cv::COLOR_mRGBA2RGBA); else tmp.copyTo(dst); } else { // info.format == ANDROID_BITMAP_FORMAT_RGB_565 ALOGD("nBitmapToMat: RGB_565 -> CV_8UC4"); cv::Mat tmp(info.height, info.width, CV_8UC2, pixels); cvtColor(tmp, dst, cv::COLOR_BGR5652RGBA); } AndroidBitmap_unlockPixels(env, bitmap); return; /* } catch (const cv::Exception &e) { AndroidBitmap_unlockPixels(env, bitmap); LOGE("nBitmapToMat catched cv::Exception: %s", e.what()); jclass je = env->FindClass("org/opencv/core/CvException"); if (!je) je = env->FindClass("java/lang/Exception"); env->ThrowNew(je, e.what()); return; } catch (...) { AndroidBitmap_unlockPixels(env, bitmap); LOGE("nBitmapToMat catched unknown exception (...)"); jclass je = env->FindClass("java/lang/Exception"); env->ThrowNew(je, "Unknown exception in JNI code {nBitmapToMat}"); return; } */ } void BitmapToMat(JNIEnv *env, jobject& bitmap, cv::Mat& mat) { BitmapToMat2(env, bitmap, mat, false); } void MatToBitmap2 (JNIEnv *env, cv::Mat& mat, jobject& bitmap, jboolean needPremultiplyAlpha) { AndroidBitmapInfo info; void *pixels = 0; cv::Mat &src = mat; // try { ALOGD("nMatToBitmap"); CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0); CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 || info.format == ANDROID_BITMAP_FORMAT_RGB_565); CV_Assert(src.dims == 2 && info.height == (uint32_t) src.rows && info.width == (uint32_t) src.cols); CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4); CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0); CV_Assert(pixels); if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) { cv::Mat tmp(info.height, info.width, CV_8UC4, pixels); if (src.type() == CV_8UC1) { ALOGD("nMatToBitmap: CV_8UC1 -> RGBA_8888"); cvtColor(src, tmp, cv::COLOR_GRAY2RGBA); } else if (src.type() == CV_8UC3) { ALOGD("nMatToBitmap: CV_8UC3 -> RGBA_8888"); cvtColor(src, tmp, cv::COLOR_RGB2RGBA); } else if (src.type() == CV_8UC4) { ALOGD("nMatToBitmap: CV_8UC4 -> RGBA_8888"); if (needPremultiplyAlpha) cvtColor(src, tmp, cv::COLOR_RGBA2mRGBA); else src.copyTo(tmp); } } else { // info.format == ANDROID_BITMAP_FORMAT_RGB_565 cv::Mat tmp(info.height, info.width, CV_8UC2, pixels); if (src.type() == CV_8UC1) { ALOGD("nMatToBitmap: CV_8UC1 -> RGB_565"); cvtColor(src, tmp, cv::COLOR_GRAY2BGR565); } else if (src.type() == CV_8UC3) { ALOGD("nMatToBitmap: CV_8UC3 -> RGB_565"); cvtColor(src, tmp, cv::COLOR_RGB2BGR565); } else if (src.type() == CV_8UC4) { ALOGD("nMatToBitmap: CV_8UC4 -> RGB_565"); cvtColor(src, tmp, cv::COLOR_RGBA2BGR565); } } AndroidBitmap_unlockPixels(env, bitmap); return; /* } catch (const cv::Exception &e) { AndroidBitmap_unlockPixels(env, bitmap); LOGE("nMatToBitmap catched cv::Exception: %s", e.what()); jclass je = env->FindClass("org/opencv/core/CvException"); if (!je) je = env->FindClass("java/lang/Exception"); env->ThrowNew(je, e.what()); return; } catch (...) { AndroidBitmap_unlockPixels(env, bitmap); LOGE("nMatToBitmap catched unknown exception (...)"); jclass je = env->FindClass("java/lang/Exception"); env->ThrowNew(je, "Unknown exception in JNI code {nMatToBitmap}"); return; }*/ } void MatToBitmap(JNIEnv *env, cv::Mat& mat, jobject& bitmap) { MatToBitmap2(env, mat, bitmap, false); } #endif class CPhoneDevice : public IDevice { public: struct NETWORK { std::string iface; std::string ip; std::string netmask; std::string gateway; }; class CPhoneCamera : public NdkCamera { public: CPhoneCamera(CPhoneDevice* dev, int32_t width, int32_t height, const NdkCamera::CAMERA_PARAMS& params); virtual ~CPhoneCamera(); virtual bool on_image(cv::Mat& rgb); virtual void on_error(const std::string& msg); virtual void onDisconnected(ACameraDevice* device); virtual bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); virtual bool onOneCapture(std::shared_ptr characteristics, std::shared_ptr results, uint32_t ldr, uint32_t duration, cv::Mat rgb); virtual bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); protected: CPhoneDevice* m_dev; }; class CJpegCamera : public CPhoneCamera { public: CJpegCamera(CPhoneDevice* dev, int32_t width, int32_t height, const std::string& path, const NdkCamera::CAMERA_PARAMS& params); virtual void onImageAvailable(AImageReader* reader); virtual int32_t getOutputFormat() const; virtual bool onOneCapture(std::shared_ptr characteristics, std::shared_ptr results, uint32_t ldr, uint32_t duration, cv::Mat rgb); virtual bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); virtual bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); protected: std::string m_path; }; struct TIMER_CONTEXT { CPhoneDevice* device; unsigned int timerType; unsigned long times; void* data; unsigned long expectedTimes; unsigned long uid; }; CPhoneDevice(JavaVM* vm, jobject service, const std::string& appPath, unsigned int netId, unsigned int versionCode, const std::string& nativeLibDir); virtual ~CPhoneDevice(); virtual void SetListener(IListener* listener); virtual void SetRecognizationCfg(const CFG_RECOGNIZATION* pRecognizationCfg); virtual bool BindNetwork(int sock); virtual bool SelfTest(std::string& result); virtual bool UpdateTime(time_t ts); virtual bool UpdateSchedules(); virtual bool QuerySystemProperties(map& properties); virtual bool InstallAPP(const std::string& path, unsigned int delayedTime); virtual bool Reboot(int resetType, bool manually, const std::string& reason); virtual bool EnableGPS(bool enabled); virtual float QueryBattaryVoltage(int timesForAvg, bool* isCharging); virtual bool RequestPosition(); virtual timer_uid_t RegisterHeartbeat(unsigned int timerType, unsigned int timeout, time_t tsForNextPhoto); virtual bool TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector& osds, const std::string& path); virtual bool CloseCamera(); virtual timer_uid_t RegisterTimer(unsigned int timerType, unsigned int timeout, void* data, unsigned long times = 0); virtual bool UnregisterTimer(timer_uid_t uid); virtual unsigned long RequestWakelock(unsigned long timeout); virtual bool ReleaseWakelock(unsigned long wakelock); virtual int GetWData(WEATHER_INFO *weatherInfo); virtual int GetIceData(ICE_INFO *iceInfo, ICE_TAIL *icetail, SENSOR_PARAM *sensorParam); virtual bool OpenSensors(int sensortype); virtual bool CloseSensors(int sensortype, uint32_t delayedCloseTime); virtual bool OpenPTZSensors(uint32_t sec); virtual bool ClosePTZSensors(uint32_t delayedCloseTime); virtual bool GetPTZSensorsStatus(); virtual bool GetCameraStatus(); bool LoadNetworkInfo(); bool GetNextScheduleItem(uint32_t tsBasedZero, uint32_t scheduleTime, vector& items); void UpdatePosition(double lon, double lat, double radius, time_t ts); bool OnVideoReady(bool photoOrVideo, bool result, const char* path, unsigned int photoId); bool OnCaptureReady(bool photoOrVideo, bool result, cv::Mat& mat, unsigned int photoId); void UpdateSignalLevel(int signalLevel); void UpdateTfCardPath(const std::string& tfCardPath) { m_tfCardPath = tfCardPath; } void SetBuildTime(time_t buildTime) { mBuildTime = buildTime; } void UpdateSimcard(const std::string& simcard); void UpdateEthernet(net_handle_t nethandle, bool available); net_handle_t GetNetHandle() const; protected: std::string GetFileName() const; std::string GetVersion() const; bool SendBroadcastMessage(std::string action, int value); // bool MatchCaptureSizeRequest(ACameraManager *cameraManager, const char *selectedCameraId, unsigned int width, unsigned int height, uint32_t cameraOrientation_, bool PostProcessPhoto(const PHOTO_INFO& photoInfo, const vector& osds, const std::string& path, const std::string& cameraInfo, cv::Mat& mat); inline bool TakePhotoCb(int res, const IDevice::PHOTO_INFO& photoInfo, const string& path, time_t photoTime, const std::vector& objects) const { if (m_listener != NULL) { return m_listener->OnPhotoTaken(res, photoInfo, path, photoTime, objects); } return false; } inline bool TakePhotoCb(int result, const IDevice::PHOTO_INFO& photoInfo, const string& path, time_t photoTime) const { if (m_listener != NULL) { std::vector objects; return m_listener->OnPhotoTaken(result, photoInfo, path, photoTime, objects); } return false; } inline bool TakePTZPhotoCb(int result, const IDevice::PHOTO_INFO& photoInfo) const { if (m_listener != NULL) { std::vector objects; return m_listener->OnPTZPhotoTaken(result, photoInfo); } return false; } inline bool GetPhotoSerialsParamCb(SerialsPhotoParam ¶m) const { if (m_listener != NULL) { return m_listener->OnPhotoSerialsParamGet(param); } return false; } void QueryPowerInfo(std::map& powerInfo); std::string QueryCpuTemperature(); bool OnImageReady(cv::Mat& mat); bool onOneCapture(std::shared_ptr characteristics, std::shared_ptr results, uint32_t ldr, uint32_t duration, cv::Mat rgb); bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); bool onBurstCapture(std::shared_ptr characteristics, std::vector >& results, uint32_t ldr, uint32_t duration, std::vector >& frames); void onError(const std::string& msg); void onDisconnected(ACameraDevice* device); void CloseCamera2(CPhoneCamera* camera, unsigned int photoId, unsigned char cameraType); static void handleSignal(int sig, siginfo_t *si, void *uc); bool RegisterHandlerForSignal(int sig); void static handleTimer(union sigval v); void handleTimerImpl(TIMER_CONTEXT* context); void static handleRebootTimer(union sigval v); // void handleRebootTimerImpl(); void RestartApp(int rebootType, long timeout, const std::string& reason); int QueryBatteryVoltage(int retries); int CallExecv(int rotation, int frontCamera, const std::string& outputPath, const std::vector& images); void SetStaticIp(const std::string& iface, const std::string& ip, const std::string& netmask, const std::string& gateway); void ConvertDngToPng(const std::string& dngPath, const std::string& pngPath); void SetStaticIp(); protected: mutable std::mutex m_devLocker; JavaVM* m_vm; jobject m_javaService; std::string m_appPath; std::string m_tfCardPath; std::string m_nativeLibraryDir; NETWORK* m_network; net_handle_t m_netHandle; jmethodID mRegisterHeartbeatMid; jmethodID mUpdateCaptureScheduleMid; jmethodID mUpdateTimeMid; jmethodID mStartRecordingMid; jmethodID mRequestWakelockMid; jmethodID mReleaseWakelockMid; jmethodID mGetSystemInfoMid; jmethodID mRebootMid; jmethodID mInstallAppMid; jmethodID mEnableGpsMid; jmethodID mRequestPositionMid; jmethodID mExecHdrplusMid; jmethodID mSetStaticIpMid; jmethodID mConvertDngToPngMid; jmethodID mCallSysCameraMid; std::string mPath; IDevice::PHOTO_INFO mPhotoInfo; vector mOsds; IListener* m_listener; const CFG_RECOGNIZATION* m_pRecognizationCfg; bool mAIInitialized; unsigned int mNetId; unsigned int mVersionCode; time_t mBuildTime; atomic_ulong m_timerUidFeed; atomic_ulong m_wakelockIdFeed; atomic_ulong m_uniqueIdFeed; std::map mTimers; mutable CPhoneCamera* mCamera; time_t mHeartbeatStartTime; unsigned int mHeartbeatDuration; std::thread m_threadClose; int m_signalLevel; time_t m_signalLevelUpdateTime; std::string m_simcard; mutable std::mutex m_cameraLocker; bool m_cameraStatus; bool m_sensorsStatus; time_t m_lastTime; std::atomic m_shouldStopWaiting; IDevice::ICE_TAIL m_tempData; mutable std::mutex m_dataLocker; mutable std::mutex m_collectDataLocker; std::condition_variable m_CollectDatacv; std::atomic m_collecting; unsigned long long localDelayTime; }; #endif // __PHONE_DEVICE_H__