开发者

SWT Image to/from String

开发者 https://www.devze.com 2023-03-09 06:07 出处:网络
I am trying to convert an SWT Image to String and vice versa: To String: Display display = new Display();

I am trying to convert an SWT Image to String and vice versa:

To String:

Display display = new Display();
final Image image = new Image(display, "c:\test.png");
 // Looks good
showImage(image ,600,400);
ImageData imageData = testImage.getImageData();
byte[] data = imageData.data;
String imageString = new String(Base64.encode(data));

Back to image:

byte[] decode = Base64.decode(imageString.getBytes());
decode.toString();
Image c_img = new Image(Display.getCurrent(), stringToInputStream(decode.toString()));
 // Throws exception.
showImage(c_image ,600,400);

where:

  private static void showImage(final Image image, int w, int h) {
    Display display = new Display();
    Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.DOUBLE_BUFFERED);
    shell.setLayout(new FillLayout());
    shell.addListener(SWT.Paint, new Listener() {
      public void handleEvent(Event e) {
        GC gc = e.gc;
        int x = 10, y = 10;
        gc.drawImage(image, x, y);
        gc.dispose();
      }
    });
    shell.setSize(w, h);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    if (image != null && !image.isDisposed()) {
      image.dispose();
    }
    display.dispose();
  }


  private static InputStream stringToInputStream(String input) {
    InputStream is = null;
    try {
      is = new ByteArrayInputStream(input.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return is;
  }

The error when showing the image from String is:

org.eclipse.swt.SWTException: Unsupported or unrecognized format
    at org开发者_高级运维.eclipse.swt.SWT.error(SWT.java:4083)
    at org.eclipse.swt.SWT.error(SWT.java:3998)
    at org.eclipse.swt.SWT.error(SWT.java:3969)
    at org.eclipse.swt.internal.image.FileFormat.load(FileFormat.java:82)
    at org.eclipse.swt.graphics.ImageLoader.load(ImageLoader.java:130)
    at org.eclipse.swt.graphics.ImageDataLoader.load(ImageDataLoader.java:22)
    at org.eclipse.swt.graphics.ImageData.<init>(ImageData.java:331)
    at org.eclipse.swt.graphics.Image.<init>(Image.java:545)

Am I the first to do this? Or are there some examples showing how this should be done properly?


Certainly this is wrong:

stringToInputStream(decode.toString())

If you call toString() on a byte array, you don't get the bytes converted to a String; you get a String that looks like

byte[@6536753

You need to construct the ByteArrayInputStream from "decode" itself.


Why you're operating with String? Is byte[] is not good for you? You're adding useless step in your operations converting it.


If someone needs it, I have achieved it with help of this link: http://www.programcreek.com/2009/02/java-convert-image-to-byte-array-convert-byte-array-to-image/

After a lot of tries, using the suggested approach of the question, or also using ImageLoader, I was always getting:

org.eclipse.swt.SWTException: Unsupported or unrecognized format

But with the approach of the link (using Base64.decode and encode) it works.

0

精彩评论

暂无评论...
验证码 换一张
取 消