JMF를 이용한 웹캠 사진촬영 후 JPEG 파일로 저장하는 소스
에드가
노트북에서 run.java를 실행시켰을때 노트북 웸켐의 드라이버를 잡아서 화면을 캡쳐해 jpeg로 저장하는 프로그램을 만들려합니다.
jpegcapture.java 소스입니다.
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;
protected format[] videoformats = null;
public formatcontrol formatcontrol = null;
public jpegcapture() {
this.initdevice();
}
public void initdevice() {
//string str1 = vfw:logitech usb video camera:0;
string str = vfw:microsoft wdm image capture (win32):0;
di = capturedevicemanager.getdevice(str);
ml = di.getlocator();
try{
player = manager.createrealizedplayer(ml);
player.start();
formatcontrol = (formatcontrol)player.getcontrol ( javax.media.control.formatcontrol );
videoformats = di.getformats();
}catch(exception e){
e.printstacktrace();
}
}
public static void playerclose() {
player.stop();
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);
// 제가 볼때 이 부분에서 img 변수에 화상정보를 못 담는것 같은데... 그래서 저 밑에 버퍼이미지를 생성할때 제대로 담지 못하는 것 같습니다.
제 추측이 맞는지요..
}
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.1f, false);
encoder.setjpegencodeparam(param);
try {
encoder.encode(bi);
out.close();
//}catch(java.io.ioexception io){
//system.out.println(ioexception);
//}
}catch(exception e){
e.printstacktrace();
}
}
}play.java 소스입니다.
public class play extends jpegcapture {
public static void main(string[] args){
jpegcapture cap = new jpegcapture();
cap.capture(c:\\test.jpg);
cap.playerclose();
}
}
이클립스에서 run.java를 실행시켰을때 나오는 오류메시지 입니다.
exception in thread main java.lang.nullpointerexception
at joocam.jpegcapture.savejpg(jpegcapture.java:69)
at joocam.jpegcapture.capture(jpegcapture.java:66)
at joocam.play.main(play.java:6)
다음은 jpegcapture.java 만 돌려 보았을때 나오는 오류메시지 입니다.
jpegcapture.java:65: warning: com.sun.image.codec.jpeg.jpegimageencoder is sun proprietary api and may be removed in a future release
jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
^
jpegcapture.java:65: warning: com.sun.image.codec.jpeg.jpegcodec is sun proprietary api and may be removed in a future release
jpnbsp; jpegimageencoder encoder = jpegcodec.createjpegencoder(out);
^
jpegcapture.java:66: warning: com.sun.image.codec.jpeg.jpegencodeparam is sun proprietary api and may be removed in a future release
jpegencodeparam param = encoder.getdefaultjpegencodeparam(bi);
^
3 warnings
제가 볼때 savejpeg 함수에서 img 변수에 화상정보를 못 담는것 같은데... 그래서 저 밑에 버퍼이미지를 생성할때 제대로 담지 못하는 것 같습니다.
제 추측이 맞는지요..
맞다면.. 제대로 구동할수 있게 소스 수정 도와주시면 감사하겠습니다... ㅠ ㅠ