关于jmstudio 无法初始化海康威视摄像头初始化的问题,求助

最近突然想做一个人脸识别的程序,图像的获取可以从本地或者摄像头获取。
1、从本地获取比较简单,不多说。
2、从摄像头获取图片。网上查了一下,都是用的JMF(但是JMF已经很久没有更新,而且只支持32位的JDK,如果知道有更好的麻烦推荐一下!)
安装完之后桌面上回出现JMStudio的快捷图标,
&点开测试一下,file-&capture;
摄像头驱动正常加载,点击OK,然后
我靠,这什么鬼?一片粉啊,一定要这么嫩吗 -_-||。 晃动一下窗口,会显示图像,但是是卡住的。
摄像头正常打开了,但是显示不了画面,这样完全没用啊!于是就去网上查了一下,没有答案,再看看回答时间,瞬间怀疑自己是不是用了个老古董了!
没办法,先试着写程序吧
1 public static Component loadDiver()
//String str1 = "vfw:logitech usb video camera:0";
String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
deviceInfo = CaptureDeviceManager.getDevice(str2);
//加载设备驱动
mLocator = deviceInfo.getLocator();
<span style="color: #
<span style="color: #
player = Manager.createRealizedPlayer(mLocator);
//获取就绪的播放器
<span style="color: #
<span style="color: #
<span style="color: #
if((cp = player.getVisualComponent())!=null)
<span style="color: #
<span style="color: #
player.start();
<span style="color: #
//返回窗体组件
<span style="color: #
<span style="color: #
<span style="color: #
catch(Exception e)
<span style="color: #
<span style="color: #
e.printStackTrace();
<span style="color: #
<span style="color: #
return null;
<span style="color: #
运行之后发现一样出现粉屏的问题, 弄了一会儿发现好像在画面上覆盖了一层JLabel或者JTextArea就可以正常显示图像(感觉好low啊/捂脸),可能是打开的方式不对!
最后无意中发现,只要窗体对准电脑屏幕的最左边就可以正常显示了,这程序是不是有强迫症啊?_?
好吧,虽然莫名其妙,但是也勉强算是解决了显示问题了。
下面就是截图了,于是就用了笨办法,屏幕截图(求聪明的办法^_^):
1 BufferedImage buffImg = null;
buffImg = (new Robot()).createScreenCapture(new
Rectangle(0, 100, c1.getWidth(), c1.getHeight()-100));
//c1是上面方法返回的Component
7 catch(AWTException e1)
e1.printStackTrace();
<span style="color: # }
<span style="color: # try
<span style="color: # {
<span style="color: #
ImageIO.write(buffImg, "jpg", new File(path));
<span style="color: # }
<span style="color: # catch(IOException e1)
<span style="color: # {
<span style="color: #
e1.printStackTrace();
<span style="color: # }
成功获取图像
总结:JMF调用摄像头产生粉屏,把窗体放在屏幕最左侧可以正常显示,具体原因未知!个人猜测可能是系统不兼容的造成。
阅读(...) 评论()澳门巴黎人賭场热新闻
澎湃新闻APP下载
澳门巴黎人賭场:热门推荐请问:java导入jmf.jar包的有关问题!_读书人
请问:java导入jmf.jar包的有关问题!
&来源:读书人网&【读书人网():综合教育门户网站】
请教:java导入jmf.jar包的问题!!!急急急!问题如下:用java写一个播放器的时候导入(import javax.media.Play
请教:java导入jmf.jar包的问题!!!急急急!问题如下: & 用java写一个播放器的时候导入(import javax.media.P)jmf.jar包会出现如下的错误: & Access restriction: The type Player is not accessible due to restriction on required library C:\Program Files\Java\jdk1.6.0_22\jre\lib\ext\jmf.jar & 即访问jmf.jar时受到限制了。classpath 和path的环境变量都已经设置了。请问高手这是怎么回事啊, 如何解决这个问题啊?[解决办法]这个有点意思,你不能直接搞个jar包放到项目里面的,像摄像头那块,要安装jmStudio的那个jar包我的意思就是安装jmstudio之后他会在jre的ext文件夹中生成jmf.jar,而这个jmf的jar才是可用的,因为你注册到系统的摄像头信息也要保存在相关的文件中的,而你如果直接将jmf.jar高进项目,这个信息文件就丢失了,自然会出现一些问题,不知道是否能解决你的问题,给点类似经历而已...对摄像头初始化时错误
部分代码:
if(0 == ioctl(fd, VIDIOC_CROPCAP, &cropcap)) & & { & & & & CLEAR(crop); & & & & crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; & & & & crop.c = cropcap.
& & & & if(-1 == ioctl(fd, VIDIOC_S_CROP, &crop)) & & & & { & & & & & & if(EINVAL == errno) & & & & & & { // & & & & & & & &emit display_error(tr("VIDIOC_S_CROP not supported")); & & & & & & } & & & & & & else & & & & & & { & & & & & & & & emit display_error(tr("VIDIOC_S_CROP: %1").arg(QString(strerror(errno)))); & & & & & & & & return -1; & & & & & & } & & & & } & & } 出现错误提示:VIDIOC_S_CROP: Inappropriate ioctl for device(程序在qt中运行)
是否你的设备不支持呀?我找了找以前写的代码,供你参考。v4l2的文档中也有sample的。
/* camera io function */
static int xioctl(int fd, int request, void *arg)
r = ioctl(fd, request, arg);
}while(-1 == r && EINTR == errno);
/* set camera params to default values */
static void _setdefaultparams()
struct v4l2_queryctrl queryctrl = {0};
struct v4l2_control control = {0};
__u32 ids[] =
V4L2_CID_HFLIP,
V4L2_CID_BRIGHTNESS,
V4L2_CID_CONTRAST,
V4L2_CID_SATURATION,
V4L2_CID_HUE,
V4L2_CID_AUTO_WHITE_BALANCE,
V4L2_CID_DO_WHITE_BALANCE,
V4L2_CID_RED_BALANCE,
V4L2_CID_BLUE_BALANCE,
V4L2_CID_GAMMA,
V4L2_CID_EXPOSURE,
V4L2_CID_AUTOGAIN,
V4L2_CID_GAIN,
V4L2_CID_HFLIP,
V4L2_CID_VFLIP
U32 i = 0;
fprintf(stderr, "Camera Control\n");
for(i = 0; i & sizeof(ids) / sizeof(ids[0]); i++)
memset(&queryctrl, 0, sizeof(queryctrl));
queryctrl.id = ids[i];
if (-1 != xioctl(_cam.fd, VIDIOC_QUERYCTRL, &queryctrl))
memset(&control, 0, sizeof(control));
control.id = ids[i];
control.value = queryctrl.default_
xioctl(_cam.fd, VIDIOC_S_CTRL, &control);
fprintf(stderr, "\t%s\t:\t%d\t\n", queryctrl.name, control.value);
/* init mmap to get camera data */
static int _init_mmap()
enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
struct v4l2_requestbuffers req =
CAMERA_BUF_CNT,
/* count */
V4L2_BUF_TYPE_VIDEO_CAPTURE,
/* type */
V4L2_MEMORY_MMAP,
/* memory */
/* reserved */
struct v4l2_buffer buf = {0};
U32 i = 0;
/* tell v4l2 that we need CAMERA_BUF_CNT buffers,
and the buffer is created by V4L2_MEMORY_MMAP.
mmap means buffer is created in the kernel, and mapped to application.
if (-1 == xioctl(_cam.fd, VIDIOC_REQBUFS, &req))
printf("Error VIDIOC_REQBUFS\n");
return -1;
if (req.count != CAMERA_BUF_CNT)
printf("Error CAMERA_BUF_CNT\n");
return -2;
for (i = 0; i & CAMERA_BUF_CNT; i++)
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index =
/* get each buffers information */
if (-1 == xioctl(_cam.fd, VIDIOC_QUERYBUF, &buf))
printf("Error VIDIOC_QUERYBUF\n");
return -3;
/* map the buffer to application */
_cam.bufs[i].length = buf.
if (MAP_FAILED ==
(_cam.bufs[i].start = mmap(NULL, buf.length, PROT_READ, MAP_SHARED | MAP_LOCKED | MAP_POPULATE | MAP_NONBLOCK, _cam.fd, buf.m.offset)))
_cam.bufs[i].start = NULL;
_cam.bufs[i].length = 0;
printf("Error MAP_FAILED\n");
return -4;
/* add the buffers to the queue */
for (i = 0; i & CAMERA_BUF_CNT; i++)
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index =
if (-1 == xioctl(_cam.fd, VIDIOC_QBUF, &buf))
printf("Error VIDIOC_QBUF\n");
return -5;
/* open camera data stream */
if (-1 == xioctl(_cam.fd, VIDIOC_STREAMON, &buf_type))
printf("Error VIDIOC_STREAMON\n");
return -6;
static void _uninit_mmap()
U32 i = 0;
enum v4l2_buf_type buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
/* close camera data stream */
xioctl(_cam.fd, VIDIOC_STREAMOFF, &buf_type);
/* unmap */
for(i = 0; i & CAMERA_BUF_CNT; i++)
if (_cam.bufs[i].start)
munmap(_cam.bufs[i].start, _cam.bufs[i].length);
_cam.bufs[i].start = NULL;
_cam.bufs[i].length = 0;
static int _read_frame_mmap()
if (0 == _cam.fd || NULL == S(&_cam.img))
printf("_read_frame_mmap:Error- _cam.fd = %d, S(&_cam.img) = 0x%x\n", _cam.fd, (unsigned int)S(&_cam.img));
return -1;
memset(&_cam.buf, 0, sizeof(struct v4l2_buffer));
_cam.buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
_cam.buf.memory = V4L2_MEMORY_MMAP;
/* move out one buffer from the queue, so we can read its data */
if (-1 == xioctl(_cam.fd, VIDIOC_DQBUF, &_cam.buf))
if (EAGAIN == errno)
printf("_read_frame_mmap:Error - VIDIOC_DQBUF - EAGAIN\n");
//printf("_read_frame_mmap:Error - VIDIOC_DQBUF\n");
return -1;
if (_cam.buf.index &= CAMERA_BUF_CNT)
printf("_read_frame_mmap:Error-indec &= CAMERA_BUF_CNT\n");
return -1;
mtHSurface mtOpenCamera()
    struct v4l2_format
fmt = {0};
struct v4l2_capability
cap = {{0}};
struct v4l2_cropcap cropcap = {0};
struct v4l2_crop crop = {0};
DFBSurfaceDescription
sdsc = {0};
unsigned char
retry = 3;
printf("Open Camera, mtOpenCamera\n");
memset(&_cam, 0, sizeof(_cam));
if (-1 == (_cam.fd = open("/dev/video0", O_RDWR | O_NONBLOCK, 0)))
if (-1 == (_cam.fd = open("/dev/video", O_RDWR | O_NONBLOCK, 0)))
if(0 == retry)
printf("Can't open Camera\n");
return NULL;
printf("Retry to open Camera\n");
usleep(50 * 1000);
}while(retry --);
/* get camera's capabilities */
if (-1 == xioctl(_cam.fd, VIDIOC_QUERYCAP, &cap))
printf("Error: Get Camera's capabilities\n");
/* the camera is able to streaming (preview mode) ?
if (!(cap.capabilities & V4L2_CAP_STREAMING))
printf("Error: The Camera is able to streaming\n");
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 != xioctl(_cam.fd, VIDIOC_CROPCAP, &cropcap))
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.
xioctl(_cam.fd, VIDIOC_S_CROP, &crop);
/* set pixel format */
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(_cam.fd, VIDIOC_G_FMT, &fmt))
printf("Error: Set pixel format\n");
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = CAMERA_WIDTH;
fmt.fmt.pix.height = CAMERA_HEIGHT;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;//V4L2_FIELD_NONE;
if (-1 == xioctl(_cam.fd, VIDIOC_S_FMT, &fmt))
printf("Error: Set FMT\n");
_setdefaultparams();
W(&_cam.img) = CAMERA_WIDTH;
H(&_cam.img) = CAMERA_HEIGHT;
sdsc.flags = (DFBSurfaceDescriptionFlags)(DSDESC_HEIGHT | DSDESC_WIDTH | DSDESC_PIXELFORMAT);
sdsc.width = W(&_cam.img);
sdsc.height = H(&_cam.img);
sdsc.pixelformat = DSPF_ARGB;
if (_dfb-&CreateSurface(_dfb, &sdsc, &_cam.img.sur))
printf("Error: Create surface to save camera data\n");
if (_init_mmap())
printf("Error: _init_mmap\n");
_cam.valid.x = _cam.valid.y = 0;
_cam.valid.w = CAMERA_WIDTH;
_cam.valid.h = CAMERA_HEIGHT;
_cam.lowquality = 0;
_cam.retry = 0;
_cam.read = 0;
_cam.alpha = 255;
fprintf(stderr, "opencamera success\n");
return (mtHSurface)&_cam.
mtCloseCamera();
fprintf(stderr, "opencamera error\n");
return NULL;
--- 共有 3 条评论 ---
能正常运行mjpeg-streamer的摄像程序 但这个不行 方便提供个邮箱么 我把源码发给你
朋友,你的问题解决了吗?什么办法还记得吗?
我是在oschina上找的代码,也是报的这个错误,我用的是笔记本,系统是linux。
找到了v4l2的官网,有下面的代码:
/*&Ignore&if&cropping&is&not&supported&(EINVAL).&*/
if&(-1&==&ioctl&(fd,&VIDIOC_S_CROP,&&crop)
&&&&&&&errno&!=&EINVAL)&{
perror&("VIDIOC_S_CROP");
exit&(EXIT_FAILURE);
应该是驱动的问题吧,等换其他电脑试试吧。}

我要回帖

更多关于 海康摄像头初始化 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信