java 打开摄像头_Java(opencv) 窗体打开摄像头

本文介绍了如何使用Java结合OpenCV库,在窗体应用中打开摄像头,并进行灰度处理。通过VideoCapture打开摄像头,读取帧,转换为灰度图像并显示在JLabel上。当点击“拍照”按钮时,程序会停止显示视频流。
摘要由CSDN通过智能技术生成

整个程序下载地址:Java(opencv) 窗体打开摄像头,并做一个灰度话处理,以后有关图像处理的和opencv的没有太大的区别

在testVideo项目下创建VideoIO包,包下新建ShowVideo.java窗体应用。

2de551fb74f0fa3ee72d4aa64fed106d.png

直接来看ShowVideo.java程序内容部分:

package VedioIO;

import java.awt.EventQueue;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JButton;

import org.opencv.core.Core;

import org.opencv.core.Mat;

import org.opencv.videoio.VideoCapture;

import org.opencv.imgproc.Imgproc;

import tool.mat2BufferedImage;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

public class ShowVedio {

static{System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}

private JFrame frame;

static JLabel label;

static int flag=0;

/**

* Launch the application.

*/

public static void main(String[] args) {

EventQueue.invokeLater(new Runnable() {

public void run() {

try {

ShowVedio window = new ShowVedio();

window.frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

});

//操作

VideoCapture camera=new VideoCapture();

camera.open(1);

if(!camera.isOpened()){

System.out.println("Camera Error");

}

else{

Mat frame=new Mat();

while(flag==0){

camera.read(frame);

Mat gray=new Mat(frame.rows(),frame.cols(),frame.type());

Imgproc.cvtColor(frame, gray, Imgproc.COLOR_RGB2GRAY);

label.setIcon(new ImageIcon(mat2BufferedImage.matToBufferedImage(gray)));

try{

Thread.sleep(100);

}

catch(InterruptedException e){

e.printStackTrace();

}

}

}

}

/**

* Create the application.

*/

public ShowVedio() {

initialize();

}

/**

* Initialize the contents of the frame.

*/

private void initialize() {

frame = new JFrame();

frame.setBounds(100, 100, 798, 600);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.getContentPane().setLayout(null);

JButton btnNewButton = new JButton("拍照");

btnNewButton.addMouseListener(new MouseAdapter() {

@Override

public void mouseClicked(MouseEvent arg0) {

flag=1;

}

});

btnNewButton.setBounds(20, 20, 113, 27);

frame.getContentPane().add(btnNewButton);

label=new JLabel("");

label.setBounds(50, 50, 640, 480);

frame.getContentPane().add(label);

}

}

关于

import tool.mat2BufferedImage;

在上一篇文章中已经贴出来了,这里就不写了。

Java(opencv)窗体中显示图像。 mat2BufferedImage可以查看这篇文章

结果显示:

4270c71e157e514eb1a7f106236a5480.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值