如何在Android中将ImageIcon转换为BitmapDrawable?

时间:2013-05-01 15:06:49

标签: java android sockets

我有从java中的屏幕捕获图像的代码,我将最终捕获的图像作为BufferedImage对象并可以将其转换为ImageIcon

问题是当将该文件发送到android时无法将其读取为位图drawable。有谁回答过这个问题?

要发送的代码(Java)

  BufferedImage image = robot.createScreenCapture(rectangle);
    ImageIcon imageIcon = new ImageIcon(image);

    //Send captured screen to the server
    try {
        System.out.println("before sending image");      

        oos.writeObject(imageIcon);
        oos.reset(); //Clear ObjectOutputStream cache
        System.out.println("New screenshot sent");
    } catch (IOException ex) {
       ex.printStackTrace();
    }

Android接收器部件

Thread t= new Thread(new Runnable() {

    @Override
    public void run() {
        while (true) {


            try {

                client= sc.accept();
                is = client.getInputStream();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            BitmapDrawable imageIcon = null;

            try {
                ois = new ObjectInputStream(is);
                imageIcon = (BitmapDrawable) ois.readObject();
                //Drawable d = Drawable.createFromStream(is, null);
                IV.setImageDrawable(imageIcon);
            } catch (OptionalDataException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("New image recieved");


        }

    }

我得到的例外是它无法将imageIcon或BufferedImage转换为Bitmap drawable。

1 个答案:

答案 0 :(得分:0)

你在一边有Java awt部分,在另一边有Android部分。这不会起作用。你需要一个中间格式,比如png或者jpg。您可以compress the image to bytes,发送这些内容和decode them on the other side 此外,Android上的对象序列化速度非常慢......