我正在製作一個簡單的android應用程序,它將攝像頭的視頻源流到基於自定義java的服務器,我使用tcp發送幀(基本上是字節[]),[我知道tcp不是視頻流的好方法,但對我來說可以],android是客戶端,java應用程序是服務器,我已經成功實現了客戶端,但是我在實現java應用程序時遇到問題,服務器必須接收字節[],將它們轉換爲圖像並顯示在一些圖像容器,這是服務器的源代碼:Android的流攝像頭飼料到基於java的服務器
package dummyserver;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.media.jai.PlanarImage;
/**
*
* @author usama
*/
public class ServerListen implements Runnable{
CameraFeed camera_feed_ui;
ServerSocket server_socket=null;
Socket client_socket=null;
InputStream in=null;
int port=9000;
DataInputStream dis=null;
public ServerListen() {}
public ServerListen(CameraFeed camera_feed_ui)
{
this.camera_feed_ui = camera_feed_ui;
}
public void run() {
int len = 0;
byte[] data;
while(true)
{
try {
System.out.println("Waiting");
server_socket = new ServerSocket(port);
client_socket=server_socket.accept();
System.out.println("Client arrived");
System.out.println("Reading Image");
in=client_socket.getInputStream();
data=new byte[client_socket.getReceiveBufferSize()];
in.read(data, 0, client_socket.getReceiveBufferSize());
Image image = getImageFromByteArray(data);
BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
PlanarImage planar_image=PlanarImage.wrapRenderedImage(bufferedImage);
System.out.println("Converting byte[] to Image completed");
camera_feed_ui.displayImage(planar_image);
// client_socket.close();
server_socket.close();
}
catch(Exception ex)
{
System.out.println("error: " + ex.toString());
}
}
}
public static Image getImageFromByteArray(byte[] byteArray) {
InputStream is = new ByteArrayInputStream(byteArray);
try {
return ImageIO.read(is);
} catch (IOException ex) {
System.out.println("Unable to converet byte[] into image.");
return null;
}
}
}
的代碼說明: CameraFeed是我的JFrame的對象並將其基本上包含的圖像C要在其上顯示視頻的顯示器(用於顯示我正在使用的視頻jai [Java advance imaging])。 displayImage(PlanarImage)方法只是將圖像顯示在容器中。我認爲問題在於將byte []轉換爲圖像,或者我沒有從套接字中正確提取byte [],現在在輸出中我得到一個黑色圖像。另外一件事,在客戶端,我爲每一幀都建立了tcp連接,從這段代碼中我也很清楚,我在收到幀之後關閉了連接(server_socket.close()),這是一個好方法?我怎樣才能使這個流媒體高效?如果你可以請描述適當的方法從android手機到服務器的視頻流(我問的算法)。
在此先感謝
問候
烏薩馬·本
編輯:
C#代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
namespace VideoServer
{
public partial class Form1 : Form
{
TcpListener server_socket;
Socket client_socket;
Thread video_thread;
NetworkStream ns;
public Form1()
{
InitializeComponent();
server_socket = null;
client_socket = null;
}
private void startVideoConferencing()
{
try
{
server_socket = new TcpListener(System.Net.IPAddress.Parse("192.168.15.153"),9000);
server_socket.Start();
client_socket = server_socket.AcceptSocket();
ns = new NetworkStream(client_socket);
pictureBoxVideo.Image = Image.FromStream(ns);
server_socket.Stop();
if (client_socket.Connected == true)
{
while (true)
{
startVideoConferencing();
}
ns.Flush();
}
}
catch (Exception ex)
{
button1.Enabled = true;
video_thread.Abort();
}
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
video_thread = new Thread(new ThreadStart(startVideoConferencing));
video_thread.Start();
}
}
}
Basicall問題是,在java中,如果在java中有任何這樣的方法只需要一個流並將其轉換爲圖像,同時抽象轉換的低級細節,那麼Java中的Image.FromStream的等效也相當於此。那麼任何想法?
您可以發佈您的C#實現嗎? –
我發佈了C#代碼.. – uyaseen
我有同樣的問題,你會得到任何成功 –