0
我试图从相机捕获预览图像,然后通过wifi将其发送到我的计算机。Android通过tcp压缩和发送图像
的过程如下:
在我的电话:启动摄像头预览,然后压缩并发送过来的TCP连接。 在我的电脑上:接收压缩数据并保存照片。
我使用手机发送此代码:
try {
ByteArrayOutputStream outstr = new ByteArrayOutputStream();
Camera.Parameters parameters = camera.getParameters();
Size size = parameters.getPreviewSize();
YuvImage image = new YuvImage(data, parameters.getPreviewFormat(), size.width, size.height, null);
image.compressToJpeg(new Rect(0, 0, image.getWidth(), image.getHeight()), 100, outstr);
out.writeBytes("DATA|" + outstr.size() + "\n");
out.flush();
out.write(outstr.toByteArray());
out.flush();
} catch (IOException e) {
t.append("ER: " + e.getMessage());
}
在哪里出的DataOutputStream
在onCreate
方法创建:
tcp = new Socket("192.168.0.12", 6996);
in = new BufferedReader(new InputStreamReader(tcp.getInputStream()));
out = new DataOutputStream(tcp.getOutputStream());
然后用我的计算机上运行此代码:
StreamReader sr = new StreamReader(client.GetStream());
string line = sr.ReadLine();
if(line.StartsWith("DATA"))
{
piccount++;
int size = Convert.ToInt32(line.Substring(5));
Console.WriteLine("PHOTO, SIZE: " + size + ", #: " + piccount);
byte[] data = new byte[size];
client.GetStream().Read(data, 0, size);
FileStream fs = System.IO.File.Create("C:/Users/M/photo"+piccount+".jpeg");
fs.Write(data, 0, data.Length);
fs.Flush();
fs.Close();
}
问题是,发送的图片是可以的,但其中一些是c orrupted。哪里可以解决问题?
好吧,我不能复制全码流播放,因为我多次与标题行数据发送照片| lenght 。我实现了这个:'int i = client.GetStream()。Read(data,0,size); while(i