JAVACV调用本地摄像头拍照

第一次写博客,老板要求用javacv实现拍照功能

各种百度加实验,记录下  也不知道有没有啥bug,非常的不专业。。。


首先准备jar包,来源:http://blog.csdn.net/eguid_1/article/details/51659578

根据上面文章的说明导入jia包

javacv.jar,javacpp.jar,ffmpeg.jar,ffmpeg-系统平台.jar,OpenCV.jar,opencv-系统平台.jar


让我头疼都是布局问题,大小问题。蛋疼,SE的这部分就没学过

遇到的主要问题:IplImage没有获取bufferedImage对象的方法了,需要转换

public class Demo {
	public static void main(String[] args) throws Exception, IOException, InterruptedException {


		OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
		grabber.setImageWidth(800);
		grabber.setImageHeight(640);
		grabber.start();


		OpenCVFrameConverter.ToIplImage converter = new OpenCVFrameConverter.ToIplImage();
		IplImage grabbedImage = converter.convertToIplImage(grabber.grab());
		CanvasFrame frame = new CanvasFrame("Some Title", CanvasFrame.getDefaultGamma() / grabber.getGamma());
		// frame.setSize(800, 600);
		//frame.setBounds(200, 100, 640, 640);
		// 设置操作界面
		JPanel contentPane = new JPanel();
		//contentPane.setBounds(0, 0, 640, 640);
		Container contentPane2 = frame.getContentPane();


		JButton take_photo = new JButton("拍照");
		JButton save_photo = new JButton("保存");
		JButton cancle = new JButton("关闭");
		Camera camera = new Camera();
		// 监听拍摄
		take_photo.addMouseListener(new TakePhotoMouseAdapter(take_photo, camera));
		// 监听保存
		save_photo.addMouseListener(new SavePhotoMouseAdapter(grabbedImage));
		// 关闭
		cancle.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent arg0) {
				frame.setVisible(false);
			}
		});
		// 添加按钮
		contentPane.add(take_photo, BorderLayout.SOUTH);
		contentPane.add(save_photo, BorderLayout.SOUTH);
		contentPane.add(cancle, BorderLayout.SOUTH);
		// 添加面板
		contentPane2.add(contentPane, BorderLayout.SOUTH);
		// 操作状态
		while (frame.isVisible()) {


			// 获取图像
			if (camera.getState()) {
				grabbedImage = converter.convert(grabber.grab());
			}
			frame.showImage(converter.convert(grabbedImage));
			// 每40毫秒刷新视频,一秒25帧
			Thread.sleep(40);
		}


		frame.dispose();


		grabber.stop();
	}


}
//点击保存
public class SavePhotoMouseAdapter extends MouseAdapter {


	private IplImage iplImage;
	
	public SavePhotoMouseAdapter(IplImage iplImage) {
		this.iplImage = iplImage;
	}


	@Override
	public void mouseClicked(MouseEvent arg0) {
		System.out.println("保存");
		// 保存结果提示框
		JFrame myFrame = new JFrame();
		try {
			if (iplImage != null) {
				// 保存图片
				cvSaveImage("D://yepao//savedImage.jpg", iplImage);
				// 发送修改用户头像请求...也可以直接发送字节数组到服务器,由服务器上传图片并修改用户头像
				JOptionPane.showMessageDialog(myFrame, "上传成功");


			}
		} catch (IOException e) {
			JOptionPane.showMessageDialog(myFrame, "保存失败");
			e.printStackTrace();
		} finally {
			// 关闭提示jframe
			myFrame.dispose();
			myFrame = null;
		}
	}


	public static void cvSaveImage(String path, IplImage image) throws IOException {
		File file = new File(path);
		// ImageIO.write(toBufferedImage(image), "jpg", file);
		// 使用字节保存
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(toBufferedImage(image), "jpg", out);
		byte[] bs = out.toByteArray();

		// 保存字节数组为图片到本地
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(bs, 0, bs.length);
		fos.close();

		out.close();
	}


	// 通过image获取bufferedImage
	//iplConverter 可以查看convert方法可以转换的对象,Frame IplImage,Mat之间转换   
	//Java2DFrameConverter让Frame和BufferedImage之间相互转换
	public static BufferedImage toBufferedImage(IplImage image) {
		ToIplImage iplConverter = new OpenCVFrameConverter.ToIplImage();
		Java2DFrameConverter java2dConverter = new Java2DFrameConverter();
		BufferedImage bufferedImage = java2dConverter.convert(iplConverter.convert(image));
		return bufferedImage;
	}
}
//切换图像显示
public class TakePhotoMouseAdapter extends MouseAdapter {


	private JButton jButton;
	private Camera camera;


	public TakePhotoMouseAdapter(JButton jButton, Camera camera) {
		this.jButton = jButton;
		this.camera = camera;
	}


	@Override
	public void mouseClicked(MouseEvent arg0) {
		System.out.println("拍照");
		
		// 修改显示
		if (camera.getState()) {
			jButton.setText("继续拍照");
			// 暂停拍照
			camera.setState(false);
		} else {
			jButton.setText("拍照");
			// 继续拍照
			camera.setState(true);
		}
	}
}
//标记是否需要取图获片
public class Camera {
	private Boolean state = true;


	public Boolean getState() {
		return state;
	}


	public void setState(Boolean state) {
		this.state = state;
	}


}
//最后发现我举左手,视频里面的我就有事。。于是还需要图片水平翻转下 这个flip方法来自 org.bytedeco.javacpp.opencv_core.*;
Mat mat = new Mat();
int flipCode = 1;
flip(converter.convertToMat(要翻转的Frame), mat, flipCode);
保存的时候依然需要将Mat转换
BufferedImage bufferedImage = java2dConverter.convert(convert.convert(mat));

你可能感兴趣的:(JAVACV调用本地摄像头拍照)