我怎么一个ImageIcon转换为BitmapDrawable在Android的? [英] How do I convert an ImageIcon to BitmapDrawable in Android?

查看:145
本文介绍了我怎么一个ImageIcon转换为BitmapDrawable在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code在java中捕获图像从屏幕上,我有最后捕捉的图像以BufferedImage对象,并可以将其转换为的ImageIcon

I have the code to capture image from screen in java, I have the final captured image as BufferedImage object and Can cast it to ImageIcon

发送该文件到Android无法读取它作为位图绘制时出了问题。任何一个有答案吗?

The problem is when sending that file to android can't read it as bitmap drawable. Any one have answer to this ?

code派(JAVA)

Code to send (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的接收器部分。

Android Receiver Part

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中位图绘制。

I get the exception of it can't cast the imageIcon or the BufferedImage to Bitmap drawable.

推荐答案

您已经在一边了Java AWT部分,并在其他Android的一部分。这是行不通的。你需要一个中间格式,如PNG或JPG。您可以 COM preSS的形象字节,把这些和德code他们在另一边。结果
此外,对象序列化是Android上的该死的慢...

You have the Java awt part on one Side, and the Android part on the other. This is not going to work. You need an intermediary format, like png or jpg. You can compress the image to bytes, send these, and decode them on the other side.
Also, Object serialization is damn slow on Android...

这篇关于我怎么一个ImageIcon转换为BitmapDrawable在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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