所以我一直试图通过socket程序将图像从android传输到pc。但转移是模糊的,因为有时服务器崩溃或有时我无法接收完整的图像。
服务器
while (true) {
ServerSocket serverSocket = new ServerSocket(5001);
System.out.println("Server is running at port 5001");
Socket socket = serverSocket.accept();
InputStream inputStream = socket.getInputStream();
System.out.println("Reading: " + System.currentTimeMillis());
byte[] sizeAr = new byte[4];
inputStream.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageAr = new byte[size];
System.out.println(imageAr);
inputStream.read(imageAr);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageAr));
System.out.println("Received " + image.getHeight() + "x" + image.getWidth() + ": " + System.currentTimeMillis());
name = "test" + count + ".png";
ImageIO.write(image, "png", new File(name));
count++;
//new CopyCut(name);
serverSocket.close();}
我收到以下客户的图片:
客户端
public void Client(Bitmap bitmap,Context context){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
//byte [] b=baos.toByteArray();
try {
if(ip.isEmpty()){
ipSetting(context);
}
s = new Socket(ip, 5001);
OutputStream outputStream = s.getOutputStream();
byte[] size = ByteBuffer.allocate(4).putInt(baos.size()).array();
outputStream.write(size);
outputStream.write(baos.toByteArray());
outputStream.flush();
s.close();
}catch (IOException e)
{
e.printStackTrace();
}
}
我犯了什么错误我服务器崩溃或部分接收图像。 当服务器崩溃时出现以下错误。
错误
javax.imageio.IIOException: Error reading PNG image data
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1345)
at com.sun.imageio.plugins.png.PNGImageReader.read(PNGImageReader.java:1614)
at javax.imageio.ImageIO.read(ImageIO.java:1448)
at javax.imageio.ImageIO.read(ImageIO.java:1352)
at ServerImage.run(ServerImage.java:33)
at java.lang.Thread.run(Thread.java:748)Caused by: javax.imageio.IIOException: Unknown row filter type (= 128)!
at com.sun.imageio.plugins.png.PNGImageReader.decodePass(PNGImageReader.java:1143)
at com.sun.imageio.plugins.png.PNGImageReader.decodeImage(PNGImageReader.java:1223)
at com.sun.imageio.plugins.png.PNGImageReader.readImage(PNGImageReader.java:1338)
... 5 more
如果可能的话,帮助我正确的代码。