The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

199 lines
7.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-9 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "../jucedemo_headers.h"
  19. #if JUCE_USE_CAMERA
  20. //==============================================================================
  21. class CameraDemo : public Component,
  22. public ComboBoxListener,
  23. public ButtonListener,
  24. public CameraImageListener
  25. {
  26. public:
  27. //==============================================================================
  28. CameraDemo()
  29. {
  30. setName (T("Camera"));
  31. cameraDevice = 0;
  32. cameraPreviewComp = 0;
  33. lastSnapshot = 0;
  34. recordingMovie = false;
  35. addAndMakeVisible (cameraSelectorComboBox = new ComboBox (T("Camera")));
  36. createListOfCameras();
  37. cameraSelectorComboBox->setSelectedId (1);
  38. cameraSelectorComboBox->addListener (this);
  39. addAndMakeVisible (snapshotButton = new TextButton (T("Take a snapshot")));
  40. snapshotButton->addButtonListener (this);
  41. snapshotButton->setEnabled (false);
  42. addAndMakeVisible (recordMovieButton = new TextButton (T("Record a movie file (to your desktop)...")));
  43. recordMovieButton->addButtonListener (this);
  44. recordMovieButton->setEnabled (false);
  45. cameraSelectorComboBox->setSelectedId (2);
  46. }
  47. ~CameraDemo()
  48. {
  49. deleteAllChildren();
  50. delete cameraDevice;
  51. delete lastSnapshot;
  52. }
  53. void paint (Graphics& g)
  54. {
  55. if (lastSnapshot != 0)
  56. g.drawImageWithin (lastSnapshot,
  57. getWidth() / 2 + 10, 40,
  58. getWidth() / 2 - 20, getHeight() - 50,
  59. RectanglePlacement::centred, false);
  60. }
  61. void resized()
  62. {
  63. cameraSelectorComboBox->setBounds (10, 4, 250, 24);
  64. snapshotButton->changeWidthToFitText (24);
  65. snapshotButton->setTopLeftPosition (cameraSelectorComboBox->getRight() + 20, 4);
  66. recordMovieButton->changeWidthToFitText (24);
  67. recordMovieButton->setTopLeftPosition (snapshotButton->getRight() + 20, 4);
  68. if (cameraPreviewComp != 0)
  69. cameraPreviewComp->setBounds (10, 40, getWidth() / 2 - 20, getHeight() - 50);
  70. }
  71. void comboBoxChanged (ComboBox*)
  72. {
  73. // This is called when the user chooses a camera from the drop-down list.
  74. deleteAndZero (cameraDevice);
  75. deleteAndZero (cameraPreviewComp);
  76. recordingMovie = false;
  77. if (cameraSelectorComboBox->getSelectedId() > 1)
  78. {
  79. // Try to open the user's choice of camera..
  80. cameraDevice = CameraDevice::openDevice (cameraSelectorComboBox->getSelectedId() - 2);
  81. // and if it worked, create a preview component for it..
  82. if (cameraDevice != 0)
  83. addAndMakeVisible (cameraPreviewComp = cameraDevice->createViewerComponent());
  84. }
  85. snapshotButton->setEnabled (cameraDevice != 0);
  86. recordMovieButton->setEnabled (cameraDevice != 0);
  87. resized();
  88. }
  89. void createListOfCameras()
  90. {
  91. cameraSelectorComboBox->clear();
  92. cameraSelectorComboBox->addItem ("No camera", 1);
  93. cameraSelectorComboBox->addSeparator();
  94. StringArray cameras = CameraDevice::getAvailableDevices();
  95. for (int i = 0; i < cameras.size(); ++i)
  96. cameraSelectorComboBox->addItem (cameras[i], i + 2);
  97. }
  98. void buttonClicked (Button* b)
  99. {
  100. if (cameraDevice != 0)
  101. {
  102. if (b == recordMovieButton)
  103. {
  104. // The user has clicked the record movie button..
  105. if (! recordingMovie)
  106. {
  107. // Start recording to a file on the user's desktop..
  108. recordingMovie = true;
  109. File file (File::getSpecialLocation (File::userDesktopDirectory)
  110. .getNonexistentChildFile (T("JuceCameraDemo"),
  111. CameraDevice::getFileExtension()));
  112. cameraDevice->startRecordingToFile (file);
  113. recordMovieButton->setButtonText (T("Stop Recording"));
  114. }
  115. else
  116. {
  117. // Already recording, so stop...
  118. recordingMovie = false;
  119. cameraDevice->stopRecording();
  120. recordMovieButton->setButtonText (T("Start recording (to a file on your desktop)"));
  121. }
  122. }
  123. else
  124. {
  125. // When the user clicks the snapshot button, we'll attach ourselves to
  126. // the camera as a listener, and wait for an image to arrive...
  127. cameraDevice->addListener (this);
  128. }
  129. }
  130. }
  131. // This is called by the camera device when a new image arrives
  132. void imageReceived (Image& image)
  133. {
  134. // In this app we just want to take one image, so as soon as this happens,
  135. // we'll unregister ourselves as a listener.
  136. if (cameraDevice != 0)
  137. cameraDevice->removeListener (this);
  138. // This callback won't be on the message thread, so need to lock it before using
  139. // data that may already be in use..
  140. const MessageManagerLock mm;
  141. deleteAndZero (lastSnapshot);
  142. lastSnapshot = image.createCopy();
  143. repaint();
  144. }
  145. private:
  146. //==============================================================================
  147. CameraDevice* cameraDevice;
  148. ComboBox* cameraSelectorComboBox;
  149. TextButton* snapshotButton;
  150. TextButton* recordMovieButton;
  151. Component* cameraPreviewComp;
  152. bool recordingMovie;
  153. Image* lastSnapshot;
  154. };
  155. //==============================================================================
  156. Component* createCameraDemo()
  157. {
  158. return new CameraDemo();
  159. }
  160. #endif