java相机_java语言调用摄像头 (不是照相机)

该博客展示了如何使用Java的webcam-capture库来调用摄像头并进行截图操作。代码中创建了一个GUI按钮,点击即可从默认摄像头捕获图片并保存到指定路径。注意,该代码不适用于照相机操作,C++可能提供对照相机的支持。
摘要由CSDN通过智能技术生成

需要jar包如下:

1、 webcam-capture-0.3.12.jar

2、bridj-0.6.2.jar

下面代码可以实现调用摄像头的效果

package service;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.io.IOException;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.SwingUtilities;

import com.github.sarxos.webcam.Webcam;

import com.github.sarxos.webcam.WebcamPanel;

import com.github.sarxos.webcam.WebcamResolution;

import com.github.sarxos.webcam.WebcamUtils;

import com.github.sarxos.webcam.util.ImageUtils;

public class CaptureDemo

{

private static int    num    = 0;

public static void main(String[] args) throws IOException

{

final Webcam webcam = Webcam.getDefault();

webcam.setViewSize(WebcamResolution.VGA.getSize());

WebcamPanel panel = new WebcamPanel(webcam);

panel.setFPSDisplayed(true);

panel.setDisplayDebugInfo(true);

panel.setImageSizeDisplayed(true);

panel.setMirrored(true);

final JFrame window = new JFrame("摄像头");

window.addWindowListener(new WindowAdapter() {

@Override

public void windowClosed(WindowEvent e)

{

webcam.close();

window.dispose();

}

});

// window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton("截图");

window.add(panel, BorderLayout.CENTER);

window.add(button, BorderLayout.SOUTH);

window.setResizable(true);

window.pack();

window.setVisible(true);

button.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e)

{

button.setEnabled(false);

String fileName = "D://" + num;

WebcamUtils.capture(webcam, fileName, ImageUtils.FORMAT_PNG);

SwingUtilities.invokeLater(new Runnable() {

@Override

public void run()

{

JOptionPane.showMessageDialog(null, "截图成功");

button.setEnabled(true);

num++;

return;

}

});

}

});

}

}

效果图

0818b9ca8b590ca3270a3433284dd417.png

1adf4afbae976f90de2e3277f85e570d.png

注 经验证发现上面代码无法对照相机进行操作,仅可以对摄像头进行操作,C++可以对照相机实现操作。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,要使用Java OpenCV调用摄像头并进行文字识别,您需要遵循以下步骤: 1. 安装Tesseract OCR引擎 Tesseract OCR引擎是一个开源的OCR引擎,可以识别多种语言的文本。您可以从Tesseract OCR官方网站下载并安装Tesseract OCR引擎。 2. 导入Tesseract OCR库 在Java项目中,您需要导入Tesseract OCR库。您可以使用Maven和Gradle等构建工具来导入Tesseract OCR库。 3. 调用摄像头 您可以使用Java OpenCV中的VideoCapture调用摄像头。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.videoio.VideoCapture; public class CameraCapture { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); if(!camera.isOpened()){ System.out.println("Error"); } else { Mat frame = new Mat(); while(true){ if (camera.read(frame)){ System.out.println("Frame Obtained"); System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height()); Core.flip(frame, frame, 1); break; } } } camera.release(); } } ``` 此代码段将获取从摄像头获取的帧并将其翻转。 4. 文字识别 要进行文字识别,您可以使用Java OpenCV中的Imgcodecs类将帧转换为图像,然后使用Tesseract OCR库进行识别。以下是一个简单的示例代码: ``` import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.videoio.VideoCapture; import net.sourceforge.tess4j.*; public class TextRecognition { public static void main(String[] args) { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); VideoCapture camera = new VideoCapture(0); Mat frame = new Mat(); while (true){ if (camera.read(frame)){ Mat grayFrame = new Mat(); Imgproc.cvtColor(frame, grayFrame, Imgproc.COLOR_BGR2GRAY); File imageFile = new File("image.png"); Imgcodecs.imwrite(imageFile.getAbsolutePath(), grayFrame); ITesseract tess = new Tesseract(); tess.setDatapath("tessdata"); tess.setLanguage("eng"); String result = tess.doOCR(imageFile); System.out.println(result); HighGui.imshow("Text Recognition", frame); HighGui.waitKey(1); } } } } ``` 此代码段将在从摄像头获取的帧中识别文本。它首先将帧转换为灰度图像,然后将其保存到磁盘上的图像文件中。然后,它使用Tesseract OCR库对图像文件中的文本进行识别,并将结果打印到控制台上。您需要将Tesseract OCR库的数据路径设置为“tessdata”文件夹,其中包含识别语言的数据文件。 希望这可以帮助您开始使用Java OpenCV调用摄像头并进行文字识别。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值