カメラからプレビュー画像をキャプチャして、wifi経由でコンピューターに送信しようとしています。
プロセスは次のとおりです。
私の電話の場合:カメラのプレビューを開始し、圧縮して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());
}
outがメソッドで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();
}
問題は、送信された画像の一部は問題ありませんが、一部が破損していることです。問題はどこにありますか?