人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當(dāng)前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細(xì)頁面

Java中運(yùn)用JMF編寫攝像頭拍照程序

Java中運(yùn)用JMF編寫攝像頭拍照程序

更新時(shí)間:2022-08-06 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):


我把程序分為兩種,有趣的和無趣的,最近做了幾個(gè)有趣的項(xiàng)目,其中一個(gè),應(yīng)當(dāng)就算是攝像頭拍照程序了。用于現(xiàn)場拍照,生成照片,主要用到Java Media Framework(JMF)。

首先到SUN下載最新的JMF,然后安裝。http://java.sun.com/products/java-media/jmf/index.jsp

然后,說一下需求

1. 用攝像頭拍照

2. 在文本框輸入文件名

3. 按下拍照按鈕,獲取攝像頭內(nèi)的圖像

4. 在拍下的照片上有一紅框截取固定大小的照片。

5. 保存為本地圖像為jpg格式,不得壓縮畫質(zhì)

技術(shù)關(guān)鍵,相信也是大家最感興趣的部分也就是如何讓一個(gè)攝像頭工作,并拍下一張照片了。

利用JMF,代碼很簡單:

//利用這三個(gè)類分別獲取攝像頭驅(qū)動(dòng),和獲取攝像頭內(nèi)的圖像流,獲取到的圖像流是一個(gè)Swing的Component組件類

public static Player player = null;
private CaptureDeviceInfo di = null;
private MediaLocator ml = null;

//文檔中提供的驅(qū)動(dòng)寫法,為何這么寫我也不知:)

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();
}
接下來就是點(diǎn)擊拍照,獲取攝像頭內(nèi)的當(dāng)前圖像。

代碼也是很簡單:

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(); // 獲取當(dāng)前禎并存入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");
}
已經(jīng)申請將JWebCam建立為一個(gè)開源項(xiàng)目,放到GRO,大家發(fā)揮自己的想象力加入自己的代碼吧,比如拍攝視頻,添加圖像處理功能,等等。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 博湖县| 利津县| 墨竹工卡县| 青铜峡市| 鄂托克前旗| 漠河县| 德化县| 郯城县| 石棉县| 雅安市| 乌拉特后旗| 开鲁县| 汶川县| 沙坪坝区| 余江县| 铁岭县| 亚东县| 兴安县| 辉县市| 白城市| 阜新| 化德县| 冷水江市| 德兴市| 巴彦淖尔市| 崇义县| 云安县| 甘泉县| 吉安县| 西昌市| 日喀则市| 嘉义市| 五台县| 高唐县| 新余市| 靖宇县| 方山县| 葫芦岛市| 泰和县| 朝阳区| 丰城市|