0

I have a raspberry pi, from which I am trying to send 2 stereo image using sockets to an android device. I have written the code for the server socket to send the images at the raspberry pi, I even receive it at the android end. But now I want to show it in ImageViews. How should I convert a string to a 640 x 480 x 3 int matrix, and then to Bitmap? Or is there a way to convert the string directly to a Bitmap?

Edit: I have a string of a matrix of raw pixel values like:

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

*Not full array, I am receiving full array without the dots.

From this matrix I want this image to be displayed in a imageview:

enter image description here

I have tried this code:

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

But this returns null

My Code (receiving class):

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

And Raspberry Side code:

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)

Thanks in advance

Arush Gupta
  • 81
  • 1
  • 7

1 Answers1

0

I won't say there is a direct way but there are possibilities. we can use below methods to print the matrix and then create a bitmap.

1) You display the text using some TextView in some UI and then use that UI to convert to bitmap like below.

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

or you can use the canvas to draw the text using paint. then again use that canvas to create bitmap.

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

More info goes here

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
  • Sorry if I misunderstood the answer, but I have a string of a matrix of raw Pixel Values of a 640 x 480 image. I have edited the question, you can see what data is being received. I think this answer draws some text of the imageView. Right? – Arush Gupta Jan 09 '20 at 17:04
  • i guess you want to show the string / Int of matrix into some bitmap. my code says use that string matrix in some textview to show in the view and then convert the view to bitmap. otherwise creating this much matrix may cost u a lot in terms of performance. so just take this string and set into text view which can be converted into some bitmap using above code. – vikas kumar Jan 09 '20 at 17:19
  • I have edited the question again, I do not want to show the text in an imageview, I want to convert the raw pixel values in to an stereo image, See above. Thanks – Arush Gupta Jan 10 '20 at 05:53