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
  • 阅读 ( 849 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

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 问答工具