java 显示摄像头

package com.aast.view;

import java.awt.AWTEvent;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;

public class Authentication{
	private JFrame jFrame;
	public MainPanel mainPanel;
	private int width=1024,height=768;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					new Authentication();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	public Authentication() {
		Toolkit tk = Toolkit.getDefaultToolkit();  
		tk.addAWTEventListener(new ImplAWTEventListener(), AWTEvent.KEY_EVENT_MASK);  
		jFrame=new JFrame();
		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jFrame.setSize(width, height);
		jFrame.setBounds(0, 0, width, height);
		jFrame.setLocationRelativeTo(null); // 设置窗口居中
		/****************设置布局***********************/
		jFrame.getContentPane().setLayout(null);
		initialize();
	}
	private void initialize() {
		/****************主页mainPanel************************/
		mainPanel = new MainPanel(width, height,jFrame);
		mainPanel.setVisible(true);
		jFrame.add(mainPanel);
		jFrame.setVisible(true);
		jFrame.requestFocus();
	}


	class ImplAWTEventListener implements AWTEventListener {  
		@Override
		public void eventDispatched(AWTEvent event) {
			if (event.getClass() == KeyEvent.class) {
				// 被处理的事件是键盘事件.
				KeyEvent keyEvent = (KeyEvent) event;
				if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {
					keyPressed(keyEvent);
				} else if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {
					keyReleased(keyEvent);
				}
			}
		}
		private void keyPressed(KeyEvent event) {
			System.out.println(event.getKeyChar());
			if (event.getKeyCode() == KeyEvent.VK_ESCAPE) {
				//esc按下
				jFrame.dispose();
			}
		}
		private void keyReleased(KeyEvent event) {
		}
	} 
}

package com.aast.view;

import javax.accessibility.Accessible;


public class MainPanel extends JPanel implements Accessible {

	private static final long serialVersionUID = 1L;
	public Camera canvas;
	private JPanel cam;
	private static int w=640,h=480;

	OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0); 
	Java2DFrameConverter converter = new Java2DFrameConverter();
	public  ExecutorService threadPool = Executors.newFixedThreadPool(5);
	private ShowImage showImage;


	private BufferedImage bufferedImage;

	//线程池
	public MainPanel(int width, int height,JFrame jFrame){
		setSize(width, height);
		setLayout(null);
		Init();
		
		threadExecute();
	}

	private void threadExecute() {
		//显示摄像头线程
		showImage=new ShowImage();
		threadPool.execute(showImage);

	}

	private void Init() {
		cam=new JPanel();
		cam.setBounds(0,0,640, 480);
		cam.setVisible(true);
		add(cam);
		canvas= new Camera();
		canvas.setBounds(0,0,640, 480);
		cam.add(canvas);
	}
	class ShowImage extends Thread{
		@Override
		public void run() {
			try {
				System.out.println(w+","+h);
				canvas.init();
				grabber.setImageHeight(w);
				grabber.setImageWidth(h);
				grabber.start();
				bufferedImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
				while(!isInterrupted()){  
					bufferedImage=converter.getBufferedImage(grabber.grab());//获取bufferedImage
					canvas.setImg(bufferedImage);
					Thread.sleep(10);
					if(!canvas.isDisplayable()){
						grabber.stop();
					}  
				} 
			} catch (org.bytedeco.javacv.FrameGrabber.Exception |java.lang.NullPointerException |InterruptedException e) {
//				e.printStackTrace();
			} 
		}

	}

}

package com.aast.view;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

public class Camera extends Canvas {
	private static final long serialVersionUID = 1L;
	private BufferedImage img;
	@Override 
	public void update(Graphics g) {
		paint(g);
	}

	@Override 
	public void paint(Graphics g) {
		try {
			if (getWidth() <= 0 || getHeight() <= 0) {
				return;
			}
			BufferStrategy strategy = getBufferStrategy();
			do {
				do {
					g = strategy.getDrawGraphics();
					if (img != null) {                
						g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
						g.setColor(Color.YELLOW);
					}
					g.dispose();
				} while (strategy.contentsRestored());
				strategy.show();
			} while (strategy.contentsLost());
		} catch (NullPointerException e) {
		} catch (IllegalStateException e) { }
	}


	public BufferedImage getImg() {
		return img;
	}

	public void setImg(BufferedImage img) {
		this.img = img;        
		repaint();                       
	}


	//实现双缓冲
	public void init(){
		new Thread(){
			public void run(){
				boolean error = true;
				while(error){
					try{
						error = false;
						createBufferStrategy(2);
					}catch(IllegalStateException e){
						error = true;
						try {
							Thread.sleep(50);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
					}
				}
			}
		}.start();
	}
}

需要的jar包

阅读更多
个人分类: JavaSE
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭