【问题标题】:How to convert String to matrix and then to a Bitmap (Android studio, java)如何将字符串转换为矩阵,然后转换为位图(Android Studio,Java)
【发布时间】:2020-01-09 16:18:24
【问题描述】:

我有一个树莓派,我正在尝试使用套接字将 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]]]

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

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

我试过这段代码:

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)

提前致谢

【问题讨论】:

    标签: java android imageview android-bitmap


    【解决方案1】:

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

    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

    【讨论】:

    • 对不起,如果我误解了答案,但我有一个 640 x 480 图像的原始像素值矩阵字符串。我已经编辑了这个问题,你可以看到正在接收什么数据。我认为这个答案绘制了 imageView 的一些文本。对吗?
    • 我猜你想将矩阵的字符串/整数显示到一些位图中。我的代码说在某些文本视图中使用该字符串矩阵在视图中显示,然后将视图转换为位图。否则创建这么多矩阵可能会在性能方面花费很多。所以只需将此字符串设置为文本视图,可以使用上面的代码将其转换为一些位图。
    • 我再次编辑了问题,我不想在图像视图中显示文本,我想将原始像素值转换为立体图像,见上文。谢谢
    猜你喜欢
    • 2022-01-03
    • 1970-01-01
    • 2019-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2019-10-10
    相关资源
    最近更新 更多