You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
3.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "dataMediaImageSeq.h"
  2. imageSequence::imageSequence(){
  3. head = NULL;
  4. tail = NULL;
  5. videoWriter = NULL;
  6. savePath = NULL;
  7. //TS = new basicTimeStamp();
  8. TS = getUnixTimeStamp();
  9. currentCount = 0;
  10. saveMode = false;
  11. maxCount = IMAGE_SEQ_DEFAULT_LENGTH;
  12. };
  13. imageSequence::imageSequence( int frameCount ){
  14. head = NULL;
  15. tail = NULL;
  16. videoWriter = NULL;
  17. savePath = NULL;
  18. //TS = new basicTimeStamp();
  19. TS = getUnixTimeStamp();
  20. currentCount = 0;
  21. saveMode = false;
  22. maxCount = frameCount;
  23. };
  24. imageSequence::~imageSequence(){
  25. if( currentCount ) {
  26. if( saveMode ) this->flush();
  27. };
  28. if( savePath ) {
  29. delete( savePath );
  30. };
  31. };
  32. bool imageSequence::alarm(){
  33. //TS->setCurrent();
  34. if( saveCount == 0 ) {
  35. // Set TS just for New Clean Events.
  36. TS = getUnixTimeStamp();
  37. };
  38. saveCount = maxCount >> 1;
  39. if( saveCount ) { // If MaxCount too Low ( <3 ) do nothing
  40. saveMode = true;
  41. return true;
  42. };
  43. return false;
  44. };
  45. bool imageSequence::pushImage( basicMediaImage *Img ){
  46. bool retVal;
  47. retVal = false;
  48. if( Img != NULL ) {
  49. if( tail == NULL ){
  50. head = ( imageSequenceNode *) malloc(sizeof( imageSequenceNode ));
  51. if( head!= NULL ) {
  52. tail = head;
  53. tail->frame = Img;
  54. tail->next = NULL;
  55. retVal = true;
  56. currentCount++;
  57. };
  58. } else {
  59. tail->next = ( imageSequenceNode *) malloc(sizeof( imageSequenceNode ));
  60. if( tail->next != NULL ) {
  61. tail = tail->next;
  62. tail->frame = Img;
  63. tail->next = NULL;
  64. retVal = true;
  65. currentCount++;
  66. };
  67. };
  68. };
  69. if( saveMode ){
  70. if( head != NULL ){
  71. imageSequenceNode *tNode;
  72. tNode = head;
  73. saveImage( head->frame );
  74. head = head->next;
  75. delete( tNode->frame );
  76. delete( tNode );
  77. currentCount--;
  78. };
  79. };
  80. return retVal;
  81. };
  82. /*
  83. void createAlphaMat( cv::Mat &mat ){
  84. CV_Assert( mat.channels() == 4);
  85. for (int i = 0; i < mat.rows; ++i) {
  86. for (int j = 0; j < mat.cols; ++j) {
  87. Vec4b& bgra = mat.at<Vec4b>(i, j);
  88. bgra[0] = UCHAR_MAX; // Blue
  89. bgra[1] = saturate_cast<uchar>((float (mat.cols - j)) / ((float)mat.cols) * UCHAR_MAX); // Green
  90. bgra[2] = saturate_cast<uchar>((float (mat.rows - i)) / ((float)mat.rows) * UCHAR_MAX); // Red
  91. bgra[3] = saturate_cast<uchar>(0.5 * (bgra[1] + bgra[2])); // Alpha
  92. }
  93. }
  94. };
  95. */
  96. void imageSequence::saveImage( basicMediaImage *Img ){
  97. switch( saveMethod ) {
  98. case 1:{
  99. std::vector<int> compression_params;
  100. compression_params.push_back( CV_IMWRITE_PNG_COMPRESSION );
  101. compression_params.push_back( 9 );
  102. try {
  103. imwrite( "alpha.png", *Img->getMatHandle(), compression_params );
  104. //} catch ( std::runtime_error &ex ){
  105. } catch ( ... ){
  106. //fprintf(stderr, "Exception converting image to PNG format: %s\n", ex.what());
  107. return;
  108. };
  109. break;
  110. };
  111. case 2:{ // Save Image just after trigger.
  112. break;
  113. };
  114. case 3:{ // Always save video stream
  115. if( videoWriter ) {
  116. if( videoWriter->isOpened() ) {
  117. videoWriter->write( *Img->getMatHandle() );
  118. } else {
  119. if ( videoWriter->open( savePath, CV_FOURCC('M','J','P','G'), framePerSecond, frameSize, true ) ){
  120. videoWriter->write( *Img->getMatHandle() );
  121. };
  122. };
  123. };
  124. break;
  125. };
  126. };
  127. };
  128. bool imageSequence::flush( void ){
  129. return true;
  130. };
  131. void imageSequence::setSaveMethod( U8 Method, CSTR Destination ){
  132. switch( Method ) {
  133. case 1:
  134. if( videoWriter ) {
  135. delete( videoWriter );
  136. videoWriter = NULL;
  137. };
  138. break;
  139. case 2:
  140. videoWriter = new cv::VideoWriter();
  141. break;
  142. case 3:
  143. char fileName[1024];
  144. framePerSecond = 10.0;
  145. frameSize = cv::Size(640,480);
  146. savePath = createTimeHashPath( Destination, TS, fileName );
  147. videoWriter = new cv::VideoWriter( savePath, CV_FOURCC('M','J','P','G'), framePerSecond, frameSize, true );
  148. break;
  149. };
  150. };
  151. //CV_FOURCC('P','I','M','1')
  152. //CV_FOURCC('M','J','P','G')