java調用攝像頭實例,java 攝像頭視頻處理

本文目錄一覽:

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了

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啟用電腦的攝像頭攝相拍照

方法/步驟

1

第一種方法,官方軟體打開。

第一步,檢測攝像頭驅動是否正常安裝。

右擊計算機,點擊管理進入計算機的管理界面,選擇設備管理器,查看裡面的攝像頭驅動是否已經安裝並正常運行。

2

第二步,下載安裝官方軟體。

我們的電腦一般都會安裝好相應的驅動了,只不過還沒有相應的軟體的支持,如果你的電腦是正版系統的話,那麼這些都會帶著,如果不是我們需要自己到官網去下載安裝。以聯想為例(YouCam)。

3

第三步,點擊軟體即可打開筆記本自帶的攝像頭。

安裝完成後後自動生成一個快捷方式,雙擊快捷方式即可打開攝像頭。

4

第二種方法,360魔法攝像頭打開。

如果你的電腦安有360安全衛士的話,那麼就很方便了。打開360安全衛士,進入功能大全界面添加360魔法攝像頭功能即可。

5

點擊安裝後你可以直接點擊打開電腦的攝像頭,也可以在計算機中打開,因為這是你的計算機(我的電腦)中會多出一個攝像頭的功能。

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遠程訪問攝像頭怎麼實現

OpenOffice java api:

簡單的說就是利用java程序可以操作OpenOffice的所有功能,比如創建doc文檔,插入文字,設置文字格式等等。

1. OpenOffice 給程序員提供了一個叫UNO (UniversalNetwork Objects)的組件技術.我理解的UNO: OpenOffice 類似於web程序中的伺服器,程序員寫的代碼類似於客戶端,利用UNO提供的介面和服務去完成對OpenOffice文檔的操作。所以寫程序首先要搭建 UNO環境:

1. 下載 OpenOffice

2.複製UNO提供的jar包: unoil.jar, java_uno.jar, juh.jar, jurt.jar, ridl.jar, unoloader.jar. (ps: 安裝了SDK之後在文件夾找)到自己的工程中,引入它們。

3. 下載文檔:DevelopersGuide.pdf.

4. 安裝了SDK後,重新啟動一下機器,然後就可以按照 DevelopersGuide 來學習 UNO 編程了。

5. 需要ava 環境。

補充: 安裝了SDK後, java, c++幫助文檔,樣常式序,其他關於sdk的信息 都放在本地openOffice安裝路徑一個叫sdk目錄下面,enjoy it !

總結一下已經實現的功能和碰到的問題匯總:

1. 首先要得到遠程office組件的上下文.通過:

com.sun.star.uno.XComponentContext xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();

得到,如果OpenOffice安裝路徑不是在工程的路徑下面(我自己猜的), 就會報:

com.sun.star.comp.helper.BootstrapException: no office executable found!

解決辦法: 黑其源代碼, 看了源代碼就會發現其實OpenOffice是在尋找本地的soffice的shell文件,所以弄個變數來保存soffice在系統中的路徑,重新寫一 個Bootstrap就可以了。詳細請參照:論壇 。

2. 得到 XMultiComponentFactory (ComponentFactory 工廠)

com.sun.star.lang.XMultiComponentFactory xMCF = xContext.getServiceManager();

3. 得到各種組件可以通過下面代碼:

// docType 是 與 soffice 同目錄下面的OpenOffice的其他shell文件,swrite等等

protected XComponent newDocComponent(String docType)

throws java.lang.Exception {

String loadUrl = “private:factory/” + docType;

mxRemoteServiceManager = this.getRemoteServiceManager();

Object desktop = mxRemoteServiceManager.createInstanceWithContext(

“com.sun.star.frame.Desktop”, mxRemoteContext);

XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime

.queryInterface(XComponentLoader.class, desktop);

PropertyValue[] loadProps = new PropertyValue[0];

return xComponentLoader.loadComponentFromURL(loadUrl, “_blank”, 0,

loadProps);

}

4.得到 XTextDocument

XComponent xEmptyWriterComponent = newDocComponent(“swriter”);

XTextDocument mxDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class,

xEmptyWriterComponent);

5. 得到一個文檔的引用

XText mxDocText = mxDoc.getText();

6. 得到文檔的屬性列表

XPropertySet mxDocProps = (XPropertySet) UnoRuntime.queryInterface(

XPropertySet.class, mxDoc);

7. 建立游標,用來插入新的內容。

XTextCursor mxDocCursor = mxDocText.createTextCursor();

XSentenceCursor xSentenceCursor = (XSentenceCursor) UnoRuntime

.queryInterface(XSentenceCursor.class, mxDocCursor);

XWordCursor xWordCursor = (XWordCursor) UnoRuntime.queryInterface(

XWordCursor.class, mxDocCursor);

8.得到游標屬性列表

XPropertySet xCursorProps = (XPropertySet) UnoRuntime .queryInterface(XPropertySet.class, mxDocCursor);

9.設置插入文字格式

xCursorProps.setPropertyValue(“CharFontName”, “宋體”);

xCursorProps.setPropertyValue(“CharWeight”, new Float(FontWeight.BOLD));

xCursorProps.setPropertyValue(“CharHeight”, new Float(10.5));

// 居中顯示

xCursorProps.setPropertyValue(“ParaAdjust”, com.sun.star.style.ParagraphAdjust.CENTER);

10.在該游標處插入信息

mxDocText.insertString(xSentenceCursor, 「Hello World”, true);

11. 保存的關鍵代碼

protected void storeDocComponent(XComponent xDoc, String storeUrl)

throws java.lang.Exception {

XStorable xStorable = (XStorable) UnoRuntime.queryInterface(

XStorable.class, xDoc);

PropertyValue[] storeProps = new PropertyValue[1];

storeProps[0] = new PropertyValue();

storeProps[0].Name = “FilterName”;

storeProps[0].Value = “MS Word 97”;

openOfficeJavaLogger.debug(“… store \”” + xDoc.toString() + “\” to \”” + storeUrl

+ “\”.”);

xStorable.storeAsURL(storeUrl, storeProps);

}

原創文章,作者:LGSI,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/136957.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
LGSI的頭像LGSI
上一篇 2024-10-04 00:17
下一篇 2024-10-04 00:17

相關推薦

  • java client.getacsresponse 編譯報錯解決方法

    java client.getacsresponse 編譯報錯是Java編程過程中常見的錯誤,常見的原因是代碼的語法錯誤、類庫依賴問題和編譯環境的配置問題。下面將從多個方面進行分析…

    編程 2025-04-29
  • Java JsonPath 效率優化指南

    本篇文章將深入探討Java JsonPath的效率問題,並提供一些優化方案。 一、JsonPath 簡介 JsonPath是一個可用於從JSON數據中獲取信息的庫。它提供了一種DS…

    編程 2025-04-29
  • Java騰訊雲音視頻對接

    本文旨在從多個方面詳細闡述Java騰訊雲音視頻對接,提供完整的代碼示例。 一、騰訊雲音視頻介紹 騰訊雲音視頻服務(Cloud Tencent Real-Time Communica…

    編程 2025-04-29
  • Java Bean載入過程

    Java Bean載入過程涉及到類載入器、反射機制和Java虛擬機的執行過程。在本文中,將從這三個方面詳細闡述Java Bean載入的過程。 一、類載入器 類載入器是Java虛擬機…

    編程 2025-04-29
  • Java Milvus SearchParam withoutFields用法介紹

    本文將詳細介紹Java Milvus SearchParam withoutFields的相關知識和用法。 一、什麼是Java Milvus SearchParam without…

    編程 2025-04-29
  • Java 8中某一周的周一

    Java 8是Java語言中的一個版本,於2014年3月18日發布。本文將從多個方面對Java 8中某一周的周一進行詳細的闡述。 一、數組處理 Java 8新特性之一是Stream…

    編程 2025-04-29
  • Java判斷字元串是否存在多個

    本文將從以下幾個方面詳細闡述如何使用Java判斷一個字元串中是否存在多個指定字元: 一、字元串遍歷 字元串是Java編程中非常重要的一種數據類型。要判斷字元串中是否存在多個指定字元…

    編程 2025-04-29
  • Python生成隨機數的應用和實例

    本文將向您介紹如何使用Python生成50個60到100之間的隨機數,並將列舉使用隨機數的幾個實際應用場景。 一、生成隨機數的代碼示例 import random # 生成50個6…

    編程 2025-04-29
  • VSCode為什麼無法運行Java

    解答:VSCode無法運行Java是因為默認情況下,VSCode並沒有集成Java運行環境,需要手動添加Java運行環境或安裝相關插件才能實現Java代碼的編寫、調試和運行。 一、…

    編程 2025-04-29
  • Java任務下發回滾系統的設計與實現

    本文將介紹一個Java任務下發回滾系統的設計與實現。該系統可以用於執行複雜的任務,包括可回滾的任務,及時恢復任務失敗前的狀態。系統使用Java語言進行開發,可以支持多種類型的任務。…

    編程 2025-04-29

發表回復

登錄後才能評論