通过蓝牙发送,从Android的图像PC [英] Sending image from android to PC via bluetooth

查看:217
本文介绍了通过蓝牙发送,从Android的图像PC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个应用程序从Android设备发送图像到一个Java应用程序在PC上运行。在客户端(安卓)的图像是一个位图,并把它转换成一个字节数组以发送它通过蓝牙服务器。

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

请注意,该位是来自一个已经融为一体pressed文件,所以我不需要COM preSS一遍。

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

我用下面的code服务器(JAVA)的:

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

有在客户端没有错误。不过,我得到这个例外在服务器端(Java应用程序):

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

java.lang.IllegalArgumentException异常:IM == NULL

java.lang.IllegalArgumentException: im == null!

在javax.imageio.ImageIO.write(来源不明)

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

在javax.imageio.ImageIO.write(来源不明)

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

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

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

在java.lang.Thread.run(来源不明)

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

因此​​, ImageIO.read()不返回任何东西。现在看来似乎不承认的字节数组作为图像。我搜索在互联网上,但没有什么可以帮助我解决这个问题。没有人有任何想法?

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?

非常感谢!!

推荐答案

我终于可以看着办吧!它发生在客户端(安卓)创建一个线程用于接收和线程进行写操作。所以,当我发送图像,它发送的数据块,我。即现在,然后写线程暂停每次由Android操作系统,还等什么,在服务器端(Java应用)的InputStream看到的是,图片即将在片。因此, ImageIO.read()未成功读取图像,但一小部分,这就是为什么我得到了java.lang.IllegalArgumentException:如果IM ==空!,因为没有图像可以只用一大块被创建。

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, i. e. 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.

解决方法:

除了图像,我也送字符串到服务器的文件结束,所以它知道什么时候该文件是完整的(我认为是有向这更好的方法,但是这是工作)。在服务器端,在一个while循环我收到的所有字节块,并把它们放在一起,直到接收到一个文件结束。在code:

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客户端:

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

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.

这篇关于通过蓝牙发送,从Android的图像PC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆