JMF做USB摄像头刷卡拍照
拍照原理:
第一步:捕获摄像头视频
captureDeviceInfo=CaptureDeviceManager.getDevice("vfw:Microsoft
WDM Image Capture (Win32):0"); //这里放置的是视频驱动
mediaLocator = new MediaLocator("vfw://0"); //这里是视频地址 确定所
需协议和媒体资源的位置
mediaLocator = captureDeviceInfo.getLocator();
DataSource ds = new DataSource(); //数据源
ds.setLocator(mediaLocator); //设置定位器
try {
Manager.setHint(Manager.LIGHTWEIGHT_RENDERER,newBoolean(true)); //set Hint设置提示 LIGHTWEIGHT RENDERER轻量级渲染器会使图像显示效果更好
player=Manager.createRealizedPlayer(mediaLocator); //将本地视频绑定到播放对象中,数据源绑定.
player.start(); //启动,一般分为6个状态
} catch (NoPlayerException e) {
e.printStackTrace();
} catch (CannotRealizeException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
第二步:抓取视频帧(拍照)
public void actionPerformed(ActionEvent e) { //拍照事件
player.start();
System.out.println("走了");
//Frame Grabbing Controlattendance 帧抓取控制
FrameGrabbingControl fgc=(FrameGrabbingControl)player.getControl
("javax.media.control.FrameGrabbingControl");//加载帧抓取控制 jar包
buffer=fgc.grabFrame(); //grabFrameFrame 抢帧
bufferToImage=newBufferToImage((VideoFormat)buffer.getFormat());
//bufferToImage 缓冲区图像 getFormat 获得格式 VideoFormat视频格式
image=bufferToImage.createImage(buffer);
imagePanel.setImage(image);
saveImage(image,"D:/photo/test.jpg"); //设置照片名称和保存图像
player.close();
}
需要思考的问题:
由于USB摄像头是独享的,所以在整个系统中只能加载一次摄像头驱动。
导致的后果:
1.直接后果就是不能实现拍照预览功能!
2.如果首先开启本系统那么其它视频软件就不能运行了,相反如果首先开启其它视频软件那么本系统也不能运行!
(关于USB摄像头是独享性在我的另一片博文有详细介绍,希望看过文章的博友能够给个评论! http://xieyang.blog.51cto.com/2375086/699193)
案例:
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.Graphics;
- import java.awt.GraphicsConfiguration;
- import java.awt.GraphicsDevice;
- import java.awt.GraphicsEnvironment;
- import java.awt.HeadlessException;
- import java.awt.Image;
- import java.awt.Transparency;
- 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.awt.image.ColorModel;
- import java.awt.image.PixelGrabber;
- import java.io.File;
- import java.io.IOException;
- import java.util.Vector;
-
- import javax.imageio.ImageIO;
- import javax.media.Buffer;
- import javax.media.CaptureDeviceInfo;
- import javax.media.CaptureDeviceManager;
- import javax.media.Format;
- import javax.media.MediaLocator;
- import javax.media.NoPlayerException;
- import javax.media.control.FrameGrabbingControl;
- import javax.media.format.RGBFormat;
- import javax.media.format.VideoFormat;
- import javax.media.protocol.DataSource;
- import javax.media.protocol.PushBufferDataSource;
- import javax.media.util.BufferToImage;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JPanel;
-
- import jmapps.jmstudio.CaptureDialog;
- import jmapps.ui.PlayerFrame;
- import jmapps.util.CDSWrapper;
- import jmapps.util.JMFUtils;
-
- public class Demo001 extends PlayerFrame {
-
- public Demo001() {
- super(null, "视频捕获窗口");
- }
-
- DataSource dataSource;
-
- private CaptureDeviceInfo infor;
-
-
-
- private MediaLocator mediaLocator;
-
- String str1 = "vfw:Logitech USB Video Camera:0";
- String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
-
- private String url = "vfw:Microsoft WDM Image Capture (Win32):0";
-
- private Component com;
-
- private JPanel panel;
-
- private int captureCount = 0;
-
- FrameGrabbingControl controlGrabber;
-
- public void play() {
-
- if (mediaPlayerCurrent.getState() != mediaPlayerCurrent.Started) {
- mediaPlayerCurrent.start();
- }
- }
-
- private void init() throws NoPlayerException, IOException {
-
-
- String nameCaptureDeviceAudio = null;
- String nameCaptureDeviceVideo = null;
- CaptureDialog dialogCapture = new CaptureDialog(this, null);
- dialogCapture.show();
- if (dialogCapture.getAction() == CaptureDialog.ACTION_CANCEL)
- return;
- CaptureDeviceInfo cdi = dialogCapture.getAudioDevice();
- if (cdi != null && dialogCapture.isAudioDeviceUsed())
- nameCaptureDeviceAudio = cdi.getName();
- cdi = dialogCapture.getVideoDevice();
- if (cdi != null && dialogCapture.isVideoDeviceUsed())
- nameCaptureDeviceVideo = cdi.getName();
-
- dataSource = JMFUtils.createCaptureDataSource(nameCaptureDeviceAudio,
- dialogCapture.getAudioFormat(), nameCaptureDeviceVideo,
- dialogCapture.getVideoFormat());
- DataSource cdswrapper = new CDSWrapper(
- (PushBufferDataSource) dataSource);
- dataSource = cdswrapper;
- dataSource.connect();
- open(dataSource);
- JPanel controlPanel = new JPanel();
- controlPanel.setName("controlPnael is here");
- add(BorderLayout.SOUTH, controlPanel);
- JButton capture = new JButton("拍照");
-
-
- capture.addActionListener(new ActionListener() {
-
- public void actionPerformed(ActionEvent arg0) {
- mediaPlayerCurrent.stop();
- Buffer bufferFrame;
- BufferToImage bufferToImage;
- Image image;
- BufferedImage bi;
- controlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
- .getControl("javax.media.control.FrameGrabbingControl");
- bufferFrame = controlGrabber.grabFrame();
- bufferToImage = new BufferToImage((VideoFormat) bufferFrame
- .getFormat());
- image = bufferToImage.createImage(bufferFrame);
-
- File out = new File("capture" + (++captureCount) + ".png");
- try {
- bi = toBufferedImage(image);
- ImageIO.write(bi, "png", out);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
-
- mediaPlayerCurrent.start();
-
- }
- });
-
- controlPanel.add(BorderLayout.CENTER, capture);
-
- JButton playStop = new JButton("暂停");
-
-
-
- playStop.addActionListener(new ActionListener() {
-
-
- public void actionPerformed(ActionEvent arg0) {
- mediaPlayerCurrent.stop();
-
- }
- });
- controlPanel.add(BorderLayout.EAST, playStop);
-
- JButton playStart = new JButton("开始");
-
-
- playStart.addActionListener(new ActionListener() {
-
-
- public void actionPerformed(ActionEvent arg0) {
-
- if (mediaPlayerCurrent.getState() != mediaPlayerCurrent.Started) {
- mediaPlayerCurrent.start();
- }
- }
- });
- controlPanel.add(BorderLayout.WEST, playStart);
- addWindowListener(new WindowAdapter() {
-
- public void windowDeiconified(WindowEvent e) {
- mediaPlayerCurrent.stop();
- Buffer bufferFrame;
- BufferToImage bufferToImage;
- Image image;
- BufferedImage bi;
- controlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
- .getControl("javax.media.control.FrameGrabbingControl");
- bufferFrame = controlGrabber.grabFrame();
- bufferToImage = new BufferToImage((VideoFormat) bufferFrame
- .getFormat());
- image = bufferToImage.createImage(bufferFrame);
-
- File out = new File("capture" + (++captureCount) + ".png");
- try {
- bi = toBufferedImage(image);
- ImageIO.write(bi, "png", out);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
-
- mediaPlayerCurrent.start();
- }
- @Override
- public void windowClosing(WindowEvent e) {
- mediaPlayerCurrent.close();
- dataSource.disconnect();
- System.out.println("exit.....");
- System.exit(0);
-
- }
- });
- }
-
-
- public void Photo(){
- mediaPlayerCurrent.stop();
- Buffer bufferFrame;
- BufferToImage bufferToImage;
- Image image;
- BufferedImage bi;
- controlGrabber = (FrameGrabbingControl) mediaPlayerCurrent
- .getControl("javax.media.control.FrameGrabbingControl");
- bufferFrame = controlGrabber.grabFrame();
- bufferToImage = new BufferToImage((VideoFormat) bufferFrame
- .getFormat());
- image = bufferToImage.createImage(bufferFrame);
- File out = new File("capture" + (++captureCount) + ".png");
- try {
- bi = toBufferedImage(image);
- ImageIO.write(bi, "png", out);
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- mediaPlayerCurrent.start();
- }
-
- public static boolean hasAlpha(Image image) {
-
- if (image instanceof BufferedImage) {
- BufferedImage bimage = (BufferedImage) image;
- return bimage.getColorModel().hasAlpha();
- }
-
-
- PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
- try {
- pg.grabPixels();
- } catch (InterruptedException e) {
- }
-
-
- ColorModel cm = pg.getColorModel();
- return cm.hasAlpha();
- }
-
- public static BufferedImage toBufferedImage(Image image) {
- if (image instanceof BufferedImage) {
- return (BufferedImage) image;
- }
-
-
- image = new ImageIcon(image).getImage();
-
-
-
-
- boolean hasAlpha = hasAlpha(image);
-
-
-
- BufferedImage bimage = null;
- GraphicsEnvironment ge = GraphicsEnvironment
- .getLocalGraphicsEnvironment();
- try {
-
- int transparency = Transparency.OPAQUE;
- if (hasAlpha) {
- transparency = Transparency.BITMASK;
- }
-
-
- GraphicsDevice gs = ge.getDefaultScreenDevice();
- GraphicsConfiguration gc = gs.getDefaultConfiguration();
- bimage = gc.createCompatibleImage(image.getWidth(null), image
- .getHeight(null), transparency);
- } catch (HeadlessException e) {
-
- System.err.println("The system does not have a screen!");
- System.exit(-1);
- }
-
- if (bimage == null) {
-
- int type = BufferedImage.TYPE_INT_RGB;
- if (hasAlpha) {
- type = BufferedImage.TYPE_INT_ARGB;
- }
- bimage = new BufferedImage(image.getWidth(null), image
- .getHeight(null), type);
- }
-
-
- Graphics g = bimage.createGraphics();
-
-
- g.drawImage(image, 0, 0, null);
- g.dispose();
-
- return bimage;
- }
-
- private MediaLocator autoDetect() {
- MediaLocator ml = null;
- VideoFormat currentFormat = null;
- Format setFormat = null;
- Format[] videoFormats = null;
- System.out.println(" AutoDetect for VFW");
-
-
- Vector deviceList = CaptureDeviceManager.getDeviceList(null);
- CaptureDeviceInfo device = CaptureDeviceManager.getDevice(url);
- if (deviceList != null) {
-
- for (int i = 0; i < deviceList.size(); i++) {
- try {
- CaptureDeviceInfo di = (CaptureDeviceInfo) deviceList
- .elementAt(i);
-
- if (di.getName().startsWith("vfw:")) {
-
- videoFormats = di.getFormats();
- for (int j = 0; j < videoFormats.length; j++) {
-
- if (videoFormats[j] instanceof RGBFormat) {
- currentFormat = (RGBFormat) videoFormats[i];
- break;
- }
- }
- if (currentFormat == null) {
- System.err.println("Search For RGBFormat Failed");
- System.exit(-1);
- }
-
- ml = di.getLocator();
- }
- } catch (Exception npe) {
- System.err.println("Unable to get Processor for device");
- System.exit(-1);
- }
- }
- } else {
- System.err.println("No Capture Device OK");
- System.exit(-1);
- }
- mediaLocator = ml;
- return ml;
- }
-
- public static void main(String[] args) throws NoPlayerException,
- IOException {
- Demo001 demo = new Demo001();
- demo.setSize(100, 100);
- demo.autoDetect();
- demo.init();
- demo.play();
- demo.setVisible(true);
- }
- }