国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Java中利用JMF編寫攝像頭拍照程序

2019-11-17 05:50:53
字體:
來源:轉載
供稿:網友
  我把程序分為兩種,有趣的和無趣的,最近做了幾個有趣的項目,其中一個,應當就算是攝像頭拍照程序了。用于現場拍照,生成照片,主要用到java Media Framework(JMF)。

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

  然后,說一下需求

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

  已經申請將JWebCam建立為一個開源項目,放到GRO,大家發揮自己的想象力加入自己的代碼吧,比如拍攝視頻,添加圖像處理功能,等等。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 蓝山县| 浦东新区| 饶阳县| 军事| 南投市| 汝南县| 维西| 海安县| 常宁市| 卓尼县| 辽宁省| 石狮市| 监利县| 临泉县| 溧阳市| 陵川县| 瑞金市| 资溪县| 和平区| 昭苏县| 新宁县| 沙田区| 汉川市| 拜泉县| 沂水县| 依兰县| 济宁市| 兰溪市| 天等县| 遂溪县| 新邵县| 松溪县| 黄山市| 内黄县| 邢台市| 济南市| 纳雍县| 运城市| 灵寿县| 泰州市|