我正在开发一个请求快照到DVR和IP摄像头的应用程序。我正在处理的设备只提供RTSP请求。然后我实现了必要的RTSP方法来开始接收流数据包,然后我开始通过UDP连接建立接收。我的疑问是,如何将收到的数据保存到jpeg文件?收到的图像字节的乞讨和结束在哪里?
我搜索了许多在Java中实现这种服务的库,比如Xuggler(它不再维护),javacpp-presets - 包含了ffmpeg和opencv库 - 我遇到了一些环境问题。如果有人知道从流中保存快照的简单且好的,请告诉我。
我的代码:
final long timeout = System.currentTimeMillis() + 3000;
byte[] fullImage = new byte[ 1024 * 1024 ];
DatagramSocket udpSocket = new DatagramSocket( 8000 );
int lastByte = 0;
// Skip first 2 packets because I think they are HEADERS
// Since I don't know what they mean, I just print then in hexa
for( int i = 0; i < 2; i++ ){
byte[] buffer = new byte[ 1024 ];
DatagramPacket dataPacket = new DatagramPacket( buffer, buffer.length );
udpSocket.receive( dataPacket );
int dataLenght = dataPacket.getLength();
buffer = Arrays.copyOf( buffer, dataLenght );
System.out.println( "RECEIVED[" + DatatypeConverter.printHexBinary( buffer ) + " L: " + dataLenght );
}
do{
byte[] buffer = new byte[ 1024 ];
DatagramPacket dataPacket = new DatagramPacket( fullImage, fullImage.length );
udpSocket.receive( dataPacket );
System.out.println( "RECEIVED: " + new String( fullImage ) );
for( int i = 0; i < buffer.length; i++ ){
fullImage[ i + lastByte ] = buffer[ i ];
lastByte ++;
}
} while( System.currentTimeMillis() < timeout );
// I know this timeout is wrong, I should stop after getting full image bytes
输出:
RECEIVED:80E0000100004650000000006742E01FDA014016C4 L:21 收到:80E00002000046500000000068CE30A480 L:17 RECEIVED:来自流媒体的大量数据...... RECEIVED:来自流媒体的大量数据...... RECEIVED:来自流媒体的大量数据...... [...]
正如您可能想象的那样,我保存到文件中的图像不可读,因为我做错了。 我认为标题为我提供了一些关于服务器发送给我的下一个数据包的信息,告诉我从流媒体开始和结束图像。但我不理解他们。有人知道怎么解决吗?欢迎任何提示!