首页 > 编程知识 正文

java调用摄像头实例,java 摄像头视频处理

时间:2023-12-24 12:11:52 阅读:321694 作者:JFIZ

本文目录一览:

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);

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。