Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Capture to jpg  (Read 1394 times)
0 Members and 1 Guest are viewing this topic.
Offline kohaar

Senior Newbie




Java games rock!


« Posted 2005-06-06 20:56:48 »

Hi. I need to add a function where the user can press a key and take a screen dump, saved as an image (e.g. jpg). Is this possible and how?
Offline woogley
« Reply #1 - Posted 2005-06-06 21:31:55 »

I dont know if Xith3D has a class specifically for this but you can do this via the java.awt.Robot and javax.imageio.ImageIO class.

Example:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
import javax.imageio.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Example {
   protected static void capture() {
      GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
      Robot r = null;
      try {
         r = new Robot(gd);
      }
      catch (AWTException e) {
         e.printStackTrace();
         return;
      }
      DisplayMode dm = gd.getDisplayMode();
      BufferedImage b = r.createScreenCapture(new Rectangle(0,0,dm.getWidth(),dm.getHeight()));
      try {
         ImageIO.write(b,"jpg",new FileOutputStream("screen.jpg"));
      }
      catch (IOException e) {
         e.printStackTrace();
      }
   }
   public static void main(String args[]) {
      capture();
   }
}


Unfortunately ImageIO doesn't seem to save JPEGs with the highest quality. However, if you write PNGs (which I dont think is fully supported prior to 1.5) the image will be lossless.
Offline woogley
« Reply #2 - Posted 2005-06-06 21:42:39 »

one thing, the previous code I posted takes a screenshot of the entire screen (so if your app isn't full-screen, you'll be capturing the other parts of the screen that have nothing to do with your app). You can use this modified version of capture() to capture just a particular Window:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
protected void capture(Window w) {
   GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
   Robot r = null;
   try {
      r = new Robot(gd);
   }
   catch (AWTException e) {
      e.printStackTrace();
      return;
   }
   DisplayMode dm = gd.getDisplayMode();
   BufferedImage b = r.createScreenCapture(w.getBounds());
   try {
      ImageIO.write(b,"jpg",new FileOutputStream("screen.jpg"));
   }
   catch (IOException e) {
      e.printStackTrace();
   }
}
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #3 - Posted 2005-06-07 06:25:41 »

Xith3D does provide this and it is really easy  Cool

1  
2  
3  
4  
5  
6  
7  
View v;
Canvas3D canvas;

// ...

String filename = "myfilename.png";
view.getSnapshot(canvas, filename);



Some nifty formatting for the filename
1  
2  
String filename = System.getProperty("home.dir") + System.getProperty("file.separator") + "MYPROJECT" + 
     (new SimpleDateFormat("_yyyy-MM-dd-HHmmss")).format(new GregorianCalendar().getTime()) + ".png";


That will create the shot in the user's home dir (as opposed to the working dir, of course this is up to you) named:  "MYPROJECT_2005-06-06-163101.png".

I think I will add this question to the FAQ.

Cheers,

Will.

Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #4 - Posted 2005-06-07 06:33:48 »

I dont know if Xith3D has a class specifically for this but you can do this via the java.awt.Robot and javax.imageio.ImageIO class.

Unfortunately ImageIO doesn't seem to save JPEGs with the highest quality. However, if you write PNGs (which I dont think is fully supported prior to 1.5) the image will be lossless.

For your information, this is actually possible.

This is the code I use in my photo album processing program Gallery Mage:


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
   /**
    * Writes the given image data to a JPEG image file, with the given quality
    * @param toWrite the image to write
    * @param fileout name of the file
    * @param quality JPEG quality percentage (0 == worst, 1 == best)
    *
    * @throws IOException if there was an error writing the image
    */

   public static void writeJpegImage(BufferedImage toWrite, String fileout, float quality) throws IOException {
       
      ImageOutputStream ios = ImageIO.createImageOutputStream(new File(fileout));
     
      ImageWriter writer = null;
      Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
      if (iter.hasNext()) {
          writer = (ImageWriter)iter.next();
      }
     
      writer.setOutput(ios);
      ImageWriteParam iwparam = writer.getDefaultWriteParam();
      iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT) ;
      //System.out.println(iwparam.getCompressionQuality()) ;
      iwparam.setCompressionQuality(quality) ;
      writer.write(null, new IIOImage(toWrite, null, null), iwparam);
      ios.flush();
      writer.dispose();
      ios.close();
   }


You can also find that solution by looking up "ugly" in your dictionary Wink

Cheers,

Will.

Offline woogley
« Reply #5 - Posted 2005-06-07 12:03:58 »

aha! I was thinking ImageIO SHOULD be able to set quality, just didn't know where it was

thanks for pointing this out Wink
Offline kohaar

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-06-07 18:55:23 »

Thanks for the quick reply. You are the greatest!

Btw. I got an 11. for a project involving xith3d and jmf (3d, video and motion detection). I owe much of this to you people who made xith so intuitive!
Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #7 - Posted 2005-06-07 23:58:58 »

aha! I was thinking ImageIO SHOULD be able to set quality, just didn't know where it was

thanks for pointing this out Wink

No worries.  I really wish there was a cleaner solution though.  Something that doesn't require one to write a utility method.

Will.

Offline William Denniss

JGO Coder


Projects: 2


Fire at will


« Reply #8 - Posted 2005-06-08 00:01:53 »

Thanks for the quick reply. You are the greatest!

Btw. I got an 11. for a project involving xith3d and jmf (3d, video and motion detection). I owe much of this to you people who made xith so intuitive!

You're welcome.  Well done on your project (I assume 11 is a good score Smiley), please share the details, it's always interesting to learn how people are using Xith3D.

Cheers,

Will.

Offline kohaar

Senior Newbie




Java games rock!


« Reply #9 - Posted 2005-06-08 13:35:49 »

Hi. 11 is a great score. It’s equal to an A :-) Still can't believe it.

The source code is a mess because I only had two months to create it program, but I’ll upload the part of the report that describes the program in a day or so.

A summery: A webcam gets an image and detects red colour (the user of the program will wear a red light). This is mapped to a grid and this grid is used to control the view. If the views position matches a given variable (vector), the render is stopped and a new frame is created in which an mpeg2 movie is played. The motion detection is then used to move forward and backwards in the movie and when the movie ends, the frame is closed and the render is started again. It’s supposed to be an interactive movie. There were some bugs in the end because caused by JMF. For some reason it has problems moving in a movie precisely enough for my needs. Something about the setMediaTime() doesn’t really move to the precise timeframe. 
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline kohaar

Senior Newbie




Java games rock!


« Reply #10 - Posted 2005-06-09 19:55:24 »

http://www.harme.dk/engine/inside.pdf

For anyone interested, a small part of the report concerning the program
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (29 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (40 views)
2014-12-09 22:41:13

BurntPizza (75 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (50 views)
2014-12-03 16:27:13

CopyableCougar4 (47 views)
2014-11-29 21:32:03

toopeicgaming1999 (113 views)
2014-11-26 15:22:04

toopeicgaming1999 (100 views)
2014-11-26 15:20:36

toopeicgaming1999 (30 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!