优化拍照资源释放

serial
Matthew 2 years ago
parent 94968a676a
commit bd77fa8ac2

@ -136,7 +136,7 @@ CPhoneDevice::CPhoneCamera::CPhoneCamera(CPhoneDevice* dev, int32_t width, int32
{ {
} }
bool CPhoneDevice::CPhoneCamera::on_image(const cv::Mat& rgb) const bool CPhoneDevice::CPhoneCamera::on_image(const cv::Mat& rgb)
{ {
if (m_dev != NULL) if (m_dev != NULL)
{ {
@ -146,7 +146,7 @@ bool CPhoneDevice::CPhoneCamera::on_image(const cv::Mat& rgb) const
return false; return false;
} }
void CPhoneDevice::CPhoneCamera::on_error(const std::string& msg) const void CPhoneDevice::CPhoneCamera::on_error(const std::string& msg)
{ {
if (m_dev != NULL) if (m_dev != NULL)
{ {
@ -353,7 +353,7 @@ bool CPhoneDevice::EnableGPS(bool enabled)
if (!res) if (!res)
{ {
ALOGE("Failed to get JNI Env"); ALOGE("Failed to get JNI Env");
return 0; return false;
} }
jboolean jenabled = enabled ? JNI_TRUE : JNI_FALSE; jboolean jenabled = enabled ? JNI_TRUE : JNI_FALSE;
env->CallVoidMethod(m_javaService, mEnableGpsMid, jenabled); env->CallVoidMethod(m_javaService, mEnableGpsMid, jenabled);
@ -361,6 +361,8 @@ bool CPhoneDevice::EnableGPS(bool enabled)
{ {
m_vm->DetachCurrentThread(); m_vm->DetachCurrentThread();
} }
return true;
} }
bool CPhoneDevice::RequestPosition() bool CPhoneDevice::RequestPosition()
@ -371,7 +373,7 @@ bool CPhoneDevice::RequestPosition()
if (!res) if (!res)
{ {
ALOGE("Failed to get JNI Env"); ALOGE("Failed to get JNI Env");
return 0; return false;
} }
jboolean ret = env->CallBooleanMethod(m_javaService, mRequestPositionMid); jboolean ret = env->CallBooleanMethod(m_javaService, mRequestPositionMid);
@ -579,15 +581,23 @@ bool CPhoneDevice::TakePhoto(const IDevice::PHOTO_INFO& photoInfo, const vector<
bool CPhoneDevice::CloseCamera() bool CPhoneDevice::CloseCamera()
{ {
if (mCamera != NULL) // if (mCamera != NULL)
{ {
delete mCamera; // mCamera->close();
mCamera = NULL; // delete mCamera;
// mCamera = NULL;
} }
return true; return true;
} }
bool CPhoneDevice::OnImageReady(const cv::Mat& mat) const void CPhoneDevice::CloseCamera2(CPhoneDevice::CPhoneCamera* camera)
{
std::this_thread::sleep_for(std::chrono::milliseconds(16));
camera->close();
delete camera;
}
bool CPhoneDevice::OnImageReady(const cv::Mat& mat)
{ {
int baseline = 0; int baseline = 0;
cv::Size textSize, textSize2; cv::Size textSize, textSize2;
@ -640,6 +650,7 @@ bool CPhoneDevice::OnImageReady(const cv::Mat& mat) const
compression_params.push_back(cv::IMWRITE_JPEG_QUALITY); compression_params.push_back(cv::IMWRITE_JPEG_QUALITY);
compression_params.push_back(mPhotoInfo.quality); compression_params.push_back(mPhotoInfo.quality);
bool res = false;
if (!std::filesystem::exists(std::filesystem::path(mPath))) if (!std::filesystem::exists(std::filesystem::path(mPath)))
{ {
bool res = cv::imwrite(mPath.c_str(), mat, compression_params); bool res = cv::imwrite(mPath.c_str(), mat, compression_params);
@ -652,27 +663,33 @@ bool CPhoneDevice::OnImageReady(const cv::Mat& mat) const
ALOGI("Succeeded to write photo: %s", mPath.c_str()); ALOGI("Succeeded to write photo: %s", mPath.c_str());
} }
TakePhotoCb(res, mPhotoInfo, mPath, time(NULL)); TakePhotoCb(res, mPhotoInfo, mPath, time(NULL));
return res;
} }
else else
{ {
ALOGI("Photo file exists: %s", mPath.c_str()); ALOGI("Photo file exists: %s", mPath.c_str());
} }
// mCamera->close(); CPhoneCamera* pCamera = mCamera;
// delete mCamera; mCamera = NULL;
// mCamera = NULL;
return true; std::thread closeThread(&CPhoneDevice::CloseCamera2, pCamera);
closeThread.detach();
return res;
} }
void CPhoneDevice::onError(const std::string& msg) const void CPhoneDevice::onError(const std::string& msg)
{ {
// XFLOG(XFLOG_SEVERITY_ERROR, "Failed to Take Photo: %s", msg.c_str()); // XFLOG(XFLOG_SEVERITY_ERROR, "Failed to Take Photo: %s", msg.c_str());
ALOGE("Failed to Take Photo: %s", msg.c_str()); ALOGE("Failed to Take Photo: %s", msg.c_str());
// mCamera->close(); CPhoneCamera* pCamera = mCamera;
mCamera = NULL;
TakePhotoCb(false, mPhotoInfo, mPath, 0); TakePhotoCb(false, mPhotoInfo, mPath, 0);
std::thread closeThread(&CPhoneDevice::CloseCamera2, pCamera);
closeThread.detach();
} }
std::string CPhoneDevice::GetFileName() const std::string CPhoneDevice::GetFileName() const

@ -28,6 +28,17 @@
class CPhoneDevice : public IDevice class CPhoneDevice : public IDevice
{ {
public: public:
class CPhoneCamera : public NdkCamera {
public:
CPhoneCamera(CPhoneDevice* dev, int32_t width, int32_t height);
virtual bool on_image(const cv::Mat& rgb);
virtual void on_error(const std::string& msg);
protected:
CPhoneDevice* m_dev;
};
CPhoneDevice(JavaVM* vm, jobject service); CPhoneDevice(JavaVM* vm, jobject service);
virtual ~CPhoneDevice(); virtual ~CPhoneDevice();
@ -70,9 +81,10 @@ protected:
void QueryPowerInfo(std::map<std::string, std::string>& powerInfo); void QueryPowerInfo(std::map<std::string, std::string>& powerInfo);
bool OnImageReady(const cv::Mat& mat) const; bool OnImageReady(const cv::Mat& mat);
void onError(const std::string& msg) const; void onError(const std::string& msg);
static void CloseCamera2(CPhoneCamera* camera);
protected: protected:
JavaVM* m_vm; JavaVM* m_vm;
@ -101,15 +113,6 @@ protected:
atomic_ulong m_wakelockIdFeed; atomic_ulong m_wakelockIdFeed;
std::map<IDevice::timer_uid_t, unsigned long> mTimers; std::map<IDevice::timer_uid_t, unsigned long> mTimers;
class CPhoneCamera : public NdkCamera {
public:
CPhoneCamera(CPhoneDevice* dev, int32_t width, int32_t height);
virtual bool on_image(const cv::Mat& rgb) const;
virtual void on_error(const std::string& msg) const;
protected:
CPhoneDevice* m_dev;
};
mutable CPhoneCamera* mCamera; mutable CPhoneCamera* mCamera;

@ -414,8 +414,6 @@ int NdkCamera::open(const char* cameraId) {
void NdkCamera::close() void NdkCamera::close()
{ {
__android_log_print(ANDROID_LOG_WARN, "NdkCamera", "DBG::close %s", mCameraId.c_str());
if (capture_session) if (capture_session)
{ {
ACameraCaptureSession_stopRepeating(capture_session); ACameraCaptureSession_stopRepeating(capture_session);
@ -457,6 +455,8 @@ void NdkCamera::close()
{ {
ACameraManager_delete(camera_manager); ACameraManager_delete(camera_manager);
camera_manager = 0; camera_manager = 0;
__android_log_print(ANDROID_LOG_WARN, "NdkCamera", "DBG::close %s", mCameraId.c_str());
} }
} }
@ -562,16 +562,16 @@ void NdkCamera::onImageAvailable(AImageReader* reader)
AImage_delete(image); AImage_delete(image);
} }
void NdkCamera::on_error(const std::string& msg) const void NdkCamera::on_error(const std::string& msg)
{ {
} }
bool NdkCamera::on_image(const cv::Mat& rgb) const bool NdkCamera::on_image(const cv::Mat& rgb)
{ {
return false; return false;
} }
void NdkCamera::on_image(const unsigned char* nv21, int nv21_width, int nv21_height) const void NdkCamera::on_image(const unsigned char* nv21, int nv21_width, int nv21_height)
{ {
__android_log_print(ANDROID_LOG_WARN, "NdkCamera", "nv21 size: %d x %d", nv21_width, nv21_height); __android_log_print(ANDROID_LOG_WARN, "NdkCamera", "nv21 size: %d x %d", nv21_width, nv21_height);
// rotate nv21 // rotate nv21

@ -46,10 +46,10 @@ public:
void close(); void close();
void onImageAvailable(AImageReader* reader); void onImageAvailable(AImageReader* reader);
virtual bool on_image(const cv::Mat& rgb) const; virtual bool on_image(const cv::Mat& rgb);
virtual void on_error(const std::string& msg) const; virtual void on_error(const std::string& msg);
virtual void on_image(const unsigned char* nv21, int nv21_width, int nv21_height) const; virtual void on_image(const unsigned char* nv21, int nv21_width, int nv21_height);
void onCaptureCompleted(ACameraCaptureSession* session, ACaptureRequest* request, const ACameraMetadata* result); void onCaptureCompleted(ACameraCaptureSession* session, ACaptureRequest* request, const ACameraMetadata* result);

@ -101,7 +101,7 @@ public class MainActivity extends AppCompatActivity {
AppConfig appConfig = getAppConfig(); AppConfig appConfig = getAppConfig();
binding.cmdid.setText(appConfig.cmdid); binding.cmdid.setText(appConfig.cmdid);
binding.server.setText(appConfig.server); binding.server.setText(appConfig.server);
binding.port.setText(Integer.toString(appConfig.port)); binding.port.setText(appConfig.port != 0 ? Integer.toString(appConfig.port) : "");
this.binding.startServBtn.setOnClickListener(new View.OnClickListener() { this.binding.startServBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {

@ -629,7 +629,8 @@ public class MicroPhotoService extends Service {
private String buildPhotoFileName(int channel, int preset, long ts) { private String buildPhotoFileName(int channel, int preset, long ts) {
String date = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); LocalDateTime nowDT = LocalDateTime.now();
String date = nowDT.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss.S"));
String photoFile = "img_" + Integer.toString(channel) + "_" + Integer.toHexString(preset).toUpperCase() + "_" + date + ".jpg"; String photoFile = "img_" + Integer.toString(channel) + "_" + Integer.toHexString(preset).toUpperCase() + "_" + date + ".jpg";
return photoFile; return photoFile;
} }

@ -0,0 +1 @@
XyMpApp
Loading…
Cancel
Save