No Description
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.

camera.cpp 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2018 dimercur
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "camera.h"
  18. #include "img.h"
  19. #include <unistd.h>
  20. using namespace cv;
  21. Camera::Camera(int size, int fps) {
  22. this->SetSize(size);
  23. #ifndef __FOR_PC__
  24. this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  25. this->cap.set(CV_CAP_PROP_FRAME_WIDTH,width);
  26. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT,height);
  27. this->cap.set(CV_CAP_PROP_FPS, fps);
  28. #endif /* __FOR_PC__ */
  29. }
  30. bool Camera::Open() {
  31. bool status = false;
  32. #ifdef __FOR_PC__
  33. if (this->cap.open(0)) {
  34. //this->cap.set(CV_CAP_PROP_FORMAT, CV_8UC3);
  35. this->cap.set(CV_CAP_PROP_FRAME_WIDTH,width);
  36. this->cap.set(CV_CAP_PROP_FRAME_HEIGHT,height);
  37. status =true;
  38. }
  39. #else
  40. if (this->cap.open()) {
  41. cout<<"Camera warmup 2sec"<<endl<<flush;
  42. sleep(2);
  43. cout<<"Start capture"<<endl<<flush;
  44. status =true;
  45. }
  46. #endif /* __FOR_PC__ */
  47. return status;
  48. }
  49. void Camera::Close() {
  50. this->cap.release();
  51. }
  52. void Camera::SetSize(int size) {
  53. this->size = size;
  54. switch (size){
  55. case xs:
  56. this->width = 480;
  57. this->height = 360;
  58. break;
  59. case sm:
  60. this->width = 640;
  61. this->height = 480;
  62. break;
  63. case md:
  64. this->width = 1024;
  65. this->height = 768;
  66. break;
  67. case lg:
  68. this->width = 1280;
  69. this->height = 960;
  70. break;
  71. default:
  72. this->width = 480;
  73. this->height = 360;
  74. }
  75. }
  76. Img Camera::Grab() {
  77. ImageMat frame;
  78. #ifdef __FOR_PC__
  79. cap >> frame;
  80. Img capture = Img(frame);
  81. #else
  82. cap.grab();
  83. cap.retrieve (frame);
  84. cvtColor(frame,frame,CV_BGR2RGB);
  85. Img capture = Img(frame);
  86. #endif /* __FOR_PC__ */
  87. return capture;
  88. }
  89. bool Camera::IsOpen() {
  90. return cap.isOpened();
  91. }
  92. int Camera::GetWidth() const {
  93. return width;
  94. }
  95. int Camera::GetHeight() const {
  96. return height;
  97. }