JavaCV的初次使用(在Eclipse)

本文介绍了作者初次使用JavaCV在Eclipse上实现电脑摄像头拍照功能的经历。遇到的挑战包括JMF对32位Win7的支持问题,以及尝试FMJ但因资料稀缺而放弃。最终选择JavaCV,通过配置javacv0.7的三个jar包成功运行程序,实现了摄像头截图并保存。在关闭程序时遇到异常,经过尝试发现可能是JDK7的不稳定性导致,改用JDK6后问题解决。
摘要由CSDN通过智能技术生成

javaCV我也是昨天才接触的,因为我想在eclipse上搞一个电脑摄像头拍照的功能,百度了一大堆东西,都是jmf的,不过jmf对32位win7的支持不是很好,但我还是安装了“jmf-2_1_1e-windows-i586.exe”试了试,然后我死心了。

还有一个fmj的文章http://www.2cto.com/kf/201206/135907.html很火,被好多人转载,下载下来,发现自己看不懂,网上fmj的文章很少很少,我也知道fmj是jmf的替代。

偶然在贴吧看见有人说javacv和opencv,然后我就百度他们。

OpenCV的全称是:Open Source Computer Vision Library。OpenCV是一个开源的跨平台计算机视觉库,它是用C/C++ 写的,旨在发挥多核心优势,提供C、C++、Python、java接口,并支持所有主流操作系统,包括Linux、Windows、Mac OS、IOS及Android。
JavaCV提供了计算机视觉领域研究人员常用的函数库的封装:OpenCV、FFmpeg、libdc1394、PGRFlyCapture、OpenKinect、VideoInput和ARToolKitPlus。

不多说了,说一下怎么用JavaCV吧!

下载javacv0.7。我用的就是这个版本。

网上好多人讲的是javacv和opencv一起配置的,但也有人说javacv中封装了opencv,不用再下载安装就能使用javacv。而且配置opencv还得设置系统变量,但我试了试,程序还是报异常:no jniopencv_core injava.library.path。文章javaCV 配置第一种的方法我试了,没用!

干脆就不管OpenCV了,反正我只要用javacv实现需求就行了。

调用摄像头的代码如下:(复制人家的,不是自己写的)

import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Timer;

import com.googlecode.javacv.CanvasFrame;
import com.googlecode.javacv.OpenCVFrameGrabber;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvReleaseImage;

/**
 * 
 * Use JavaCV/OpenCV to capture camera images
 * 
 * There are two functions in this demo:
 * 1) show real-time camera images 
 * 2) capture camera images by mouse-clicking anywhere in the JFrame, 
 * the jpg file is saved in a hard-coded path. 
 * 
 * @author ljs
 * 2011-08-19
 *
 */
public class Test1 {
    public static String savedImageFile = "D:\\tmp\\my.jpg";

    //timer for image capture animation
    static class TimerAction implements ActionListener {
        private Graphics2D g;
        private CanvasFrame canvasFrame;
        private int width,height;

        private int delta=10;
        private int count = 0;

        private Timer timer;
        public void setTimer(Timer timer){
            this.timer = timer;
        }

        public TimerAction(CanvasFrame canvasFrame){
            this.g = (Graphics2D)canvasFrame.getCanvas().getGraphics(); 
            this.canvasFrame = canvasFrame;
            this.width = canvasFrame.getCanvas().getWidth();
            this.height = canvasFrame.getCanvas().getHeight();
        }
        public void actionPerformed(ActionEvent e) {
            int offset = delta*count;
            if(width-offset>=offset && height-offset >= offset) {        
                g.drawRect(offset, offset, width-2*offset, height-2*offset);
                canvasFrame.repaint();
                count++;
            }else{
                //when animation is done, reset count and stop timer.
                timer.stop();
                count = 0;
            }
        }
    }

    public static void main(String[] args) throws Exception {
        //open camera source
        OpenCVFrameGrabber grabber = new OpenCVFrameGrabber(0);
        grabber.start();

        //create a frame for real-time image display
        CanvasFrame canvasFrame = new CanvasFrame("Camera");
        IplImage image = grabber.grab();
        int width = image.width();
        int height = image.height();
        canvasFrame.setCanvasSize(width, height);

        //onscreen buffer for image capture
        final BufferedImage bImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D bGraphics = bImage.createGraphics();     

        //animation timer
        TimerAction timerAction = new TimerAction(canvasFrame);
        final Timer timer=new Timer(10, timerAction);
        timerAction.setTimer(timer);

        //click the frame to capture an image
        canvasFrame.getCanvas().addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e){     
                timer.start(); //start animation
                try {
                    ImageIO.write(bImage, "jpg", new File(savedImageFile));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }                   
            }                
        });

        //real-time image display
        while(canvasFrame.isVisible() && (image=grabber.grab()) != null){
            if(!timer.isRunning()) { //when animation is on, pause real-time display
                canvasFrame.showImage(image);   
                //draw the onscreen image simutaneously
                bGraphics.drawImage(image.getBufferedImage(),null,0,0);  
            }
        }

        //release resources
        cvReleaseImage(image);   
        grabber.stop();
        canvasFrame.dispose();
    }

}

程序调用摄像头,点击摄像画面就能保存当时的画面。
然后导入需要的包,我试了试,只需3个jar包就能使上面的代码运行。
..\javacv\javacv-bin\javacpp.jar
..\javacv-bin\javacv.jar
..\javacv-cppjars\opencv-2.4.8-windows-x86.jar

程序运行的很好,也能保存摄像截图。(注意:上面的代码我只用了javacv的3个jar包,完全没有配置opencv)


但就在我关闭程序时,出现了下面的问题:
问题

百度了一下,网上很多对这个问题的讨论,但谁都说不清,注定了我也说不清,我只能给出现这个问题的同学提供一个想法。

  1. 有说:是谷歌输入法的问题。当时我也怀疑是谷歌的问题,因为看javacv的包名com.googlecode.javacv就能看出,javacv和谷歌是有关的。
  2. 有说:卸了谷歌输入法后,用来搜狗输入法,还是报错,原来是不能用中文输入法造成的。
  3. 有说:重转系统。
  4. 有说:装jdk6

    上面的方法不能说谁对谁错,看实际情况了,也许你试了其中一个方法就解决问题了呢!
    另外解释一下“java(tm) platform se binary”的意思,百度知道上有人回答的:“这是j2se的安装程序,是一个Java的运行库,所有用JAVA写的桌面程序、浏览器要用到的JAVA代码都需要此运行库来运行”(突然感觉自己什么也不知道,全靠想知道和百度)。

    wo采用了最后一种方法,确实jdk7不稳定,老是出现一些莫名其妙的问题。装了jdk6后,就没报错了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

归否

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值