java - 如何将字符串转换为矩阵,然后转换为位图(Android studio,java)

标签 java android imageview android-bitmap

我有一个树莓派,我试图使用套接字将 2 个立体图像发送到 Android 设备。我已经编写了服务器套接字的代码来在树莓派上发送图像,我什至在android端接收它。但现在我想在 ImageViews 中显示它。我应该如何将字符串转换为 640 x 480 x 3 int 矩阵,然后转换为位图?或者有没有办法将字符串直接转换为位图?

编辑:我有一个原始像素值矩阵的字符串,例如:

[[[ 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]]]

*不是完整数组,我收到的是没有点的完整数组。

从这个矩阵中,我希望该图像显示在 ImageView 中:

enter image description here

我尝试过这段代码:

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;
        }
    }

但这返回 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;
            }
        }
    }

和覆盆子端代码:

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)

提前致谢

最佳答案

我不会说有直接的方法,但有可能性。我们可以使用以下方法打印矩阵,然后创建位图。

1) 您在某些 UI 中使用某些 TextView 显示文本,然后使用该 UI 转换为位图,如下所示。

 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绘制文本。然后再次使用该 Canvas 创建位图。

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

关于java - 如何将字符串转换为矩阵,然后转换为位图(Android studio,java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668155/

相关文章:

android - 获取 View 文本(按钮)

javascript - 从文本框获取值 - Android WebView 和 JavascriptInterface 问题

android - 如何让动画效果在 Activity 之间持续存在?

java - 如何在Java中将String的第一个字母大写?

java - 从 Android 7 开始,WebView 显示空白页面

JavaFX:我应该使用具有多个 ImageView 的静态图像吗

android - 如何获取缩放后的图片宽度

Android 在 ImageView 中获取位图的位置

java - Spring Boot (1.3.5) - 默认错误 View

java - 如何查找在@EntityListeners 调用期间更改了哪些字段