PageRenderTime 24ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gettingstarted/E05_Timelapse.java

http://github.com/kritzikratzi/edsdk4j
Java | 167 lines | 140 code | 12 blank | 15 comment | 4 complexity | 77ff397491aeb906c84594aa3997032c MD5 | raw file
  1. package gettingstarted;
  2. import java.awt.GridBagConstraints;
  3. import java.awt.GridBagLayout;
  4. import java.awt.Insets;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.File;
  8. import java.text.SimpleDateFormat;
  9. import java.util.Date;
  10. import java.util.LinkedList;
  11. import javax.swing.BorderFactory;
  12. import javax.swing.JComboBox;
  13. import javax.swing.JFrame;
  14. import javax.swing.JLabel;
  15. import javax.swing.JPanel;
  16. import edsdk.api.CanonCamera;
  17. import edsdk.api.commands.ShootCommand;
  18. import edsdk.utils.CanonConstants.DescriptiveEnum;
  19. import edsdk.utils.CanonConstants.EdsAv;
  20. import edsdk.utils.CanonConstants.EdsISOSpeed;
  21. import edsdk.utils.CanonConstants.EdsSaveTo;
  22. import edsdk.utils.CanonConstants.EdsTv;
  23. /**
  24. * An example of taking multiple sequential shots.
  25. *
  26. * Copyright Š 2014 Hansi Raber <super@superduper.org>, Ananta Palani
  27. * <anantapalani@gmail.com>
  28. * This work is free. You can redistribute it and/or modify it under the
  29. * terms of the Do What The Fuck You Want To Public License, Version 2,
  30. * as published by Sam Hocevar. See the COPYING file for more details.
  31. *
  32. * @author hansi
  33. * @author Ananta Palani
  34. *
  35. */
  36. public class E05_Timelapse {
  37. public static void main( final String[] args ) throws InterruptedException {
  38. //Native.setProtected( true );
  39. final CanonCamera camera = new CanonCamera();
  40. if ( camera.openSession() ) {
  41. E05_Timelapse.createUI( camera );
  42. while ( true ) {
  43. System.out.println( "=========================================" );
  44. final long level = camera.getBatteryLevel();
  45. if ( level != 0xffffffff ) {
  46. System.out.println( "Battery Level = " + level );
  47. }
  48. camera.execute( new ShootCommand( EdsSaveTo.kEdsSaveTo_Host, 20, E05_Timelapse.filename() ) );
  49. try {
  50. Thread.sleep( 15000 );
  51. }
  52. catch ( final InterruptedException e ) {
  53. // TODO Auto-generated catch block
  54. e.printStackTrace();
  55. }
  56. }
  57. }
  58. CanonCamera.close();
  59. }
  60. public static File filename() {
  61. return new File( "images\\" +
  62. new SimpleDateFormat( "yyyy\\MM\\dd\\HH-mm-ss" ).format( new Date() ) +
  63. ".jpg" );
  64. }
  65. private static void createUI( final CanonCamera camera ) {
  66. final JFrame frame = new JFrame();
  67. final JPanel content = new JPanel( new GridBagLayout() );
  68. content.setBorder( BorderFactory.createEmptyBorder( 10, 10, 10, 10 ) );
  69. final GridBagConstraints gbc = new GridBagConstraints();
  70. gbc.anchor = GridBagConstraints.EAST;
  71. gbc.fill = GridBagConstraints.HORIZONTAL;
  72. gbc.insets = new Insets( 3, 3, 3, 3 );
  73. gbc.gridy = 1;
  74. final EdsTv currentShutterSpeed = camera.getShutterSpeed();
  75. final EdsAv currentApertureValue = camera.getApertureValue();
  76. final EdsISOSpeed currentISOSpeed = camera.getISOSpeed();
  77. final EdsTv[] availableShutterSpeeds = camera.getAvailableShutterSpeeds();
  78. final EdsAv[] availableApertureValues = camera.getAvailableApertureValues();
  79. final EdsISOSpeed[] availableISOSpeeds = camera.getAvailableISOSpeeds();
  80. E05_Timelapse.addCombobox( content, gbc, "Shutter Speed", availableShutterSpeeds, currentShutterSpeed, new Callback() {
  81. @Override
  82. public void call( final String name ) {
  83. camera.setShutterSpeed( EdsTv.enumOfDescription( name ) );
  84. }
  85. } );
  86. E05_Timelapse.addCombobox( content, gbc, "Aperture", availableApertureValues, currentApertureValue, new Callback() {
  87. @Override
  88. public void call( final String name ) {
  89. camera.setApertureValue( EdsAv.enumOfDescription( name ) );
  90. }
  91. } );
  92. E05_Timelapse.addCombobox( content, gbc, "ISO", availableISOSpeeds, currentISOSpeed, new Callback() {
  93. @Override
  94. public void call( final String name ) {
  95. camera.setISOSpeed( EdsISOSpeed.enumOfDescription( name ) );
  96. }
  97. } );
  98. frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  99. frame.setContentPane( content );
  100. frame.setSize( 500, 400 );
  101. frame.setVisible( true );
  102. }
  103. private static void addCombobox( final JPanel content,
  104. final GridBagConstraints gbc,
  105. final String label,
  106. final DescriptiveEnum<?>[] enums,
  107. final DescriptiveEnum<?> selected,
  108. final Callback callback ) {
  109. gbc.gridx = 0;
  110. gbc.weightx = 0;
  111. content.add( new JLabel( label ), gbc );
  112. gbc.gridx = 1;
  113. gbc.weightx = 1;
  114. // find the items ...
  115. if ( enums == null ) {
  116. content.add( new JLabel( "not available with current mode / lens" ), gbc );
  117. } else {
  118. final LinkedList<String> items = new LinkedList<String>();
  119. for ( final DescriptiveEnum<?> e : enums ) {
  120. items.add( e.description() );
  121. }
  122. // In Java 6 JCombBox is not generic, so to compile in Java > 6 have to do this
  123. @SuppressWarnings( { "rawtypes", "unchecked" } )
  124. final JComboBox combo = new JComboBox( items.toArray( new String[] {} ) );
  125. combo.setSelectedItem( selected.description() );
  126. combo.addActionListener( new ActionListener() {
  127. @Override
  128. public void actionPerformed( final ActionEvent event ) {
  129. callback.call( combo.getSelectedItem().toString() );
  130. }
  131. } );
  132. content.add( combo, gbc );
  133. }
  134. gbc.gridy++;
  135. }
  136. interface Callback {
  137. public void call( String name );
  138. }
  139. }