CameraSensor.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. #include "CameraSensor.h"
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <linux/v4l2-subdev.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <unistd.h>
  22. using namespace Linux;
  23. bool CameraSensor::set_format(uint32_t width, uint32_t height, uint32_t format)
  24. {
  25. struct v4l2_subdev_format fmt;
  26. int ret, fd;
  27. fd = open(_device_path, O_RDWR | O_CLOEXEC);
  28. if (fd < 0) {
  29. return false;
  30. }
  31. memset(&fmt, 0, sizeof(fmt));
  32. fmt.pad = 0;
  33. fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
  34. fmt.format.width = width;
  35. fmt.format.height = height;
  36. fmt.format.code = format;
  37. ret = ioctl(fd, VIDIOC_SUBDEV_S_FMT, &fmt);
  38. if (ret < 0) {
  39. return false;
  40. }
  41. return true;
  42. }