简体   繁体   中英

How do I convert an ImageIcon to BitmapDrawable in Android?

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

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

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 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");


        }

    }

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

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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