element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi
  • Products
  • More
Raspberry Pi
Raspberry Pi Forum Transfer Image from Raspberry Pi (Python) to Android app (Java)
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi to participate - click to join for free!
Featured Articles
Announcing Pi
Technical Specifications
Raspberry Pi FAQs
Win a Pi
Raspberry Pi Wishlist
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 2 replies
  • Subscribers 663 subscribers
  • Views 2462 views
  • Users 0 members are here
  • image processing
  • raspberry-pi-b+
  • python;
  • socket
  • android image
  • send image
  • raspberry-pi
  • raspberry_pi_space
  • android studio
  • tcp
  • android app
  • programming help
  • java
Related

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

programmer.3
programmer.3 over 6 years ago

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 an 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 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 Buffered Reader (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 around 2000-3000 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 that 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 base64 encoding/decoding but that also kept producing errors. I have tried rpi only sending 1024 bytes of the image at a time but that also did not work. 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):

//receives the message which the server sends back 
InputStream sin = socket.getInputStream();
OutputStream sout = socket.getOutputStream();
DataInputStream in = new DataInputStream(sin); 
mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));  

//in this while the client listens for the messages sent by the server
while (mRun) { 
     mServerMessage = mBufferIn.readLine(); 
     if (mServerMessage != null && mMessageListener != null) { 
          //Check if data is image
          if(mServerMessage.equals("?start")) { 
               // Get length of image byte array
               int size = Integer.parseInt(mBufferIn.readLine());  
        
               // Create buffers
               byte[] msg_buff = new byte[1024];
               byte[] img_buff = new byte[size];
               int img_offset = 0;

               while(true){
                    int bytes_read = in.read(msg_buff, 0, msg_buff.length); 
                    if(bytes_read == -1){ break; } 

                    //copy bytes into img_buff
                    System.arraycopy(msg_buff, 0, img_buff, img_offset, bytes_read);
                    img_offset += bytes_read;      
                    if( img_offset >= size) { break; }            
               } 
               ContextWrapper cw = new ContextWrapper(ApplicationContextProvider.getContext());     
               File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
               File mypath = new File(directory, "signal.jpeg"); 
               Bitmap bitmap = BitmapFactory.decodeByteArray(img_buff, 0, img_buff.length);
               FileOutputStream fos = null; 
               try{     
                    fos = new FileOutputStream(mypath);                
                    //Use compress method on Bitmap object to write image to OutputStream               
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);      
                    //Send OK byte[]          
                    OK = new byte[] {0x4F, 0x4B};     
                    sout.write(OK);
                }   catch (Exception e) {
                    e.printStackTrace();
               }                                            
     }
}

Raspberry Pi (python):

def image_to_byte_array(image_file, conn): 
     with open(image_file, 'rb') as imageFile:
          content = imageFile.read()
          conn.sendall("?start\n".encode('utf-8'))
          size = len(content) strSize = str(size) + "\n"
          conn.sendall(strSize.encode('utf-8'))
          conn.sendall(content)

Note that conn is the connection between the app and the rpi and the images are PNG.

If anyone knows why this isn't working or has a better way for me to do this, I would greatly appreciate it!! Thank you in advance!! image     

  • Sign in to reply
  • Cancel
Parents
  • clem57
    0 clem57 over 6 years ago

    I would keep the image on the RPI and setup a web server on the rpi. On the app, use browser or api to get the picture or image.

    Try https://www.tomshardware.com/news/raspberry-pi-web-server,40174.html

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • clem57
    0 clem57 over 6 years ago in reply to clem57

    Did this help? programmer.3

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • clem57
    0 clem57 over 6 years ago in reply to clem57

    Did this help? programmer.3

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube