【问题标题】:How do I convert an ImageIcon to BitmapDrawable in Android?如何在 Android 中将 ImageIcon 转换为 BitmapDrawable?
【发布时间】:2013-05-01 23:25:12
【问题描述】:

我有代码在 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。

【问题讨论】:

  • 为什么不使用 Bitmap API 而不是 BufferedImage 和 ImageIcon?
  • 你为什么从两个不同的账户问同一个问题(同一个问题主体)?
  • 因为我们正在做的是同一个项目

标签: java android sockets


【解决方案1】:

一侧有 Java awt 部分,另一侧有 Android 部分。这是行不通的。您需要一种中间格式,例如 png 或 jpg。你可以compress the image to bytes,发送这些,和decode them on the other side
此外,对象序列化在 Android 上非常慢...

【讨论】:

  • 我对 java 和 android 很陌生,所以你能帮我写一下 java 端和 android 端的代码吗?
  • 当然,如果你有一个具体的问题,但现在我应该做我的历史作业:D
  • 如果你有一段时间没有解决它,请再问一次,但它并不难,谷歌就可以了......
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 2014-03-01
  • 2015-02-02
  • 2013-02-09
  • 2015-06-17
  • 2015-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多