swt byte[] 与 Image的转换

1. 从byte[]得到Image 

  1. private static Image createImage(byte[] imageBytes) {  
  2.     Image image = null;  
  3.     try {  
  4.         ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);  
  5.         image = new Image(null, bais);  
  6.     } catch (Exception e) {  
  7.         e.printStackTrace();  
  8.     }  
  9.     return image;  
  10. }  

2. 从Image得到byte[] 

public static byte[] getImageBytes(Image image) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save(baos, image.type);

byte[] imageByteArray = baos.toByteArray();
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}

return imageByteArray;
}

原文地址:https://www.cnblogs.com/alamps/p/4720018.html