자바(Java) JMF를 이용한 USB Cam을 이용한 JPEG 캡쳐
--------------------------------------------------------------------------------------------------
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.media.*;
import javax.media.format.*;
import javax.media.util.*;
import javax.media.control.*;
import javax.media.protocol.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
public class JPEGCapture {
public static Player player = null;
public CaptureDeviceInfo di = null;
public MediaLocator ml = null;
public Buffer buf = null;
public Image img = null;
public VideoFormat vf = null;
public BufferToImage btoi = null;
public JPEGCapture() {
this.initDevice();
}
public void initDevice() {
String str1 = "vfw:Logitech USB Video Camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
di = CaptureDeviceManager.getDevice(str2);
ml = di.getLocator();
try
{
player = Manager.createRealizedPlayer(ml);
player.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
public static void playerClose() {
player.close();
player.deallocate();
}
public void Capture(String absPath) {
FrameGrabbingControl fgc = (FrameGrabbingControl) player
.getControl("javax.media.control.FrameGrabbingControl");
buf = fgc.grabFrame();
// Convert it to an image
btoi = new BufferToImage((VideoFormat) buf.getFormat());
img = btoi.createImage(buf);
// save image
saveJPG(img, absPath);
}
public static void saveJPG(Image img, String s) {
BufferedImage bi = new BufferedImage(img.getWidth(null), img
.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bi.createGraphics();
g2.drawImage(img, null, null);
FileOutputStream out = null;
try {
out = new FileOutputStream(s);
} catch (java.io.FileNotFoundException io) {
System.out.println("File Not Found");
}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
param.setQuality(0.5f, false);
encoder.setJPEGEncodeParam(param);
try {
encoder.encode(bi);
out.close();
} catch (java.io.IOException io) {
System.out.println("IOException");
}
}
}
---------------------------------------------------------------------------------------------------
JPEGCapture cap = new JPEGCapture();
cap.Capture("C:\\test.jpg");
위와같이 Capture(String) 메소드를 이용하여 캡쳐파일 생성..
'Expired > Java Works' 카테고리의 다른 글
PC상의 로컬 파일을 전송하기 위한 간단한 서블릿 예제.. (0) | 2008.01.28 |
---|---|
Java로 EXE 파일 실행..(Excuting exe file by Java Program) (0) | 2007.12.29 |
실시간 블로그 이미지 갱신.. (0) | 2007.12.02 |
Eclipse로 JSP/Servlet 개발환경 구축.. (0) | 2007.11.21 |
자바로 바탕화면 캡쳐하기(Capturing window screen by Java program) (0) | 2007.11.19 |