------------------------------------------------------------------------------------------
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class Main extends JPanel implements Runnable, ActionListener{
JButton btn_capture;
Image img = null;
public Main()
{
this.btn_capture = new JButton("영상캡쳐");
this.btn_capture.addActionListener(this);
this.setLayout(new BorderLayout());
this.add(this.btn_capture, BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e)
{
String cmd = e.getActionCommand();
if(cmd.equals("영상캡쳐"))
{
System.out.println("영상을 캡쳐합니다..");
this.capture();
}
}
private void drawImage(Image img, int x, int y)
{
Graphics g = this.getGraphics();
g.drawImage(img,0,0,x,y,this);
this.paint(g);
this.repaint();
}
public void paint(Graphics g)
{
if(this.img != null)
g.drawImage(this.img, 0, 0, this.img.getWidth(this), this.img.getHeight(this), this);
}
public void capture()
{
Robot robot;
BufferedImage bufImage = null;
try
{
robot = new Robot();
Rectangle area = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
bufImage = robot.createScreenCapture(area);
//Graphics2D g2d = bufImage.createGraphics();
int w = this.getWidth();
int h = this.getHeight();
this.img = bufImage.getScaledInstance(w, h-20, Image.SCALE_DEFAULT);
//this.repaint();
this.drawImage(img, w, h);
//saveJPEGfile("c:\\cap.jpg", bufImage);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static boolean saveJPEGfile(String filename, BufferedImage bi)
{
FileOutputStream out = null;
try
{
out = new FileOutputStream ( filename );
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder ( out );
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam ( bi );
param.setQuality ( 1.0f, false );
encoder.setJPEGEncodeParam ( param );
encoder.encode ( bi );
out.close();
}
catch ( Exception ex )
{
System.out.println ("Error saving JPEG : " + ex.getMessage() );
return false;
}
return true;
}
public void run()
{
while(true)
{
this.setBackground(Color.RED);
try
{
Thread.sleep(1000);
}catch(Exception e){}
this.setBackground(Color.GREEN);
try
{
Thread.sleep(1000);
}catch(Exception e){}
}
}
public static void createFrame()
{
JFrame frame = new JFrame("Jv");
JFrame.setDefaultLookAndFeelDecorated(true);
Container cont = frame.getContentPane();
cont.setLayout(new BorderLayout());
Main mm = new Main();
//new Thread(mm).start();
cont.add(mm, BorderLayout.CENTER);
frame.setSize(400, 400);
frame.setVisible(true);
}
public static void main(String...v)
{
//new Main();
JFrame.setDefaultLookAndFeelDecorated(true);
createFrame();
}
}
------------------------------------------------------------------------------------------------
'Expired > Java Works' 카테고리의 다른 글
PC상의 로컬 파일을 전송하기 위한 간단한 서블릿 예제.. (0) | 2008.01.28 |
---|---|
Java로 EXE 파일 실행..(Excuting exe file by Java Program) (0) | 2007.12.29 |
실시간 블로그 이미지 갱신.. (0) | 2007.12.02 |
[JMF] JPEG Capture by USB Cam (0) | 2007.11.28 |
Eclipse로 JSP/Servlet 개발환경 구축.. (0) | 2007.11.21 |