473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Image Transfer from Raspberry Pi (in Python) to Android app (Java)

Hi everyone! I need help transferring an image via TCP from a python program on my raspberry pi to an android application.

I have set up a client-server architecture such that my raspberry pi 3 records audio, performs some analysis on it, and then sends the data (via TCP) to the android app to display on the app screen. The recording and analysis is done and I am able to make the connection and transfer string data that displays on the app with no problem. However, I have been unsuccessful in transferring an image from rpi to android app. So basically, the image is stored on the rpi and I am attempting to transfer the image to the app to display it. I have been working on this for over a week with no luck so any help would be greatly appreciated!

My current implementation (code snippets provided below):

On rpi (python): Like I said, sending strings and displaying them on the android app is done without any problem. When I am sending the image portion of the audio analysis, I send a string first that says "?start" so that the android side knows that an image instead of a string is about to be sent (and will wait to update the GUI until it receives the entire image). Then, I open the image stored on rpi and read the entire image as a byte array (typically about 40-50k bytes). I get the length of the byte array and send that as a string to the android app. Finally, I send the byte array to the android and it waits for an OK message from the app. All of this works without reporting any errors.

On android app (java): When the app receives the "?start" string, it then uses a BufferedReader (which is what I used to read the string data I had transferred to the app successfully earlier) to read the size of the image byte array. Then, I create 2 buffers, msg_buff and img_buff. msg_buff will read in 1024 bytes at a time while img_buff will hold the entire byte array of the image. In the infinite while loop, I have a DataInputStream, called in, read bytes into msg_buff and returns the number of bytes read. Then, I concatenate the copied contents of msg_buff into img_buff. Once the bytes read from in is -1 or the img_offset (which is just the total number of bytes read) is greater than or equal to the size of the image bytes array, the while loop is broken. Then, I would attempt to save the image to android internal storage and then load it later to an imageView to display it. This code does successfully read in the bytes until there are about 2-3k bytes left to be read and then it seems to freeze on the int bytes_read = in.read(msg_buff, 0, msg_buff.length) line. I have not been able to get past this point so I do not know if saving the image to internal storage and then loading it to imageView that way will work either.

I have also tried using readFully() function with DataInputStream but it also freezes on that line. I have tried using base64 encoding/decoding and having the rpi send only 1024 bytes of the image at a time but neither of those worked. I have tried several implementations of this approach but nothing has worked so far. If anyone sees anything wrong or has another approach, I am all ears!

Android Studio (app side):

Expand|Select|Wrap|Line Numbers
  1. //receives the message which the server sends back 
  2. InputStream sin = socket.getInputStream(); 
  3. OutputStream sout = socket.getOutputStream(); 
  4. DataInputStream in = new DataInputStream(sin);  
  5. mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
  6.  
  7. //in this while the client listens for the messages sent by the server 
  8. while (mRun) {  
  9.      mServerMessage = mBufferIn.readLine();  
  10.      if (mServerMessage != null && mMessageListener != null) {  
  11.           //Check if data is image 
  12.           if(mServerMessage.equals("?start")) {  
  13.                // Get length of image byte array 
  14.                int size = Integer.parseInt(mBufferIn.readLine());   
  15.  
  16.                // Create buffers 
  17.                byte[] msg_buff = new byte[1024]; 
  18.                byte[] img_buff = new byte[size];
  19.                int img_offset = 0; 
  20.  
  21.                while(true){ 
  22.                     int bytes_read = in.read(msg_buff, 0, msg_buff.length);  
  23.                     if(bytes_read == -1){ break; }  
  24.  
  25.                     //copy bytes into img_buff 
  26.                     System.arraycopy(msg_buff, 0, img_buff, img_offset, bytes_read); 
  27.                     img_offset += bytes_read;       
  28.                     if( img_offset >= size) { break; }             
  29.                }  
  30.                ContextWrapper cw = new ContextWrapper(ApplicationContextProvider.getContext());      
  31.                File directory = cw.getDir("imageDir", Context.MODE_PRIVATE); 
  32.                File mypath = new File(directory, "signal.jpeg");  
  33.                Bitmap bitmap = BitmapFactory.decodeByteArray(img_buff, 0, img_buff.length); 
  34.                FileOutputStream fos = null;  
  35.                try{      
  36.                     fos = new FileOutputStream(mypath);                 
  37.                     //Use compress method on Bitmap object to write image to OutputStream                
  38.                     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);       
  39.                     //Send OK byte[]           
  40.                     OK = new byte[] {0x4F, 0x4B};      
  41.                     sout.write(OK); 
  42.                 }   catch (Exception e) { 
  43.                     e.printStackTrace(); 
  44.                }                                             
  45.      }
  46. }
  47.  
Raspberry Pi (python):

Expand|Select|Wrap|Line Numbers
  1. def image_to_byte_array(image_file, conn): 
  2.      with open(image_file, 'rb') as imageFile: 
  3.           content = imageFile.read() 
  4.           conn.sendall("?start\n".encode('utf-8')) 
  5.           size = len(content) 
  6.           strSize = str(size) + "\n" 
  7.           conn.sendall(strSize.encode('utf-8')) 
  8.           conn.sendall(content)
  9.  
Note that conn is the connection between the app and the rpi and the images are PNG on the rpi.
If anyone knows why this isn't working or has a better way for me to do this, I would greatly appreciate it!! I have already spent more than 40 hours trying to fix this so thank you in advance! :)
Jul 22 '19 #1
0 2450

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: liorm | last post by:
Hi everyone, I need to write a web app, that will support millions of user accounts, template-based user pages and files upload. The client is going to be written in Flash. I wondered if I coudl...
6
by: JKPeck | last post by:
Suppose you have an application written in Java, and you want to enable other applications or processes written in Python to communicate with it, i.e., to use Python as a scripting language for the...
8
by: Aravind | last post by:
hi, some of my friends told that python and java are similar in the idea of platform independency. Can anyone give me an idea as i'm a newbie to java and python but used to C++. My idea is to...
2
by: Clayton | last post by:
Hi all, Can anyone give me a brief description of what I need to do to transfer an image from a client/server application using C#? When I googled, I found that first I need to convert the image...
34
by: Anthony Irwin | last post by:
Hi All, I am currently trying to decide between using python or java and have a few quick questions about python that you may be able to help with. #1 Does python have something like javas...
2
by: pdesai007 | last post by:
hi all, i am trying to upload a image on to server with asp and AJAX, but it is not worked. so please give me information that upload an image to the server with java script without...
1
by: datulaida | last post by:
Hi.. How to connect Python with Java. This is my Python program. i use jpype to connect Python with Java. Code: ( text ) 1. from jpype import * 2. startJVM("d:/Program...
1
by: povs937 | last post by:
I am trying to send the screenshots of the pc(java server) to android(client) using sockets. I am not able to do this task. Well, also I have successfully send the screenshots from pc(java server)...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.