【发布时间】:2018-07-13 07:04:37
【问题描述】:
在我正在构建的 android 应用程序中,我使用 Base64.encodeToString() 将 jpg 图像转换为字符串并通过 TCP 套接字发送到服务器。
问题是当我尝试将字符串解码回图像时。 我可以打印收到的字符串,它在文件末尾看起来像这样(我可以复制的唯一部分,因为文件太大,无法在终端上打印所有内容):
....+77DgjRKHqbxBmYCDOzv9vLzFwff4N146snCWin6ZlzbN++HJOIIPodB/JTOoc1NjczeqoHwOju
iWdI6ePeSO0ADz46vh4LODnM7FCJYhbTX0TizmNatXvxSFoVzLiqfn19iYjvAPD/AQnRoUxtpJij
AAAAAElFTkSuQmCC
但是当我再次尝试解码并保存到 jpg 文件时,我收到以下错误:
Traceback (most recent call last):
File "tcp.py", line 20, in <module>
file.write(base64.decodestring(msg))
File "/usr/lib/python2.7/base64.py", line 328, in decodestring
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding
这是我用于编码和发送消息的一段 Android 应用程序代码:
//Function that encodes the Bitmapfile into string
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] arr=baos.toByteArray();
String result=Base64.encodeToString(arr, Base64.DEFAULT);
return result;
}
class myTask extends AsyncTask<Void,Void,Void>
{
//Faz a conexao entre aparelho e servidor
@Override
protected Void doInBackground(Void... params){
try
{
//create socket and buffer to send the string message
newSocket = new Socket(ipAdress,5000);
printWriter = new PrintWriter(newSocket.getOutputStream());
bufferedWriter = new BufferedWriter(new OutputStreamWriter(newSocket.getOutputStream()));
//Reads the internal storage image and converts into string using Base64
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM+"/Reccoon3D/123.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
message = BitMapToString(bitmap); //encodes the bitmap
//sends the enconded image
bufferedWriter.write(message);
bufferedWriter.flush();
bufferedWriter.close();
newSocket.close();
}catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
这是我接收消息并尝试再次将其解码为图像的python代码:
import socket
import base64
host = '192.168.1.16'
port = 5000
tcp=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
orig = (host,port)
tcp.bind(orig)
tcp.listen(1)
file=open('file.png','wb')
while True:
con, client = tcp.accept()
print('Conected by', client)
while True:
msg = con.recv(1024000) #Initially 1024 but changet so the message
#would not be sliced into pieces
if not msg: break
#print(msg)
file.write(base64.decodestring(msg))
print('Finalizado com sucesso')
con.close
【问题讨论】:
-
你没有错过一些(最多 2 个)
=base64 blob 末尾的字符吗? -
首先检查发送的字节数是否等于接收的字节数。请说出金额。
-
`recv´ 不能保证一次返回所有数据,即使您提供较大的数据。 Receive all of the data when using python socket 的可能重复项
-
I convert the jpg image to string。一点都不。您将 jpg 图像转换为位图。然后把位图转成一个png。然后你对 png 进行 base64 编码。
标签: android python sockets tcp base64