001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.examples.video; 031 032import java.awt.BorderLayout; 033import java.awt.event.ActionEvent; 034import java.awt.event.ActionListener; 035import java.io.File; 036import java.io.IOException; 037 038import javax.swing.JButton; 039import javax.swing.JFileChooser; 040import javax.swing.JFrame; 041import javax.swing.JOptionPane; 042import javax.swing.JPanel; 043 044import org.openimaj.image.ImageUtilities; 045import org.openimaj.image.MBFImage; 046import org.openimaj.video.Video; 047import org.openimaj.video.VideoDisplay; 048import org.openimaj.video.capture.VideoCapture; 049 050/** 051 * Example showing how to play a video and save single frame snapshots on a 052 * button-press. 053 * 054 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 055 * 056 */ 057public class VideoSnapshotExample { 058 private static void createUI(final Video<MBFImage> video) { 059 // create the window 060 final JFrame frame = new JFrame("Snapshot Example"); 061 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 062 063 // setup the video 064 final JPanel videoPanel = new JPanel(); 065 final VideoDisplay<MBFImage> display = VideoDisplay.createVideoDisplay(video, videoPanel); 066 frame.add(videoPanel, BorderLayout.CENTER); 067 068 // add the button 069 final JButton button = new JButton("Take Snapshot"); 070 final String SNAPSHOT_COMMAND = "snapshot"; 071 button.setActionCommand(SNAPSHOT_COMMAND); 072 frame.getContentPane().add(button, BorderLayout.SOUTH); 073 button.addActionListener(new ActionListener() { 074 @Override 075 public void actionPerformed(ActionEvent evt) { 076 if (evt.getActionCommand() == SNAPSHOT_COMMAND) { 077 // this gets called if the button is pressed 078 079 // pause the video 080 display.setMode(VideoDisplay.Mode.PAUSE); 081 082 // display a file save dialog 083 final JFileChooser saveFile = new JFileChooser(); 084 if (saveFile.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) { 085 // if a file was selected write the image 086 try { 087 // we're just going to add .jpg to the filename and 088 // save it... should be much more intelligent here 089 // in reality... 090 File outfile = saveFile.getSelectedFile(); 091 outfile = new File(outfile.getParentFile(), outfile.getName() + ".jpg"); 092 093 ImageUtilities.write(video.getCurrentFrame(), outfile); 094 } catch (final IOException ioe) { 095 // display an error if the file couldn't be saved 096 JOptionPane.showMessageDialog(null, "Unable to save file."); 097 } 098 } 099 100 // start the video playing again 101 display.setMode(VideoDisplay.Mode.PLAY); 102 } 103 } 104 }); 105 106 // prepare the window for display 107 frame.pack(); 108 109 // and display it 110 frame.setVisible(true); 111 } 112 113 /** 114 * Run the example 115 * 116 * @param args 117 */ 118 public static void main(String[] args) { 119 try { 120 // Open the video. Here we're using the default webcam, but this 121 // could 122 // be any type of video, such as a XuggleVideo which reads from a 123 // file. 124 final Video<MBFImage> video = new VideoCapture(320, 240); 125 126 // create and display the UI 127 createUI(video); 128 } catch (final IOException e) { 129 // an error occured 130 JOptionPane.showMessageDialog(null, "Unable to open video."); 131 } 132 } 133}