|
|
@@ -50,64 +50,9 @@ namespace Qt6QCamera {
|
|
|
//< hikrobot
|
|
|
void *handle = NULL;
|
|
|
int nRet = MV_OK;
|
|
|
-
|
|
|
+bool camera_gige = false;
|
|
|
SenderCustom sender_custom;
|
|
|
cv::Mat cv_frame;
|
|
|
-// 实现JNI方法
|
|
|
-extern "C" void Java_com_yfkj_cb_Q6iot_UvcCameraActivity_sendToCpp(JNIEnv* env, jobject thiz, jbyteArray rgbData,jint width,jint height)
|
|
|
-{
|
|
|
-
|
|
|
- // 获取RGB数据长度
|
|
|
- jsize length = env->GetArrayLength(rgbData);
|
|
|
- // 将Java的jbyteArray转换为C++的byte数组
|
|
|
- jbyte* rgbArray = env->GetByteArrayElements(rgbData, nullptr);
|
|
|
- #if 1
|
|
|
- try {
|
|
|
- // 将QByteArray转换为cv::Mat对象
|
|
|
- cv_frame = cv::Mat(height, width, CV_8UC3, rgbArray);
|
|
|
- // 检查转换后的cv::Mat是否为空
|
|
|
- if (cv_frame.empty()) {
|
|
|
- // 处理转换失败的情况
|
|
|
- qDebug() << "Failed to convert QByteArray to cv::Mat";
|
|
|
- return; // 可能需要根据实际情况进行处理,比如抛出异常或返回错误码
|
|
|
- }
|
|
|
- //sender_custom.sendSignal(cv_frame.clone());
|
|
|
- // 在这里进行对cv_frame的处理
|
|
|
- } catch (const cv::Exception& ex) {
|
|
|
- // 处理OpenCV异常
|
|
|
- qDebug() << "OpenCV Exception: " << ex.what();
|
|
|
- return; // 可能需要根据实际情况进行处理,比如抛出异常或返回错误码
|
|
|
- } catch (const std::exception& ex) {
|
|
|
- // 处理其他异常
|
|
|
- qDebug() << "Exception: " << ex.what();
|
|
|
- return; // 可能需要根据实际情况进行处理,比如抛出异常或返回错误码
|
|
|
- } catch (...) {
|
|
|
- // 处理未知异常
|
|
|
- qDebug() << "Unknown Exception";
|
|
|
- return; // 可能需要根据实际情况进行处理,比如抛出异常或返回错误码
|
|
|
- }
|
|
|
- #endif
|
|
|
- sender_custom.sendSignal(cv_frame.clone());
|
|
|
- // 释放Java数组
|
|
|
- env->ReleaseByteArrayElements(rgbData, rgbArray, JNI_ABORT);
|
|
|
-
|
|
|
- // 在这里进行RGB数据的处理
|
|
|
- //qDebug() << "Received RGB data from Java:" << width << " " << height << "@" << length;//cv_frame.total() * cv_frame.elemSize();
|
|
|
-}
|
|
|
-
|
|
|
-extern "C" void Java_com_yfkj_cb_Q6iot_UvcCameraActivity_UvcHandle(JNIEnv* env, jobject thiz, jstring strData)
|
|
|
-{
|
|
|
- const char* strChars = env->GetStringUTFChars(strData, nullptr);
|
|
|
- QString qstr = QString::fromUtf8(strChars);
|
|
|
- env->ReleaseStringUTFChars(strData, strChars);
|
|
|
- sender_custom.signalQStr(qstr);
|
|
|
- qDebug() << "Received string data from Java:" << qstr;
|
|
|
- if(qstr.contains("UVCFAILED"))
|
|
|
- {
|
|
|
- qDebug() << "相机打开失败!";
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
// pthread 线程函数
|
|
|
static void* pthreadGrab(void* ptr){
|
|
|
@@ -160,31 +105,6 @@ static void* pthreadGrab(void* ptr){
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-ImageProvider::ImageProvider() :
|
|
|
- QQuickImageProvider(QQuickImageProvider::Image)
|
|
|
-{
|
|
|
-// qml_img = QImage(200, 200, QImage::Format_ARGB32);
|
|
|
-// QColor color;
|
|
|
-// color.setGreen(200);
|
|
|
-// qml_img.fill(color);
|
|
|
-}
|
|
|
-
|
|
|
-QImage ImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
|
|
|
-{
|
|
|
- //qDebug()<<"ImageProvider requestImage"<<id<<size<<requestedSize;
|
|
|
- return qml_img;
|
|
|
-}
|
|
|
-
|
|
|
-void
|
|
|
-ImageProvider::updateImage(QImage i_image){
|
|
|
- qml_img = i_image;
|
|
|
- //qDebug() << "updateImage" << qml_img.size() << qml_img.format();
|
|
|
-}
|
|
|
-
|
|
|
-QPixmap ImageProvider::requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
|
|
|
-{
|
|
|
- return QPixmap::fromImage(qml_img);
|
|
|
-}
|
|
|
|
|
|
FPSTimer::FPSTimer(QObject *parent) : QObject(parent)
|
|
|
{
|
|
|
@@ -260,7 +180,6 @@ Frames::Frames(QObject * parent)
|
|
|
qRegisterMetaType<QVector<UVCCaptureDescriptor>>("QVector<UVCCaptureDescriptor>");
|
|
|
}
|
|
|
|
|
|
- m_pImgProvider = new ImageProvider();
|
|
|
stitch_des = 0;
|
|
|
|
|
|
connect( m_1s, &QTimer::timeout, this, &Frames::timer);
|
|
|
@@ -300,19 +219,65 @@ Frames::Frames(QObject * parent)
|
|
|
|
|
|
Frames::~Frames()
|
|
|
{
|
|
|
+ qDebug() << "=== Frames Destructor Start ===";
|
|
|
+ // 1. 先停止所有定时器
|
|
|
+ if (m_1s && m_1s->isActive()) {
|
|
|
+ m_1s->stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_fps_timer && m_fps_timer->isRunning()) {
|
|
|
+ m_fps_timer->setRunning(false);
|
|
|
+ }
|
|
|
+ // 2. 设置停止标志,确保相机不再运行
|
|
|
+ camera_sta = false;
|
|
|
+
|
|
|
if (handle)
|
|
|
{
|
|
|
+ // 先停止抓图
|
|
|
+ if (MV_CC_StopGrabbing(handle) != MV_OK) {
|
|
|
+ qDebug() << "Warning: Failed to stop grabbing";
|
|
|
+ }
|
|
|
+ // 等待抓图线程结束
|
|
|
+ if (m_grabThread.isRunning()) {
|
|
|
+ m_grabThread.cancel(); // 先尝试取消
|
|
|
+ m_grabThread.waitForFinished();
|
|
|
+ }
|
|
|
+ // 关闭设备
|
|
|
+ if (MV_CC_CloseDevice(handle) != MV_OK) {
|
|
|
+ qDebug() << "Warning: Failed to close device";
|
|
|
+ }
|
|
|
MV_CC_DestroyHandle(handle);
|
|
|
handle = NULL;
|
|
|
}
|
|
|
if (m_processThread.isRunning()) {
|
|
|
m_processThread.waitForFinished(); // wait for finish thread
|
|
|
}
|
|
|
- auto qtContext = QJniObject(QNativeInterface::QAndroidApplication::context());
|
|
|
- if(qtContext.isValid()){
|
|
|
- jClass->callMethod<void>("clearCameraHelper","(Landroid/content/Context;)V",qtContext.object());
|
|
|
+
|
|
|
+ // 清理JNI资源
|
|
|
+ if (jClass) {
|
|
|
+ delete jClass;
|
|
|
+ jClass = nullptr;
|
|
|
+ }
|
|
|
+ if (m_imageCapture) {
|
|
|
+ delete m_imageCapture;
|
|
|
+ m_imageCapture = nullptr;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_1s) {
|
|
|
+ delete m_1s;
|
|
|
+ m_1s = nullptr;
|
|
|
}
|
|
|
|
|
|
+ if (m_fps_timer) {
|
|
|
+ delete m_fps_timer;
|
|
|
+ m_fps_timer = nullptr;
|
|
|
+ }
|
|
|
+ if(m_videoSink)
|
|
|
+ {
|
|
|
+ delete m_videoSink;
|
|
|
+ m_videoSink = nullptr;
|
|
|
+ }
|
|
|
+ qDebug() << "=== Frames Destructor End ===";
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -384,6 +349,8 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
#else
|
|
|
if (handle)
|
|
|
{
|
|
|
+ MV_CC_StopGrabbing(handle);
|
|
|
+ MV_CC_CloseDevice(handle);
|
|
|
MV_CC_DestroyHandle(handle);
|
|
|
handle = NULL;
|
|
|
}
|
|
|
@@ -467,6 +434,7 @@ Frames::setCameraSw(bool stat_cam)
|
|
|
{
|
|
|
qDebug() << "Warning: Get Packet Size fail nRet:"<< nPacketSize;
|
|
|
}
|
|
|
+ camera_gige = true;
|
|
|
}
|
|
|
// ch:设置触发模式为off | eb:Set trigger mode as off
|
|
|
nRet = MV_CC_SetEnumValue(handle, "TriggerMode", MV_TRIGGER_MODE_OFF);
|
|
|
@@ -711,7 +679,12 @@ Frames::handleGrab(bool sta_cam){
|
|
|
cv::cvtColor(tempMat, bgrImage, cv::COLOR_YUV2RGB_YVYU);//YUV422
|
|
|
cv::cvtColor(bgrImage,m_frame,cv::COLOR_RGB2RGBA);
|
|
|
#else
|
|
|
- cv::cvtColor(tempMat,m_frame,cv::COLOR_BayerRG2RGBA);
|
|
|
+ if(!camera_gige){
|
|
|
+ cv::cvtColor(tempMat,m_frame,cv::COLOR_BayerRG2RGBA);
|
|
|
+ }else {
|
|
|
+ cv::cvtColor(tempMat,m_frame,cv::COLOR_BayerGR2BGRA);
|
|
|
+ }
|
|
|
+
|
|
|
#endif
|
|
|
#if 1
|
|
|
const int size_frame = m_frame.total()*m_frame.elemSize(); //字节数
|