java从命名的Linux管道读取BufferedInputStream只工作一次
我绝望了,。。。我已经试过很多次了,但都没找到。请帮忙
有点背景: 我使用raspberry Pi 3开发了一个网络摄像头流媒体服务器,因为我不想要那些可用的。使用raspistill时,fps非常低(4fps),这就是为什么我考虑使用v4l2选项来播放网络摄像头。为此,我将mjpeg视频输出到管道中
从该管道读取时,会显示第一个jpeg图像,但连续读取返回null
为了进一步研究这个问题,我制作了一个小的演示程序——同样的结果
下面是我使用的代码:
从bufferedinputstream读取数据时重复20次
private void standardRead()
{
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(new File(image_path)));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Is mark supported? "+bis.markSupported());
try {
for(int i=0;i<20;i++)
{
readingImage(bis,i);
TimeUnit.MILLISECONDS.sleep(250);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
读取方法(通过一些System.out增强)
private void readingImage(BufferedInputStream bis,int iteration) throws IOException
{
System.out.println("Available bytes to read:"+bis.available());
System.out.println("Reading image"+iteration);
BufferedImage read = ImageIO.read(bis);
if(read!=null){
System.out.println(read.getRGB(25, 25)+" h:"+read.getHeight());System.out.println();
}else
{
System.out.println("image is null");
}
read = null;
}
我已经尝试过: -为每个迭代创建一个新的BufferedInputStream -关闭并创建新的BufferedInputStream -尝试使用标记和重置(运气不佳) -使用read而不是ImageIO从流中读取(读取速度明显为20fps左右)
当我执行程序时,v4l2通知帧已被消耗,因此java程序正在清空/读取管道,以便可以将新帧输入其中。 只有第一个图像,而且只有在程序第一次执行时,才会返回一个图像。程序的第二次执行也会为第一个图像提供null
下面是一个示例输出:
Is mark supported? true
Available bytes to read:65536
Reading image0
image is null
Available bytes to read:73720
Reading image1
image is null
Available bytes to read:73712
Reading image2
image is null
Available bytes to read:73704
Reading image3
image is null
Available bytes to read:73696
Reading image4
image is null
Available bytes to read:73688
Reading image5
image is null
如果有帮助的话,请注意。对于ImageIO。读取(InputStream)函数,Java doc声明了一些我无法理解的奇怪内容:
(...) The InputStream is wrapped in an ImageInputStream. If no registered ImageReader claims to be able to read the resulting stream, null is returned (...)
提前感谢您的帮助和建议
# 1 楼答案
一个不眠之夜之后,我找到了工作
尤里卡:我使用v4l2库将1000帧流式传输到linux管道中,可以读取所有1000帧。将每个文件保存到一个目录大约需要103秒,也就是10fps。没有跳过单个帧
以下是方法:
提示:频繁刷新流并重置索引。如果不刷新不断增长的内存,性能会急剧下降
提示:标准ImageIO不读取BGR3、RGB3、YU12、YUYV、YV12、YVYU,而是读取H264和MJPEG
小贴士:读者通过