packageorg.openimaj.examples.video;importjava.awt.BorderLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.io.File;importjava.io.IOException;importjavax.swing.JButton;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JOptionPane;importjavax.swing.JPanel;importorg.openimaj.image.ImageUtilities;importorg.openimaj.image.MBFImage;importorg.openimaj.video.Video;importorg.openimaj.video.VideoDisplay;importorg.openimaj.video.capture.VideoCapture;/***Exampleshowinghowtoplayavideoandsavesingleframesnapshotsona*button-press.**@authorJonathonHare(jsh2@ecs.soton.ac.uk)**/publicclassVideoSnapshotExample{privatestaticvoidcreateUI(finalVideo<MBFImage>video){//createthewindowfinalJFrameframe=newJFrame("Snapshot Example");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//setupthevideofinalJPanelvideoPanel=newJPanel();finalVideoDisplay<MBFImage>display=VideoDisplay.createVideoDisplay(video,videoPanel);frame.add(videoPanel,BorderLayout.CENTER);//addthebuttonfinalJButtonbutton=newJButton("Take Snapshot");finalStringSNAPSHOT_COMMAND="snapshot";button.setActionCommand(SNAPSHOT_COMMAND);frame.getContentPane().add(button,BorderLayout.SOUTH);button.addActionListener(newActionListener(){@OverridepublicvoidactionPerformed(ActionEventevt){if(evt.getActionCommand()==SNAPSHOT_COMMAND){//thisgetscalledifthebuttonispressed//pausethevideodisplay.setMode(VideoDisplay.Mode.PAUSE);//displayafilesavedialogfinalJFileChoosersaveFile=newJFileChooser();if(saveFile.showSaveDialog(frame)==JFileChooser.APPROVE_OPTION){//ifafilewasselectedwritetheimagetry{//we're just going to add .jpg to the filename and//saveit...shouldbemuchmoreintelligenthere//inreality...Fileoutfile=saveFile.getSelectedFile();outfile=newFile(outfile.getParentFile(),outfile.getName()+".jpg");ImageUtilities.write(video.getCurrentFrame(),outfile);}catch(finalIOExceptionioe){//displayanerrorifthefilecouldn't be savedJOptionPane.showMessageDialog(null,"Unable to save file.");}}//startthevideoplayingagaindisplay.setMode(VideoDisplay.Mode.PLAY);}}});//preparethewindowfordisplayframe.pack();//anddisplayitframe.setVisible(true);}/***Runtheexample**@paramargs*/publicstaticvoidmain(String[]args){try{//Openthevideo.Herewe're using the default webcam, but this//could//beanytypeofvideo,suchasaXuggleVideowhichreadsfroma//file.finalVideo<MBFImage>video=newVideoCapture(320,240);//createanddisplaytheUIcreateUI(video);}catch(finalIOExceptione){//anerroroccuredJOptionPane.showMessageDialog(null,"Unable to open video.");}}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
My requirement is to save a snapshot of a video when I click on a button name "Take Snapshot". Please guide me how to implement this.
This example should get you going: