本文目錄一覽:
java 怎麼調用攝像頭
正好我最近在弄JAVA攝像頭東西
JAVA加載攝像頭需要用JMF框架,這個LZ可以去SUN的主頁下到,具體的配置搜下就有了
我這個是在用JFrame的
加載的代碼是這樣的:
public JPanel contentPane = new JPanel();
public void getvideo(){
CaptureDeviceInfo di = null;
MediaLocator ml = null;
Player player = null;
Vector deviceList = CaptureDeviceManager.getDeviceList(null);
if(deviceList!=null)
{
for(int i=0;ideviceList.size();i++)
{
di=(CaptureDeviceInfo)deviceList.elementAt(i);
if(di.getName().startsWith(“vfw:”)){
ml=di.getLocator();
}
}
}
else{
System.err.print(“No Capture Device”);
System.exit(-1);
}
try {
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if((comp = player.getVisualComponent())!=null)
{ comp.setBounds(new Rectangle(0, 40,320, 240));
contentPane.add(comp,BorderLayout.NORTH);
}
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
再把contentPane添加到JFrame裏面,這個就可以在一個JFrame的框架用攝像頭了
還有,要注意個事,就是第一次使用攝像頭的話,要用JMF帶的程序JMF Registry選到capture Devices中註冊下,不然是找不到驅動的
如何使用java啟用電腦的攝像頭攝相拍照
一般的USB攝像頭,使用JMF可以讀取相片、視頻片段~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
JAVA裏面有什麼方法調用攝像頭拍照的
利用jmf,代碼很簡單:
//利用這三個類分別獲取攝像頭驅動,和獲取攝像頭內的圖像流,獲取到的圖像流是一個swing的component組件類
public static player player = null;
private capturedeviceinfo di = null;
private medialocator ml = null;
//文檔中提供的驅動寫法,為何這麼寫我也不知:)
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();
component comp;
if ((comp = player.getvisualcomponent()) != null)
{
add(comp, borderlayout.north);
}
}
catch (exception e)
{
e.printstacktrace();
}
接下來就是點擊拍照,獲取攝像頭內的當前圖像。
代碼也是很簡單:
private jbutton capture;
private buffer buf = null;
private buffertoimage btoi = null;
private imagepanel imgpanel = null;
private image img = null;
private imagepanel imgpanel = null;
jcomponent c = (jcomponent) e.getsource();
if (c == capture)//如果按下的是拍照按鈕
{
framegrabbingcontrol fgc =(framegrabbingcontrol) player.getcontrol(“javax.media.control.framegrabbingcontrol”);
buf = fgc.grabframe(); // 獲取當前禎並存入buffer類
btoi = new buffertoimage((videoformat) buf.getformat());
img = btoi.createimage(buf); // show the image
imgpanel.setimage(img);
}
保存圖像的就不多說了,以下為示例代碼
bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight);
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(1f, false);//不壓縮圖像
encoder.setjpegencodeparam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.ioexception io)
{
system.out.println(“ioexception”);
}
如何使用java調用攝像頭
正好我最近在弄JAVA攝像頭東西
JAVA加載攝像頭需要用JMF框架,這個LZ可以去SUN的主頁下到,具體的配置搜下就有了
我這個是在用JFrame的
加載的代碼是這樣的:
public JPanel contentPane = new JPanel();
public void getvideo(){
CaptureDeviceInfo di = null;
MediaLocator ml = null;
Player player = null;
Vector deviceList = CaptureDeviceManager.getDeviceList(null);
if(deviceList!=null)
{
for(int i=0;ideviceList.size();i++)
{
di=(CaptureDeviceInfo)deviceList.elementAt(i);
if(di.getName().startsWith(“vfw:”)){
ml=di.getLocator();
}
}
}
else{
System.err.print(“No Capture Device”);
System.exit(-1);
}
try {
player = Manager.createRealizedPlayer(ml);
player.start();
Component comp;
if((comp = player.getVisualComponent())!=null)
{ comp.setBounds(new Rectangle(0, 40,320, 240));
contentPane.add(comp,BorderLayout.NORTH);
}
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
再把contentPane添加到JFrame裏面,這個就可以在一個JFrame的框架用攝像頭了
還有,要注意個事,就是第一次使用攝像頭的話,要用JMF帶的程序JMF Registry選到capture Devices中註冊下,不然是找不到驅動的
JAVA代碼如何調用客戶端攝像頭
首先到sun下載最新的jmf,然後安裝。
然後,說一下需求
1. 用攝像頭拍照
2. 在文本框輸入文件名
3. 按下拍照按鈕,獲取攝像頭內的圖像
4. 在拍下的照片上有一紅框截取固定大小的照片。
5. 保存為本地圖像為jpg格式,不得壓縮畫質
技術關鍵,相信也是大家最感興趣的部分也就是如何讓一個攝像頭工作,並拍下一張照片了。
利用jmf,代碼很簡單:
//利用這三個類分別獲取攝像頭驅動,和獲取攝像頭內的圖像流,獲取到的圖像流是一個swing的component組件類
public static player player = null;
private capturedeviceinfo di = null;
private medialocator ml = null;
//文檔中提供的驅動寫法,為何這麼寫我也不知:)
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();
component comp;
if ((comp = player.getvisualcomponent()) != null)
{
add(comp, borderlayout.north);
}
}
catch (exception e)
{
e.printstacktrace();
}
接下來就是點擊拍照,獲取攝像頭內的當前圖像。
代碼也是很簡單:
private jbutton capture;
private buffer buf = null;
private buffertoimage btoi = null;
private imagepanel imgpanel = null;
private image img = null;
private imagepanel imgpanel = null;
jcomponent c = (jcomponent) e.getsource();
if (c == capture)//如果按下的是拍照按鈕
{
framegrabbingcontrol fgc =(framegrabbingcontrol) player.getcontrol( “javax.media.control.framegrabbingcontrol “);
buf = fgc.grabframe(); // 獲取當前禎並存入buffer類
btoi = new buffertoimage((videoformat) buf.getformat());
img = btoi.createimage(buf); // show the image
imgpanel.setimage(img);
}
保存圖像的就不多說了,以下為示例代碼
bufferedimage bi = (bufferedimage) createimage(imgwidth, imgheight);
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(1f, false);//不壓縮圖像
encoder.setjpegencodeparam(param);
try
{
encoder.encode(bi);
out.close();
}
catch (java.io.ioexception io)
{
system.out.println( “ioexception “);
}
把.jar文件導入。下載了jmf後需要安裝,安裝後你的那個jmf目錄下就會有一個lib文件夾裏面有.jar文件,然後打開eclipse,右鍵選擇你的工程-〉屬性-〉java build path- library-〉add external jars 找到你的jmf目錄下lib的那個文件夾然後選中那些文件導入就ok了。
然後利用工具提供的導入文件幫助,一個一個導就OK了
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/309334.html