通过蓝牙

时间:2016-04-22 07:37:11

标签: java android bluetooth

我正在尝试实现一个简单的文件传输应用程序。

我的应用程序确实如此: 1.在Android中捕获当前的相机预览 2.通过蓝牙将其发送到Javafx应用程序 3.当Javafx应用程序收到图像时将其保存并显示在窗口上 4.在对图像进行一些绘制后,将其再次发送到Android

我首先在Android端实现了这样的

我创建了一种包含文件大小,实际数据和eof的数据包。

ByteArrayOutputStream baos = new ByteArrayOutputStream();
    data.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    // filesize + data + end of file
    String fileSize = String.valueOf(baos.size());
    fileSize += '\0'; // end of the filesize string
    String eof = "eof"; // end of packet
    // set packet size
    int packetSize = fileSize.length() + baos.size() + eof.length();
    ByteBuffer byteBuffer = ByteBuffer.allocate(packetSize);
    byteBuffer.put(fileSize.getBytes());
    byteBuffer.put(baos.toByteArray());
    byteBuffer.put(eof.getBytes());

    byte[] data = new byte[byteBuffer.capacity()];
    byteBuffer.position(0);
    byteBuffer.get(data);

并将数据发送到蓝牙输出流插座

在JavaFX方面,

bytes = btIn.read(buffer);
                // receive packet data
                if (fileSize == null) {
                    for (int i = 0; i < bytes; i++) {
                        if (buffer[i] == '\0') {
                            fileSize = new String(buffer, 0, i);
                            break;
                        }
                    }
                }

                // eof offset
                int offset = bytes - 3;
                byte[] eofByte = new byte[3];
                eofByte = Arrays.copyOfRange(buffer, offset, bytes);
                String message = new String(eofByte, 0, 3);

                if (message.equals("eof")) {
                    eof = true;
                    fos.write(buffer, 0, bytes-3);
                } else {
                    // set buffer size to file size
                    if (fileBuffer == null) {
                        fileBuffer = ByteBuffer.allocate(Integer.parseInt(fileSize));
                        fileBuffer.put(buffer, fileSize.length()+1, bytes);
                        fos.write(buffer, fileSize.length()+1, bytes);
                    } else {
                        fileBuffer.put(buffer, 0, bytes);
                        fos.write(buffer, 0, bytes);
                        // for progress bar
                        percent = (float) fileBuffer.position() / (float) fileBuffer.capacity();
                    }
                }

                log(String.valueOf(percent));

                if (eof) {
                    byte[] data = new byte[fileBuffer.capacity()];
                    fileBuffer.position(0);
                    fileBuffer.get(data);
                    ByteArrayInputStream input = new ByteArrayInputStream(data);
                    bufferedImage = ImageIO.read(input);
                    image = SwingFXUtils.toFXImage(bufferedImage, null);

                    if (bufferedImage != null) {
                        log("got the image");
                    }

                    // set null fileBuffer and fileSize for next images
                    fileSize = null;
                    fileBuffer = null;
                }
            }

以上代码用于从Anroid中接收图像

并且在JavaFX上发送部分是:

            WritableImage writableImage = new WritableImage((int)canvas.getWidth(), (int)canvas.getHeight());
            canvas.snapshot(null, writableImage);
            BufferedImage bufferedImage = SwingFXUtils.fromFXImage(writableImage, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
                ImageIO.write(bufferedImage, "jpg", baos);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            byte[] imageInBytes = baos.toByteArray();
            log(String.valueOf(imageInBytes.length));
            bct.write(imageInBytes);
            String eof = "end of file";
            byte[] eofbyte = eof.getBytes();
            bct.write(eofbyte);

发送和接收部分工作正常.. 但我在结果图像上遇到了问题

enter image description here

这是JavaFX方面,当你收到Android和你看到的图像时,图像的最左侧是不希望的

从JavaFX端获取图像后,在Android上的结果图像就像这样:

enter image description here

我的问题是如何修复代码以获取正确的图像?

1 个答案:

答案 0 :(得分:1)

我认为你遇到了这个错误:

https://bugs.openjdk.java.net/browse/JDK-8041459

您可以使用PNG而不是JPG来避免此问题。另一个选择是在存储图像之前将图像显式转换为没有alpha分量的图像。

迈克尔