欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)攝像頭截圖功能

 更新時(shí)間:2021年04月22日 10:04:10   作者:penngo  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)攝像頭截圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了java攝像頭截圖的具體代碼,供大家參考,具體內(nèi)容如下

本來sun有個(gè)jmf組件可以很方便的實(shí)現(xiàn)攝像頭截圖的,不過這版本后來停止更新了,當(dāng)前官網(wǎng)最新版本為Java Media Framework (JMF) 2.1.1e,下載回來,在windows 7 32位上使用,居然不能運(yùn)行,網(wǎng)上另外找了個(gè)jmf的替代框架fmj使用,截圖實(shí)現(xiàn)代碼:

package com.pengo.capture;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.media.MediaLocator;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import net.sf.fmj.ui.application.CaptureDeviceBrowser;
import net.sf.fmj.ui.application.ContainerPlayer;
import net.sf.fmj.ui.application.PlayerPanelPrefs;

public class CameraFrame extends JFrame{
  private static int num = 0;
  public CameraFrame() throws Exception{
    this.setTitle("攝像頭截圖應(yīng)用");
    this.setSize(480, 500);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final JPanel cameraPanel = new JPanel();
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(cameraPanel, BorderLayout.CENTER);
    ContainerPlayer containerPlayer = new ContainerPlayer(cameraPanel);
    MediaLocator locator = CaptureDeviceBrowser.run(null);  //彈出攝像頭設(shè)備選擇
    
//    MediaLocator locator = null;
//    GlobalCaptureDevicePlugger.addCaptureDevices();
//    Vector vectorDevices = CaptureDeviceManager.getDeviceList(null);
//    if (vectorDevices == null || vectorDevices.size() == 0)
//    {
//      System.out.println("沒有攝像頭===");
//      return;
//    }
//    //選擇第一個(gè)攝像頭設(shè)備
//    for ( int i = 0; i < vectorDevices.size(); i++ ) 
//    {
//      CaptureDeviceInfo infoCaptureDevice = (CaptureDeviceInfo) vectorDevices.get(i);
//      System.out.println("設(shè)備名===============" + infoCaptureDevice.getName());
//      //選擇第一個(gè)設(shè)備為程序使用,如果存在多個(gè)設(shè)備時(shí),則第一個(gè)可能不是攝像頭
//      locator = infoCaptureDevice.getLocator();
//      break;
//    }

    PlayerPanelPrefs prefs = new PlayerPanelPrefs();
    containerPlayer.setMediaLocation(locator.toExternalForm(), prefs.autoPlay);
    
    JPanel btnPanel = new JPanel(new BorderLayout());
    final JTextField path = new JTextField("E:\\camera");
    path.setColumns(30);
    btnPanel.add(path, BorderLayout.WEST);
    JButton okBtn = new JButton("截圖");
    okBtn.addActionListener(new ActionListener(){
       public void actionPerformed(ActionEvent e){
         Dimension imageSize = cameraPanel.getSize();
          BufferedImage image = new BufferedImage(imageSize.width,
              imageSize.height, BufferedImage.TYPE_INT_ARGB);
          Graphics2D g = image.createGraphics();
          cameraPanel.paint(g);
          g.dispose();
          try {
        
            String filePath = path.getText();
            File file = new File(filePath);
            if(file.exists() == false){
              file.mkdirs();
            }
            ImageIO.write(image, "png", new File(file.getAbsolutePath() + "/" + num + ".png"));
            num++;
          } catch (IOException ex) {
            ex.printStackTrace();
            
          }
       }
    });
    btnPanel.add(okBtn, BorderLayout.EAST);
    this.getContentPane().add(btnPanel, BorderLayout.SOUTH);
  }
  
  public static void main(String[] args) throws Exception{
    CameraFrame camera = new CameraFrame();
    camera.setVisible(true);
  }
}

源碼下載:java攝像頭截圖

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論