출처: http://jakarta.tistory.com/category/Java%20Works
==================================================================================================
==================================================================================================
package src;
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();
}
}
==================================================================================================
'IT_Programming > Java' 카테고리의 다른 글
[펌] HTML내 본문 추출 구현.. (0) | 2009.04.12 |
---|---|
[펌] Java JMF _ USB Cam을 이용한 JPEG 캡쳐 (0) | 2009.04.12 |
[펌] 자바 에러 모음 (0) | 2009.04.12 |
JTable과 DB 연동 예제 (SQL Server 2005 Express Edition) (0) | 2009.04.08 |
[펌_ jdbc] ResultSet 크기 구하기 (0) | 2009.04.08 |