java读取pi_(树莓派csi相机)使用Java从raspivid-stdout读取h...
我想編寫(xiě)一個(gè)Java應(yīng)用程序,它從樹(shù)莓派csi相機(jī)讀取h264流.
csi攝像機(jī)的接口是命令行c程序“ raspivid”,該程序通常將捕獲的視頻寫(xiě)入文件.
使用選項(xiàng)“ -o-”,raspivid將視頻寫(xiě)入stdout,這時(shí),我想捕獲h264流并對(duì)其進(jìn)行“管道傳輸”而不更改數(shù)據(jù).
我的第一步是編寫(xiě)一個(gè)應(yīng)用程序,該應(yīng)用程序從stdout讀取數(shù)據(jù)并將其寫(xiě)入文件,而無(wú)需更改數(shù)據(jù)(因此您將獲得一個(gè)可播放的.h264文件).
我的問(wèn)題是,寫(xiě)入的文件始終是損壞的,當(dāng)我用記事本打開(kāi)損壞的文件時(shí),我可以看到與可播放的文件相比,通常存在不同的“符號(hào)”.
我認(rèn)為問(wèn)題是InputStreamReader()類,該類將stdout-byte-stream轉(zhuǎn)換為字符流.
我無(wú)法為此找到合適的班級(jí).
這是我的實(shí)際代碼:
public static void main(String[] args) throws IOException, InterruptedException
{
System.out.println("START PROGRAM");
try
{
Process p = Runtime.getRuntime().exec("raspivid -w 100 -h 100 -n -t 5000 -o -");
FileOutputStream fos = new FileOutputStream("testvid.h264");
Writer out = new OutputStreamWriter(fos);
BufferedReader bri = new BufferedReader(new InputStreamReader(p.getInputStream()));
while (bri.read() != -1)
{
out.write(bri.read());
}
bri.close();
out.close();
}
catch (Exception err)
{
err.printStackTrace();
}
System.out.println("END PROGRAM");
}
謝謝!
最佳答案
解決了問(wèn)題!
不需要InputStreamReader并將字節(jié)流轉(zhuǎn)換為字符流,則不可能進(jìn)行反向轉(zhuǎn)換!
這是工作代碼(將stdout-byte-stream寫(xiě)入文件):
public static void main(String[] args) throws IOException
{
System.out.println("START PROGRAM");
long start = System.currentTimeMillis();
try
{
Process p = Runtime.getRuntime().exec("raspivid -w 100 -h 100 -n -t 10000 -o -");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
//Direct methode p.getInputStream().read() also possible, but BufferedInputStream gives 0,5-1s better performance
FileOutputStream fos = new FileOutputStream("testvid.h264");
System.out.println("start writing");
int read = bis.read();
fos.write(read);
while (read != -1)
{
read = bis.read();
fos.write(read);
}
System.out.println("end writing");
bis.close();
fos.close();
}
catch (IOException ieo)
{
ieo.printStackTrace();
}
System.out.println("END PROGRAM");
System.out.println("Duration in ms: " + (System.currentTimeMillis() - start));
}
總結(jié)
以上是生活随笔為你收集整理的java读取pi_(树莓派csi相机)使用Java从raspivid-stdout读取h...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mac11.14 mysql_mysql
- 下一篇: ros和java通讯_ROS学习之路(二