Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. Display display = new Display();
  2. final Image image = new Image(display, "c:test.png");
  3. // Looks good
  4. showImage(image ,600,400);
  5. ImageData imageData = testImage.getImageData();
  6. byte[] data = imageData.data;
  7. String imageString = new String(Base64.encode(data));
  8.  
  9. byte[] decode = Base64.decode(imageString.getBytes());
  10. decode.toString();
  11. Image c_img = new Image(Display.getCurrent(), stringToInputStream(decode.toString()));
  12. // Throws exception.
  13. showImage(c_image ,600,400);
  14.  
  15. private static void showImage(final Image image, int w, int h) {
  16. Display display = new Display();
  17. Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
  18. shell.setLayout(new FillLayout());
  19. shell.addListener(SWT.Paint, new Listener() {
  20. public void handleEvent(Event e) {
  21. GC gc = e.gc;
  22. int x = 10, y = 10;
  23. gc.drawImage(image, x, y);
  24. gc.dispose();
  25. }
  26. });
  27. shell.setSize(w, h);
  28. shell.open();
  29. while (!shell.isDisposed()) {
  30. if (!display.readAndDispatch())
  31. display.sleep();
  32. }
  33. if (image != null && !image.isDisposed()) {
  34. image.dispose();
  35. }
  36. display.dispose();
  37. }
  38.  
  39.  
  40. private static InputStream stringToInputStream(String input) {
  41. InputStream is = null;
  42. try {
  43. is = new ByteArrayInputStream(input.getBytes("UTF-8"));
  44. } catch (UnsupportedEncodingException e) {
  45. e.printStackTrace();
  46. }
  47. return is;
  48. }
  49.  
  50. org.eclipse.swt.SWTException: Unsupported or unrecognized format
  51. at org.eclipse.swt.SWT.error(SWT.java:4083)
  52. at org.eclipse.swt.SWT.error(SWT.java:3998)
  53. at org.eclipse.swt.SWT.error(SWT.java:3969)
  54. at org.eclipse.swt.internal.image.FileFormat.load(FileFormat.java:82)
  55. at org.eclipse.swt.graphics.ImageLoader.load(ImageLoader.java:130)
  56. at org.eclipse.swt.graphics.ImageDataLoader.load(ImageDataLoader.java:22)
  57. at org.eclipse.swt.graphics.ImageData.<init>(ImageData.java:331)
  58. at org.eclipse.swt.graphics.Image.<init>(Image.java:545)
  59.  
  60. stringToInputStream(decode.toString())
  61.  
  62. byte[@6536753
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement