dcsimg
How to save an applet as gif or jpg.
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Kiran_Kumar
Posted On:   Monday, February 25, 2002 07:29 PM

I have an applet in a JSP page. I was also able to save the applet by using the Robot class in jdk1.3, Now the problem is how to do it in jdk1.2, which doesn't support the Robot class. Given below is the code that I used for saving the applet in the JSP. Can anyone help me out with this. Also I'm not supposed to change the applet code. Actually I'm able to get the screen coordinates and width & height, and when passed to the Robot class would get me the image, but how do I do it using jdk1.2 This is the code that I used. import java.awt.*; import java.awt.image.*; import java.awt.FileDialog.*; import com.sun.image.codec.jpeg.*; import java.io.*;    More>>

I have an applet in a JSP page. I was also able to save the applet by using the Robot class in jdk1.3, Now the problem is how to do it in jdk1.2, which doesn't support the Robot class. Given below is the code that I used for saving the applet in the JSP. Can anyone help me out with this. Also I'm not supposed to change the applet code.
Actually I'm able to get the screen coordinates and width & height, and when passed to the Robot class would get me the image, but how do I do it using jdk1.2

This is the code that I used.

			

import java.awt.*;

import java.awt.image.*;

import java.awt.FileDialog.*;

import com.sun.image.codec.jpeg.*;

import java.io.*;



public class TestScreen {

public void getScreenImage(String loc/*, int xCoord, int yCoord*/) {

Robot robot;

try {

robot = new Robot();

}

catch(Exception e) {

throw new RuntimeException("unable to instantiate Robot");

}

/* xCoord+=10;

yCoord+=15;

Image screen = robot.createScreenCapture(new Rectangle(xCoord, yCoord, 500, 350));

*/ Image screen = robot.createScreenCapture(new Rectangle(266, 37, 500, 350));



try {


FileOutputStream fileStream = new FileOutputStream(loc);

BufferedReader in = new BufferedReader(new FileReader(loc));

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(loc)));



JPEGEncodeParam encodeParam = JPEGCodec.getDefaultJPEGEncodeParam((BufferedImage)screen);


JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(fileStream);

encoder.encode((BufferedImage)screen,encodeParam);



FileInputStream fis=new FileInputStream(loc);

byte b[]=new byte[fis.available()];



DataInputStream dis=new DataInputStream(fis);

dis.readFully(b);

PrintStream ps=new PrintStream(new FileOutputStream(loc),true);

ps.write(b,0,b.length);

}

catch(Exception ex2) {


ex2.printStackTrace();

}

}



public void fileSave(/*String position*/) {

FileDialog fd = new FileDialog(new Frame(),"Save File",FileDialog.SAVE);

fd.show();

String fileLocation = fd.getDirectory()+fd.getFile();

String fileName = fd.getFile();



if(!(fd.getDirectory()== null || fd.getFile()== null)) {



/* int count = position.indexOf(42);

int xPos = Integer.parseInt(position.substring(0, count));

int yPos = Integer.parseInt(position.substring(count+1));

*/

getScreenImage(fileLocation/*, xPos, yPos*/);

}

}

public static void main(String [] args) {

TestScreen ts = new TestScreen();

ts.fileSave();

}

}
   <<Less
About | Sitemap | Contact