JAVA-调用电脑摄像头

一、简介

JavaCV使用来自计算机视觉领域(OpenCV, FFmpeg, libdc1394, PGR FlyCapture, OpenKinect, librealsense, CL PS3 Eye Driver, videoInput, ARToolKitPlus, flandmark, Leptonica, and Tesseract)领域的研究人员常用库的JavaCPP预设的封装。提供实用程序类,使其功能更易于在Java平台上使用,包括Android。

二、案例1:调用摄像头
(1)使用Eclipse新建Maven项目,然后在pom.xml中引入下列依赖(因为要用到opencv来实现,所以需要引入opencv-platform包,此包会自动引入各大平台的依赖jar(内含dll)):


        <dependency>
            <groupId>org.bytedecogroupId>
            <artifactId>javacv-platformartifactId>
            <version>1.4.1version>
        dependency>
        
        <dependency>
            <groupId>org.bytedeco.javacpp-presetsgroupId>
            <artifactId>opencv-platformartifactId>
            <version>3.4.1-1.4.1version>
        dependency>

        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
package jdbcutil;

 
import javax.imageio.ImageIO;
import javax.swing.WindowConstants;

import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.bytedeco.javacv.OpenCVFrameGrabber;
import org.junit.Test;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JavaCV {
    public BufferedImage FrameToBufferedImage(Frame frame) {
		// 创建BufferedImage对象
		Java2DFrameConverter converter = new Java2DFrameConverter();
		BufferedImage bufferedImage = converter.getBufferedImage(frame);
		return bufferedImage;
	}
    @Test
    public void testCamera() throws InterruptedException, org.bytedeco.javacv.FrameGrabber.Exception{
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();   //开始获取摄像头数据
        CanvasFrame canvas = new CanvasFrame("摄像头窗口");//新建一个窗口
        canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        canvas.setAlwaysOnTop(true);
        while (true) {
            if (!canvas.isDisplayable()) {//窗口是否关闭
                grabber.stop();//停止抓取
                System.exit(-1);//退出
            }
            
            Frame frame = grabber.grab();
            canvas.showImage(frame);	//获取摄像头图像并放到窗口上显示, 这里的Frame frame=grabber.grab(); frame是一帧视频图像
            Thread.sleep(50);	//50毫秒刷新一次图像
        }
    }

    @Test
    public void testCamera1() throws Exception {
    	
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();   //开始获取摄像头数据
        CanvasFrame canvas = new CanvasFrame("摄像头窗口");//新建一个窗口
        canvas.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        canvas.setAlwaysOnTop(true);
        while (true) {
            if (!canvas.isDisplayable()) {//窗口是否关闭
                grabber.stop();//停止抓取
                System.exit(-1);//退出
            }
            
            Frame frame = grabber.grab();
            try {
            	BufferedImage bufferedImage = null;
            	bufferedImage = FrameToBufferedImage(frame);
                LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
                Binarizer binarizer = new HybridBinarizer(source);
                BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
                Map<DecodeHintType,Object> hints = new HashMap<DecodeHintType, Object>();
                hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
                Result result = new MultiFormatReader().decode(binaryBitmap, hints);//解码
                System.out.println("图片中内容:  ");
                System.out.println("content: " + result.getText());
                System.out.println(result.getText());
			} catch (Exception e) {
				System.out.println("ERROR");
			}
            
            canvas.showImage(frame);	//获取摄像头图像并放到窗口上显示, 这里的Frame frame=grabber.grab(); frame是一帧视频图像
            Thread.sleep(50);	//50毫秒刷新一次图像
        }
    }

}


JAVA-调用电脑摄像头_第1张图片
参考链接
https://www.cnblogs.com/songxingzhu/p/9088204.html

你可能感兴趣的:(JAVA,java,opencv)