java openCV调用摄像头并以窗体显示出来

开始

我的编码工具为idea 2020.3
准备工作很简单,下载openCV然后解压配置
如何新建项目或者怎么继承到你们自己的项目请自行解决,等下会把源码贴出来

idea 配置openCV

方法一

在这里插入图片描述
第三步选择java,找到你解压目录下opencv\build\java
在这里插入图片描述
在这里插入图片描述

方法二

此方法为maven
在项目resources目录下新建libsopencv-450.jar的放到此目录下。
然后在项目pom.xml文件中加入

<dependency>
            <groupId>opencv</groupId>
            <artifactId>opencv</artifactId>
            <version>450</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/libs/opencv-450.jar</systemPath>
</dependency>

此处有坑啊!
在这里插入图片描述
这个dll可能会出现报错,报错我没有保存,大概就是环境没配置好
我的方法是把安装目录下\opencv\build\bin放到java jdk 环境的bin目录下。

代码

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

import java.awt.image.BufferedImage;

/**
 * ========================================
 *
 * @ClassName DevOnline
 * @Description todo
 * @Author Administrator
 * @Date 2021/3/23 0023上午 8:51
 * ========================================
 **/
public class DevOnline {
    /**
     * 启动类
     *
     * @param args
     */
    public static void main(String[] args) {
        try {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
            System.out.println("opencv " + Core.VERSION);
            //创建Opencv中的视频捕捉对象
            VideoCapture camera = new VideoCapture();
            camera.release();
            //open函数中的0代表当前计算机中索引为0的摄像头,如果你的计算机有多个摄像头,那么一次1,2,3……
            camera.open(0,700);

            System.out.println("isOpen=" + camera);

            if (!camera.isOpened()) {
                //isOpened函数用来判断摄像头调用是否成功
                System.out.println("Camera Error");
                //如果摄像头调用失败,输出错误信息
                return;
            } else {
                System.out.println("摄像机名字:"+camera.getBackendName());
                //获取帧的宽度
                int frameWidth = (int) camera.get(3);
                if (frameWidth == 0) {
                    frameWidth = 1024;
                }
                //获取帧的高度
                int frameHeight = (int) camera.get(4);
                if (frameHeight == 0) {
                    frameHeight = 768;
                }
                //创建一个输出帧
                Mat mat = new Mat();
                String winName = "打开摄像头";
                HighGui.namedWindow(winName);
                HighGui.resizeWindow(winName, frameWidth, frameHeight);
                //读取一帧
                //read方法读取摄像头的当前帧
                //                boolean have = camera.read(frame);
                while (camera.read(mat)) {
                    //          会翻转
                    // Win上摄像头
                    //                Core.flip(frame, frame, 1);
                    // 重置大小
                    Mat dst = new Mat();
                    Imgproc.resize(mat, dst, new Size(frameWidth, frameHeight));
                    if (!dst.empty()) {
                        //Mat转换BufferedImage并刷新
                        HighGui.imshow(winName, dst);
                        // waitkey 必须要,否则无法显示
                        int key = HighGui.waitKey(5);
                        System.out.println("key=" + key);
                        try {
                            System.out.println("停一下");
                            Thread.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.out.println("Mat 为空。。。");
                        break;
                    }


                }
                HighGui.destroyAllWindows();
                camera.release();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }


    }

    /**
     * Mat类转换BufferedImage类
     *
     * @param mat
     * @return
     */
    public static BufferedImage conver2Image(Mat mat) {
        int width = mat.cols();
        int height = mat.rows();
        int dims = mat.channels();
        int[] pixels = new int[width * height];
        byte[] rgbdata = new byte[width * height * dims];
        mat.get(0, 0, rgbdata);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        int index = 0;
        int r = 0, g = 0, b = 0;
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                if (dims == 3) {
                    index = row * width * dims + col * dims;
                    b = rgbdata[index] & 0xff;
                    g = rgbdata[index + 1] & 0xff;
                    r = rgbdata[index + 2] & 0xff;
                    pixels[row * width + col] = ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | b & 0xff;
                }
                if (dims == 1) {
                    index = row * width + col;
                    b = rgbdata[index] & 0xff;
                    pixels[row * width + col] = ((255 & 0xff) << 24) | ((b & 0xff) << 16) | ((b & 0xff) << 8) | b & 0xff;
                }
            }
        }
        setRGB(image, 0, 0, width, height, pixels);
        return image;
    }

    /**
     * 图像中设置ARGB像素
     *
     * @param image
     * @param x
     * @param y
     * @param width
     * @param height
     * @param pixels
     */
    public static void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
        int type = image.getType();
        if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {
            image.getRaster().setDataElements(x, y, width, height, pixels);
        } else {
            image.setRGB(x, y, width, height, pixels, 0, width);
        }
    }

}

结束

看下效果
在这里插入图片描述

  • 5
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
好的,要使用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调用摄像头并进行文字识别。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无与伦比的傻

微信公众号:dasha500

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值