• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    java使用jmf媒體框架調用攝像頭在窗口上顯示

             前次經驗安裝了jmf媒體框架,怎么利用媒體框架,下面分享一下用java挪用攝像頭在窗口上顯示。你沒有安裝jmf,看看經驗鏈接,我寫的經驗對你進修java有幫忙的話,給我投票或者點贊!

    http://v.youku.com/v_show/id_XMzk1NjU2NDcyMA==.html

    0安裝媒體框架

    0轉換當作

    0線框圖怎么畫

    東西/原料

    • netbeans ide 8.1
    • jmf2-1-1

    方式/步調

    1. 1

      新建一個java項目,項目標名字為javasxt.。

    2. 2

      新建一個窗口(jframe),,窗口的類名Javasxt。

    3. 3

      打開這個javasxt.java這個文件,在窗口中增添一個面板jpanel,這個面板是用來顯示攝像頭的畫面。

    4. 4

      我的攝像頭是640*480的分辯率,所以把增添的面板的尺寸改當作寬度640,高度480,把窗口顯示位置設置為屏幕中心顯示。

    5. 5

      把面板增添一個邊框。

    6. 6

      把媒體框架(jmf)的庫函數包含在項目中。

    7. 7

      在初始化函數中增添代碼:

      引用攝像頭

       CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");

    8. 8

      建立一個數據源:

      MediaLocator med=new MediaLocator("vfw://0");

    9. 9

      新建一個播放對象:

      player=Manager.createRealizedPlayer(med);

    10. 10

      把播放 對象的顯示畫面的部件增添到用戶新建的面板中:

      if(player.getVisualComponent()!=null){

                      jPanel1.add(player.getVisualComponent());

                      player.start();

                  }

    11. 11

      運行項目窗口顯示攝像頭拍攝的畫面。

    12. 12

      源碼:

      import java.io.IOException;

      import java.util.logging.Level;

      import java.util.logging.Logger;

      import javax.media.CannotRealizeException;

      import javax.media.Manager;

      import javax.media.MediaLocator;

      import javax.media.NoPlayerException;

      import javax.media.Player;

      import javax.media.cdm.CaptureDeviceManager;

      /*

       * To change this license header, choose License Headers in Project Properties.

       * To change this template file, choose Tools | Templates

       * and open the template in the editor.

       */

      /**

       *

       * @author Administrator

       */

      public class Javasxt extends javax.swing.JFrame {

          private Player player;

          /**

           * Creates new form Javasxt

           */

          public Javasxt() {

              try {

                  initComponents();

                  CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");

                  MediaLocator med=new MediaLocator("vfw://0");

                  player=Manager.createRealizedPlayer(med);

                  if(player.getVisualComponent()!=null){

                      jPanel1.add(player.getVisualComponent());

                      player.start();

                  }

              } catch (IOException | NoPlayerException | CannotRealizeException ex) {

                  Logger.getLogger(Javasxt.class.getName()).log(Level.SEVERE, null, ex);

              }

          }

          /**

           * This method is called from within the constructor to initialize the form.

           * WARNING: Do NOT modify this code. The content of this method is always

           * regenerated by the Form Editor.

           */

          @SuppressWarnings("unchecked")

          // <editor-fold defaultstate="collapsed" desc="Generated Code">                          

          private void initComponents() {

              jFrame1 = new javax.swing.JFrame();

              jPanel2 = new javax.swing.JPanel();

              jPanel1 = new javax.swing.JPanel();

              jFrame1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

              jPanel2.setMinimumSize(new java.awt.Dimension(640, 480));

              jPanel2.setName(""); // NOI18N

              javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);

              jPanel2.setLayout(jPanel2Layout);

              jPanel2Layout.setHorizontalGroup(

                  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGap(0, 640, Short.MAX_VALUE)

              );

              jPanel2Layout.setVerticalGroup(

                  jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGap(0, 480, Short.MAX_VALUE)

              );

              javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());

              jFrame1.getContentPane().setLayout(jFrame1Layout);

              jFrame1Layout.setHorizontalGroup(

                  jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGroup(jFrame1Layout.createSequentialGroup()

                      .addContainerGap()

                      .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

              );

              jFrame1Layout.setVerticalGroup(

                  jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGroup(jFrame1Layout.createSequentialGroup()

                      .addContainerGap()

                      .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

              );

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

              jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

              jPanel1.setMinimumSize(new java.awt.Dimension(640, 480));

              jPanel1.setName(""); // NOI18N

              jPanel1.setPreferredSize(new java.awt.Dimension(640, 480));

              javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);

              jPanel1.setLayout(jPanel1Layout);

              jPanel1Layout.setHorizontalGroup(

                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGap(0, 636, Short.MAX_VALUE)

              );

              jPanel1Layout.setVerticalGroup(

                  jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGap(0, 476, Short.MAX_VALUE)

              );

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

              getContentPane().setLayout(layout);

              layout.setHorizontalGroup(

                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGroup(layout.createSequentialGroup()

                      .addContainerGap()

                      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

              );

              layout.setVerticalGroup(

                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

                  .addGroup(layout.createSequentialGroup()

                      .addContainerGap()

                      .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

                      .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

              );

              pack();

              setLocationRelativeTo(null);

          }// </editor-fold>                        

          /**

           * @param args the command line arguments

           */

          public static void main(String args[]) {

              /* Set the Nimbus look and feel */

              //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

              /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

               * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 

               */

              try {

                  for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {

                      if ("Nimbus".equals(info.getName())) {

                          javax.swing.UIManager.setLookAndFeel(info.getClassName());

                          break;

                      }

                  }

              } catch (ClassNotFoundException ex) {

                  java.util.logging.Logger.getLogger(Javasxt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

              } catch (InstantiationException ex) {

                  java.util.logging.Logger.getLogger(Javasxt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

              } catch (IllegalAccessException ex) {

                  java.util.logging.Logger.getLogger(Javasxt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

              } catch (javax.swing.UnsupportedLookAndFeelException ex) {

                  java.util.logging.Logger.getLogger(Javasxt.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

              }

              //</editor-fold>

              /* Create and display the form */

              java.awt.EventQueue.invokeLater(new Runnable() {

                  public void run() {

                      new Javasxt().setVisible(true);

                  }

              });

          }

          // Variables declaration - do not modify                     

          private javax.swing.JFrame jFrame1;

          private javax.swing.JPanel jPanel1;

          private javax.swing.JPanel jPanel2;

          // End of variables declaration                   

      }

    • 發表于 2018-12-09 00:00
    • 閱讀 ( 858 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    0 條評論

    請先 登錄 后評論
    admin
    admin

    0 篇文章

    作家榜 ?

    1. xiaonan123 189 文章
    2. 湯依妹兒 97 文章
    3. luogf229 46 文章
    4. jy02406749 45 文章
    5. 小凡 34 文章
    6. Daisy萌 32 文章
    7. 我的QQ3117863681 24 文章
    8. 華志健 23 文章

    聯系我們:uytrv@hotmail.com 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆