[英]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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.