我有一个树莓派,我正试图从它使用套接字向Android设备发送2个立体声图像。我已经为服务器套接字编写了代码,以在树莓派上发送图像,甚至在android端也收到了它。但是现在我想在ImageViews中显示它。如何将字符串转换为640 x 480 x 3的int矩阵,然后转换为位图?还是有办法将字符串直接转换为位图?
Edit:我有一个原始像素值矩阵的字符串,例如:
[[[ 1 2 1]
[ 1 4 1]
[ 1 4 1]
...
[ 0 3 0]
[ 1 4 1]
[ 1 2 1]]
[[ 1 3 1]
[ 2 6 2]
[ 1 5 2]
...
[ 0 4 1]
[ 2 6 2]
[ 1 3 1]]
[[ 1 3 1]
[ 2 6 2]
[ 1 5 1]
...
[15 24 19]
[19 26 20]
[ 9 12 10]]
...
[[26 28 30]
[16 17 8]
[10 12 4]
...
[65 76 82]
[65 76 82]
[32 38 40]]
[[30 34 36]
[17 18 8]
[13 14 4]
...
[77 86 97]
[77 86 94]
[37 42 47]]
[[18 19 20]
[ 7 8 4]
[ 5 6 2]
...
[42 46 51]
[40 46 51]
[19 22 24]]]
*非完整数组,我收到的是没有点的完整数组。
从此矩阵我希望将此图像显示在图像视图中:
我已经尝试过此代码:
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte= Base64.decode(encodedString,Base64.DEFAULT);
return BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
} catch(Exception e) {
e.getMessage();
return null;
}
}
但是返回空值
我的代码(接收类):
class SocketListenThread implements Runnable {
private Socket socket;
private BufferedReader input;
@Override
public void run() {
try{
InetAddress serverAddr = InetAddress.getByName(ip_address_input.getText().toString());
socket = new Socket(serverAddr, Integer.parseInt(port_no_input.getText().toString()));
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
Log.d(TAG, "run: Done with connection");
while (run) {
Log.d(TAG, "run: Loop");
final String message = input.readLine();
final String[] list_of_images = message.split("\t,\t");
list_of_images[0] = list_of_images[0].replace(" ", "");
list_of_images[1] = list_of_images[1].replace(" ", "").replace("\n", "");
Log.d(TAG, "run: Got message");
Log.d(TAG, "run: " + message);
if (message.equals("Bye")) {
run = false;
}
runOnUiThread(new Runnable() {
@Override
public void run() {
// data_recv_textView.setText(message);
left_pic.setImageBitmap(StringToBitMap(list_of_images[0]));
right_pic.setImageBitmap(StringToBitMap(list_of_images[1]));
}
});
}
} catch (IOException e) {
e.printStackTrace();
run = false;
}
}
}
和Raspberry附加代码:
import socket, time, cv2, sys
import numpy as np
np.set_printoptions(threshold=sys.maxsize)
left = cv2.imread("left.png")
right = cv2.imread("right.png")
print(left)
print("\n\n\n\n")
print(right)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as soc:
soc.bind(('', 21567))
print("Started server on port 21567 and address " + socket.gethostname())
soc.listen()
while True:
(clientsocket, address) = soc.accept()
print("Connected by " + str(address))
while True:
clientsocket.send(bytes(str(left).replace("\n", " ") + "\t,\t" + str(right).replace("\n", " ") + "\n", "utf-8"))
print(f"Loop")
time.sleep(3)
提前感谢
我不会说有直接方法,但有可能。我们可以使用以下方法打印matrix,然后创建位图。
1]您可以在某些UI中使用某些TextView显示文本,然后使用该UI转换为bitmap,如下所示。
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
或您可以使用canvas使用paint绘制文本。然后再次使用该画布创建位图。
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
paint.setColor(Color.BLACK);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
更多信息进入here