_camera_vidcapture.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. """pygame.camera.Camera implementation using the videocapture module for windows.
  2. http://videocapture.sourceforge.net/
  3. Binary windows wheels:
  4. https://www.lfd.uci.edu/~gohlke/pythonlibs/#videocapture
  5. """
  6. import pygame
  7. def list_cameras():
  8. """Always only lists one camera.
  9. Functionality not supported in videocapture module.
  10. """
  11. return [0]
  12. # this just cycles through all the cameras trying to open them
  13. cameras = []
  14. for x in range(256):
  15. try:
  16. c = Camera(x)
  17. except:
  18. break
  19. cameras.append(x)
  20. return cameras
  21. def init():
  22. global vidcap
  23. try:
  24. import vidcap as vc
  25. except ImportError:
  26. from VideoCapture import vidcap as vc
  27. vidcap = vc
  28. def quit():
  29. global vidcap
  30. pass
  31. del vidcap
  32. class Camera:
  33. def __init__(self, device =0,
  34. size = (640,480),
  35. mode = "RGB",
  36. show_video_window=0):
  37. """device: VideoCapture enumerates the available video capture devices
  38. on your system. If you have more than one device, specify
  39. the desired one here. The device number starts from 0.
  40. show_video_window: 0 ... do not display a video window (the default)
  41. 1 ... display a video window
  42. Mainly used for debugging, since the video window
  43. can not be closed or moved around.
  44. """
  45. self.dev = vidcap.new_Dev(device, show_video_window)
  46. width, height = size
  47. self.dev.setresolution(width, height)
  48. def display_capture_filter_properties(self):
  49. """Displays a dialog containing the property page of the capture filter.
  50. For VfW drivers you may find the option to select the resolution most
  51. likely here.
  52. """
  53. self.dev.displaycapturefilterproperties()
  54. def display_capture_pin_properties(self):
  55. """Displays a dialog containing the property page of the capture pin.
  56. For WDM drivers you may find the option to select the resolution most
  57. likely here.
  58. """
  59. self.dev.displaycapturepinproperties()
  60. def set_resolution(self, width, height):
  61. """Sets the capture resolution. (without dialog)
  62. """
  63. self.dev.setresolution(width, height)
  64. def get_buffer(self):
  65. """Returns a string containing the raw pixel data.
  66. """
  67. return self.dev.getbuffer()
  68. def start(self):
  69. """ Not implemented.
  70. """
  71. def set_controls(self, **kwargs):
  72. """ Not implemented.
  73. """
  74. def stop(self):
  75. """ Not implemented.
  76. """
  77. def get_image(self, dest_surf = None):
  78. """
  79. """
  80. return self.get_surface(dest_surf)
  81. def get_surface(self, dest_surf = None):
  82. """Returns a pygame Surface.
  83. """
  84. abuffer, width, height = self.get_buffer()
  85. if abuffer:
  86. surf = pygame.image.frombuffer(abuffer, (width, height), "RGB")
  87. # swap it from a BGR surface to an RGB surface.
  88. r,g,b,a = surf.get_masks()
  89. surf.set_masks((b,g,r,a))
  90. r,g,b,a = surf.get_shifts()
  91. surf.set_shifts((b,g,r,a))
  92. surf = pygame.transform.flip(surf, 0,1)
  93. # if there is a destination surface given, we blit onto that.
  94. if dest_surf:
  95. dest_surf.blit(surf, (0,0))
  96. else:
  97. dest_surf = surf
  98. return dest_surf
  99. if __name__ == "__main__":
  100. import pygame.examples.camera
  101. pygame.camera.Camera = Camera
  102. pygame.camera.list_cameras = list_cameras
  103. pygame.examples.camera.main()