繁体   English   中英

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

[英]How do I convert an ImageIcon to BitmapDrawable in Android?

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

问题是当将该文件发送到android时无法将其读取为可绘制的位图。 有人对此有答案吗?

发送代码(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。

一侧有Java awt部分,另一侧有Android部分。 这是行不通的。 您需要一种中间格式,例如png或jpg。 您可以将图像压缩为字节 ,进行发送,然后在另一端进行解码
而且,对象序列化在Android上真是太慢了...

暂无
暂无

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

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