Thread: Image Screen Shot

Page 1 of 2 12 LastLast
Results 1 to 10 of 16
  1. #1 Image Screen Shot 
    Banned
    Join Date
    Jan 2012
    Posts
    40
    Thanks given
    1
    Thanks received
    5
    Rep Power
    0
    Here is a snippit of the code I wrote for image screen snapper(screen shot) and it automatically saves it. I suggest using eclipse, making a new java project, then making the package snap and then making the class Main and inserting this in it.

    Code:
    /**
     * 
     */
    package snap;
    
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    
    /**
     * @author Soul Truth (Rune-Server.org Member) (Website: DeveloperHub.org)
     *
     */
    public class Main {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception {
    		// TODO Auto-generated method stub
    			Robot robot = new Robot();
    			BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    			ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    			System.out.println("Saved Screenshot.");
    	}
    
    }
    Tell me what you think. I will be adding FTP support shortly.

    Thanks!
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Jan 2012
    Posts
    211
    Thanks given
    143
    Thanks received
    130
    Rep Power
    223
    It's really basic can't really give any feedback, although you'd be better catching the exception and displaying a popup or something so the user knows an error has occurred.
    Reply With Quote  
     

  3. #3  
    Banned
    Join Date
    Jan 2012
    Posts
    40
    Thanks given
    1
    Thanks received
    5
    Rep Power
    0
    Yes, it is not fully finished.
    Reply With Quote  
     

  4. #4  
    Registered Member
    tommo's Avatar
    Join Date
    Aug 2008
    Posts
    420
    Thanks given
    30
    Thanks received
    36
    Rep Power
    200
    Code:
    			BufferedImage screenShot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
    			ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
    it's 2 lines.. there isn't much i can say

    Reply With Quote  
     

  5. #5  
    Registered Member
    Emily's Avatar
    Join Date
    Jul 2010
    Age
    30
    Posts
    622
    Thanks given
    67
    Thanks received
    215
    Rep Power
    271
    I won't comment on the simplicity of this, as that is pretty obvious.

    For naming files created by utilities such as this, you should either store a configuration of the last index used for naming (if naming is like screenshot_01, screenshot_02, ect) or just create a random name every time.

    Code:
    public static String randomName(int length) {
    		String characters = "abcdefghijklmnopqrstuvwxyz";
    		Random random = new Random();
    		char[] text = new char[length];
    		for (int i = 0; i < length; i++) {
    			text[i] = characters.charAt(random.nextInt(characters.length()));
    		}
    		return new String(text);
    	}
    There you go.
    Reply With Quote  
     

  6. #6  
    Banned

    Join Date
    Feb 2012
    Posts
    616
    Thanks given
    300
    Thanks received
    120
    Rep Power
    0
    Im using this;

    Code:
    	 public void takeScreenshot(boolean flag)
        {
            BufferedImage bufferedimage;
            try
            {
                Robot robot = new Robot();
                Point point = getLocationOnScreen();
                Rectangle rectangle = new Rectangle(point.x, point.y, getWidth(), getHeight());
                bufferedimage = robot.createScreenCapture(rectangle);
            }
            catch(Throwable throwable)
            {
                 JOptionPane.showMessageDialog(frame, "An error occured while trying to create a screenshot!", "Screenshot Error", 0);
                return;
            }
            String s = null;
            try
            {
                s = getNearestScreenshotFilename();
            }
            catch(IOException ioexception)
            {
                if(flag)
                {
                     JOptionPane.showMessageDialog(frame, "A screenshot directory does not exist, and could not be created!", "No Screenshot Directory", 0);
                    return;
                }
            }
            if(s == null && flag)
            {
                 JOptionPane.showMessageDialog(frame, "There are too many screenshots in the screenshot directory!\n"+"Delete some screen\n" +"shots and try again." , "Screenshot Directory Full", 0);
                return;
            }
            if(!flag)
            {
                final JFileChooser fileChooser = new JFileChooser();
                final JDialog fileDialog = createFileChooserDialog(fileChooser, "Save Screenshot", this);
                final BufferedImage si = bufferedimage;
                JFileChooser _tmp = fileChooser;
                fileChooser.setFileSelectionMode(0);
                fileChooser.addChoosableFileFilter(new imageFileFilter());
                fileChooser.setCurrentDirectory(new File("C:/.GabbesClient/Scre/"));
                fileChooser.setSelectedFile(new File(s));
                JFileChooser _tmp1 = fileChooser;
                fileChooser.setDialogType(1);
                fileChooser.addActionListener(new ActionListener() {
    
                    public void actionPerformed(ActionEvent actionevent)
                    {
                        String s1 = actionevent.getActionCommand();
                        if(s1.equals("ApproveSelection"))
                        {
                            File file = fileChooser.getSelectedFile();
                            if(file != null && file.isFile())
                            {
                                int i = JOptionPane.showConfirmDialog(frame, (new StringBuilder()).append(file.getAbsolutePath()).append(" already exists.\n"+"Do you want to replace it?").toString(), "Save Screenshot", 2);
                                if(i != 0)
                                {
                                    return;
                                }
                            }
                            try
                            {
                                ImageIO.write(si, "png", file);
                                //client.pushMessage("Screenshot taken.", 3, "@cr2@ Client");
                                //JOptionPane.showMessageDialog(frame,"Screenshot Taken");
                            }
                            catch(IOException ioexception2)
                            {
                                 JOptionPane.showMessageDialog(frame, "An error occured while trying to save the screenshot!\n"+"Please make sure you have\n"+" write access to the screenshot directory." , "Screenshot Error", 0);
                            }
                            fileDialog.dispose();
                        } else
                        if(s1.equals("CancelSelection"))
                        {
                            fileDialog.dispose();
                        }
                    }
    
    
                {
    
                }
                });
                fileDialog.setVisible(true);
            } else
            {
                try
                {
                    ImageIO.write(bufferedimage, "png", new File((new StringBuilder()).append("C:/.GabbesClient/Scre/").append(s).toString()));
    					                             JOptionPane.showMessageDialog(frame, "Image has been saved to C:/.GabbesClient/Scre. You can view your images by pushing File>View Images in the menu dropdowns." , "Screenshot manager", JOptionPane.INFORMATION_MESSAGE);
                    //client.pushMessage("Screenshot taken.", 3, "@cr2@ Client");
                    //JOptionPane.showMessageDialog(frame,"Screenshot taken.");
                }
                catch(IOException ioexception1)
                {
                     JOptionPane.showMessageDialog(frame, "An error occured while trying to save the screenshot!\n"+"Please make sure you have\n"+" write access to the screenshot directory." , "Screenshot Error", 0);
                }
            }
        }
    And;
    Code:
    	public static String getNearestScreenshotFilename()
            throws IOException
        {
            File file = new File("C:/.ImBaws/Screenshots/");
            int i = 0;
            do
            {
                String s = " .png";
                if(i < 10)
                {
                    s = s.replaceFirst(" ", (new StringBuilder()).append("000").append(i).toString());
                } else
                if(i < 100)
                {
                    s = s.replaceFirst(" ", (new StringBuilder()).append("00").append(i).toString());
                } else
                if(i < 1000)
                {
                    s = s.replaceFirst(" ", (new StringBuilder()).append("0").append(i).toString());
                } else
                if(i < 10000)
                {
                    s = s.replaceFirst(" ", (new StringBuilder()).append(" ").append(i).toString());
                }
                if((new File(file, s)).isFile())
                {
                    i++;
                } else
                {
                    return s;
                }
            } while(i < 10000);
            return null;
        }
    
    
    
    class imageFileFilter extends FileFilter
    {
    
        imageFileFilter()
        {
        }
    
        public boolean accept(File file)
        {
            String s = file.getName();
            return file.isDirectory() || s.toLowerCase().endsWith(".png") && s.indexOf("$") == -1;
        }
    
        public String getDescription()
        {
            return "PNG (*.png)";
        }
    }
    There you go.[/QUOTE]
    Reply With Quote  
     

  7. #7  
    Renown Programmer

    Join Date
    Dec 2010
    Posts
    2,876
    Thanks given
    508
    Thanks received
    1,898
    Rep Power
    5000
    or you could just name the file as the current date and time
    never talk to me or my wife's son ever again
    Reply With Quote  
     

  8. #8  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    only problem is that method sucks because it creates a new buffer EVERY time you need to take a screen shot, which is useless for things that need to actively monitor it, but some what useful for taking screenshots that may be separated by a few seconds or more.

    but if anyone here is planning on making a recorder or something, i don't suggest using that and instead figure out what that method uses and just use the raw methods, which take int[] as a parameter.

    but just a reminder, this thing is actually going to allocate 2 buffers for the screen, 1 to get the contents, and then one to store them for the user. just stay away from it.

    Quote Originally Posted by lolhax View Post
    It's really basic can't really give any feedback, although you'd be better catching the exception and displaying a popup or something so the user knows an error has occurred.
    yeah the only reason an exception would occur if there was insufficent java privleges (which can only occur in applets or if the application is beeing run by a 3rd party java launcher that has a restrictions, something like what powerbot used to do, but not like an ide or a JRE..)

    or an exception could occur if the graphicsenvironment is headless, which it should never be i don't know why you'd be launching a screen shot program when there is no way to take one....

    other than that it will never occur....
    Reply With Quote  
     

  9. Thankful user:


  10. #9  
    Registered Member

    Join Date
    Jan 2012
    Posts
    211
    Thanks given
    143
    Thanks received
    130
    Rep Power
    223
    Quote Originally Posted by Killer 99 View Post
    yeah the only reason an exception would occur if there was insufficent java privleges (which can only occur in applets or if the application is beeing run by a 3rd party java launcher that has a restrictions, something like what powerbot used to do, but not like an ide or a JRE..)

    or an exception could occur if the graphicsenvironment is headless, which it should never be i don't know why you'd be launching a screen shot program when there is no way to take one....

    other than that it will never occur....
    It's a generic exception, so it could be an exception from anything for example if it can't access the current folder to write it's file too (not specifically for this code).
    Reply With Quote  
     

  11. #10  
    Registered Member Killer 99's Avatar
    Join Date
    Dec 2007
    Posts
    1,480
    Thanks given
    171
    Thanks received
    503
    Rep Power
    414
    Quote Originally Posted by lolhax View Post
    It's a generic exception, so it could be an exception from anything for example if it can't access the current folder to write it's file too (not specifically for this code).
    i wasn't talking about the writing part, i was speaking about the screen capturing portion
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. PI Server wont start look at screen shot pls
    By bronzekid5000 in forum Help
    Replies: 13
    Last Post: 05-06-2011, 06:48 AM
  2. Asgarnia's Splash Screen [BIG IMAGE BE WARNED]
    By MrHewlett10 in forum Showcase
    Replies: 19
    Last Post: 07-27-2010, 12:40 PM
  3. Hover On Image - Changes Image & Website Link
    By Seven Lives in forum Website Development
    Replies: 3
    Last Post: 03-29-2010, 01:40 AM
  4. Flicking image on title screen
    By BenjaR in forum Help
    Replies: 0
    Last Post: 04-24-2009, 07:09 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •