简体   繁体   中英

Sending image from android to PC via bluetooth

I'm making an app to send an image from an android device to a java app running on a PC. The image on the client side (android) is a Bitmap and I convert it to a Byte Array in order to send it to the server via bluetooth.

 ByteArrayOutputStream baos = new ByteArrayOutputStream();  
 ImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     
 byte[] b = baos.toByteArray();
 mBluetoothService.write(b);

Note that the bitmap comes from an already compressed file, so I don't need to compress it again.

I use the following code on the server (Java):

  byte[] buffer = new byte[1024*1024];
  int bytes;
  bytes = inputStream.read(buffer);
  ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
  BufferedImage image = ImageIO.read(bais);
  ImageIO.write(image, "jpg", new File("c:/users/image.jpg"));

There are no error on the client side. But I get this exception on the server side (java app):

java.lang.IllegalArgumentException: im == null!

at javax.imageio.ImageIO.write(Unknown Source)

at javax.imageio.ImageIO.write(Unknown Source)

at com.luugiathuy.apps.remotebluetooth.ProcessConnectionThread.run(ProcessConnectionThread.java:68)

at java.lang.Thread.run(Unknown Source)

So the ImageIO.read() is not returning anything. It seems like it doesn't recognize the byte array as an image. I have searched on internet but nothing that helps me solve this. Does anyone have any idea?

Many thanks!!

I could finally figure it out! It happens that the client (Android) creates a thread for receiving and a thread for writing. So, when I send an image, it's sent in chunks, ie the writing threads is paused every now and then by the Android OS, so what the inputStream on the server side (Java app) sees is that the image is coming in pieces. So, ImageIO.read() is not successfully reading an image but a piece of it, and that's why I'm getting the "java.lang.IllegalArgumentException: im == null!", cause no image can be created with just a chunk.

Solution:

Besides the image, I also send an "end of file" string to the server, so it knows when the file is complete (I suppose there are better ways to to this, but this is working). At the server side, in a while loop I receive all the byte chunks and put them all together until an "end of file" is received. The code:

Android client:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);     
    byte[] b = baos.toByteArray();
    mBluetoothService.write(b);
    mBluetoothService.write("end of file".getBytes());

Java server:

    byte[] buffer = new byte[1024];

    File f = new File("c:/users/temp.jpg");
    FileOutputStream fos = new FileOutputStream (f);

    int bytes = 0;
    boolean eof = false;

    while (!eof) {

        bytes = inputStream.read(buffer);
        int offset = bytes - 11;
        byte[] eofByte = new byte[11];
        eofByte = Arrays.copyOfRange(buffer, offset, bytes);
        String message = new String(eofByte, 0, 11);

        if(message.equals("end of file")) {

            eof = true;

        } else {

            fos.write (buffer, 0, bytes);

        }

    }
    fos.close();

Hope it helps somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM