CameraDataHandleAndShow.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "CameraDataHandleAndShow.h"
  2. void ImageGrabber::stop() {
  3. m_running = false;
  4. };
  5. // long ImageGrabber::GrabImagetest(int iCameraId, ImageInfo& image){
  6. // static QMutex mutex;
  7. // QMutexLocker locker(&mutex);
  8. // // 生成测试图像数据
  9. // static int counter = 0;
  10. // int width = 640;
  11. // int height = 480;
  12. // image.width = width;
  13. // image.height = height;
  14. // image.format = RGB888;
  15. // image.channel = 3;
  16. // static QByteArray buffer(width * height * 3, 0);
  17. // unsigned char *ptr = reinterpret_cast<unsigned char*>(buffer.data());
  18. // // 生成渐变测试图
  19. // for(int y=0; y<height; ++y) {
  20. // for(int x=0; x<width; ++x) {
  21. // int offset = (y*width + x)*3;
  22. // ptr[offset] = x*255/width; // R
  23. // ptr[offset+1] = (x+y)*255/(width+height); // G
  24. // ptr[offset+2] = y*255/height; // B
  25. // }
  26. // }
  27. // image.data = ptr;
  28. // return 0;
  29. // }
  30. long ImageGrabber::GrabImagetest(int iCameraId, ImageInfo& image) {
  31. static QMutex mutex;
  32. QMutexLocker locker(&mutex);
  33. // 动态控制变量(每次调用自增)
  34. static int counter = 0;
  35. counter = (counter + 1) % 1000; // 防止溢出
  36. const int width = 640;
  37. const int height = 480;
  38. // 生成动态测试图案(示例包含三种动态模式)
  39. static QByteArray buffer(width * height * 3, 0);
  40. unsigned char* ptr = reinterpret_cast<unsigned char*>(buffer.data());
  41. for(int y = 0; y < height; ++y) {
  42. for(int x = 0; x < width; ++x) {
  43. int offset = (y * width + x) * 3;
  44. // 模式1:水平移动的色带
  45. int modulatedX = (x + counter) % width;
  46. // 模式2:脉动中心圆
  47. float dist = sqrt(pow(x-width/2,2) + pow(y-height/2,2));
  48. float pulse = (sin(counter*0.1) + 1) * 50;
  49. // 模式3:动态噪点
  50. float noise = (rand() % 100)/100.0 * 50 * sin(counter*0.05);
  51. // 强制类型转换解决浮点取模问题
  52. ptr[offset] = static_cast<int>(modulatedX*255/width + pulse + noise) % 256; // R
  53. ptr[offset+1] = static_cast<int>((x+y+counter)*255/(width+height) + noise) % 256; // G
  54. ptr[offset+2] = static_cast<int>(y*255/height + pulse) % 256; // B
  55. }
  56. }
  57. // 返回图像参数
  58. image.width = width;
  59. image.height = height;
  60. image.format = RGB888;
  61. image.channel = 3;
  62. image.data = ptr;
  63. return 0;
  64. }
  65. void ImageGrabber::run(){
  66. while(m_running) {
  67. ImageInfo imgInfo;
  68. if(GrabImagetest(1, imgInfo) == 0) {
  69. QImage image = convertImage(imgInfo);
  70. emit imageGrabbed(image);
  71. }
  72. msleep(1000);
  73. }
  74. }
  75. QImage ImageGrabber::convertImage(const ImageInfo &info) {
  76. QImage::Format format = QImage::Format_Invalid;
  77. switch(info.format) {
  78. case GRAY8: format = QImage::Format_Grayscale8; break;
  79. case RGB888: format = QImage::Format_RGB888; break;
  80. case ARGB8888:format = QImage::Format_ARGB32; break;
  81. case RGB32: format = QImage::Format_RGB32; break;
  82. case YUV422: return convertYUV422ToRGB(info);
  83. default: return QImage();
  84. }
  85. return QImage(info.data, info.width, info.height,
  86. info.width * info.channel, format).copy();
  87. }
  88. QImage ImageGrabber::convertYUV422ToRGB(const ImageInfo &info) {
  89. // 分配RGB数据的内存
  90. QVector<unsigned char> rgbData(info.width * info.height * 3);
  91. for (int y = 0; y < info.height; ++y) {
  92. for (int x = 0; x < info.width; x += 2) {
  93. // 计算YUV数据索引(每4字节处理2个像素)
  94. int yuvIndex = y * info.width * 2 + x * 2;
  95. // 提取YUV分量
  96. unsigned char Y0 = info.data[yuvIndex];
  97. unsigned char U = info.data[yuvIndex + 1];
  98. unsigned char Y1 = info.data[yuvIndex + 2];
  99. unsigned char V = info.data[yuvIndex + 3];
  100. // 将U/V转换为有符号(-128~127)
  101. float u = (float)U - 128.0f;
  102. float v = (float)V - 128.0f;
  103. // 处理第一个像素(Y0)
  104. float yScaled = (Y0 - 16) * 1.164f; // Y分量归一化
  105. int r = (int)(yScaled + 1.596f * v);
  106. int g = (int)(yScaled - 0.391f * u - 0.813f * v);
  107. int b = (int)(yScaled + 2.018f * u);
  108. // 钳位并存储第一个像素
  109. int rgbIndex = (y * info.width + x) * 3;
  110. rgbData[rgbIndex] = (unsigned char)(r < 0 ? 0 : r > 255 ? 255 : r);
  111. rgbData[rgbIndex + 1] = (unsigned char)(g < 0 ? 0 : g > 255 ? 255 : g);
  112. rgbData[rgbIndex + 2] = (unsigned char)(b < 0 ? 0 : b > 255 ? 255 : b);
  113. // 处理第二个像素(Y1)
  114. yScaled = (Y1 - 16) * 1.164f;
  115. r = (int)(yScaled + 1.596f * v);
  116. g = (int)(yScaled - 0.391f * u - 0.813f * v);
  117. b = (int)(yScaled + 2.018f * u);
  118. // 钳位并存储第二个像素
  119. rgbIndex += 3; // 移动到下一个像素位置
  120. if (x + 1 < info.width) { // 确保不越界
  121. rgbData[rgbIndex] = (unsigned char)(r < 0 ? 0 : r > 255 ? 255 : r);
  122. rgbData[rgbIndex + 1] = (unsigned char)(g < 0 ? 0 : g > 255 ? 255 : g);
  123. rgbData[rgbIndex + 2] = (unsigned char)(b < 0 ? 0 : b > 255 ? 255 : b);
  124. }
  125. }
  126. }
  127. // 创建QImage对象
  128. QImage image(rgbData.data(), info.width, info.height, QImage::Format_RGB888);
  129. return image;
  130. }