简体   繁体   English

通过蓝牙将图像从android发送到PC

[英]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. 我正在制作一个应用程序,用于将图像从Android设备发送到在PC上运行的Java应用程序。 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. 客户端(android)上的图像是Bitmap ,为了将其通过蓝牙发送到服务器,我将其转换为Byte Array

 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): 我在服务器(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应用程序)遇到此异常:

java.lang.IllegalArgumentException: im == null! java.lang.IllegalArgumentException:im == null!

at javax.imageio.ImageIO.write(Unknown Source) 在javax.imageio.ImageIO.write(未知源)

at javax.imageio.ImageIO.write(Unknown Source) 在javax.imageio.ImageIO.write(未知源)

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

at java.lang.Thread.run(Unknown Source) 在java.lang.Thread.run(未知来源)

So the ImageIO.read() is not returning anything. 因此ImageIO.read()不返回任何内容。 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. 碰巧,客户端(Android)创建了一个用于接收的线程和一个用于写入的线程。 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. 因此,当我发送图像时,它是成块发送的,即Android OS会不时暂停写入线程,因此服务器端(Java应用程序)上的inputStream看到的是图像成碎片。 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. 因此, ImageIO.read()不能成功读取图像,而是部分读取图像,这就是为什么我得到“ java.lang.IllegalArgumentException:im == null!”的原因,因为仅凭一个图像就无法创建图像块。

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. 在服务器端,在while循环中,我接收所有字节块并将它们全部放在一起,直到收到“文件末尾”。 The code: 代码:

Android client: Android客户端:

    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: Java服务器:

    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. 希望它能帮助到别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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