欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用Java调用摄像头进行拍照源码 JavaSwingSUNF#Microsoft 

程序员文章站 2024-02-16 15:28:16
...
使用Java调用摄像头进行拍照源码,并且有JMF下载地址。
JMF安装文件下载地址:
http://www.blackswansoft.com/songhaikang/files/myfile/Java Media Framework (JMF) API播放.rar

JMF驱动包下载地址:
http://www.blackswansoft.com/songhaikang/files/myfile/jmf.jar

更多优秀的Java案例,请到http://www.blackswansoft.com/去参考。

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.media.Buffer;
import javax.media.CannotRealizeException;
import javax.media.CaptureDeviceInfo;
import javax.media.CaptureDeviceManager;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoPlayerException;
import javax.media.Player;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.util.BufferToImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
//import com.sun.media.protocol.vfw.DataSource;
import com.sun.media.protocol.rtsp.DataSource;


public class FirstCapture extends JPanel implements ActionListener{

    private CaptureDeviceInfo captureDeviceInfo=null;
    private MediaLocator mediaLocator=null;
    private static Player player=null;
    private ImagePanel imagePanel=null;
    private JButton capture;
    private Buffer buffer=null;
    private VideoFormat videoFormat=null;
    private BufferToImage bufferToImage=null;
    private Image image=null;
  
    public FirstCapture()
    {
  
  
        setLayout(new BorderLayout());
        setSize(320,550);

        captureDeviceInfo=CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0"); //这里放置的是视频驱动
  
        mediaLocator=new MediaLocator("vfw://0"); //这里是视频地址
        imagePanel=new ImagePanel();
        capture=new JButton("拍照");
        capture.addActionListener(this);
        DataSource ds = new DataSource();
        ds.setLocator(mediaLocator);
        try {
            player=Manager.createRealizedPlayer(mediaLocator);
            player.start();
            Component comp;
            Component comp1;
            if((comp=player.getVisualComponent())!=null)
                add(comp,BorderLayout.NORTH);
//          if((comp1=player.getControlPanelComponent())!=null)
//              add(comp1,BorderLayout.CENTER);
        } catch (NoPlayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (CannotRealizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        add(imagePanel,BorderLayout.SOUTH);
        add(capture,BorderLayout.CENTER);
    }
    
    @Override
    public void print(Graphics g) {
        // TODO Auto-generated method stub
        super.print(g);
        g.setColor(new Color(255,0,0));
          g.drawLine(0, 0, 100, 100);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        JFrame f = new JFrame("FirstCapture");
        FirstCapture cf = new FirstCapture();
        
        f.addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
          player.close();
          System.exit(0);}});
        
        f.add("Center",cf);
        f.pack();
        f.setSize(new Dimension(320,550));
        f.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        FrameGrabbingControl fgc=(FrameGrabbingControl)player.getControl("javax.media.control.FrameGrabbingControl");
        buffer=fgc.grabFrame();
        bufferToImage=new BufferToImage((VideoFormat)buffer.getFormat());
        image=bufferToImage.createImage(buffer);
        imagePanel.setImage(image);
        saveImage(image,"D:/test.jpg");
    }
    public static void saveImage(Image image,String path)
    {
        BufferedImage bi=new BufferedImage(image.getWidth(null),image.getHeight(null),BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bi.createGraphics();
        g2.drawImage(image, null, null);
        FileOutputStream fos=null;
        try {
                fos=new FileOutputStream(path);
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        JPEGImageEncoder je=JPEGCodec.createJPEGEncoder(fos);
        JPEGEncodeParam jp=je.getDefaultJPEGEncodeParam(bi);
        jp.setQuality(0.5f, false);
        je.setJPEGEncodeParam(jp);
        try {
            je.encode(bi);
            fos.close();
        } catch (ImageFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
    class ImagePanel extends Panel
      {
        public Image myimg = null;
        
        public ImagePanel()
        {
          setLayout(null);
          setSize(320,240);
        }
        
        public void setImage(Image img)
        {
          this.myimg = img;
          repaint();
        }
        
        public void paint(Graphics g)
        {
          if (myimg != null)
          {
            g.drawImage(myimg, 0, 0, this);
          }
          
        }
      }
    

}


推荐软件

用java开发的一款功能强大的开源的远程控制软件,最近又升级了,添加了许多使用的功能,而且代码也有了很大程度的优化。可以实现生成服务器、修改上线主机名称、屏幕监视,屏幕控制、主动连接、上传文件、下载文件、 远程命令、远程任务管理器、发送警告、相互聊天、换皮肤、版本升级提醒等功能,通过该软件可以实现用一台电脑同时控制多台电脑,也可以多台电脑控制同一台电脑等。
软件官方网站下载地址:http://dl.iteye.com/topics/download/c1b7acd2-c7ad-3753-8929-48424025d711

软件如下图所示:

使用Java调用摄像头进行拍照源码
            
    
    
        JavaSwingSUNF#Microsoft