Image processing using SWT Image APIS

Eclipse SWT Image APIs provides great level of abstraction for all basic image processing operations like scaling , cropping etc.In this post I have provided code snippet to some of the most common image processing operations.
There are main two classes in SWT (org.eclipse.swt.graphics) Image API
  • org.eclipse.swt.graphics.Image
  • org.eclipse.swt.graphics.ImageData
Instances of Image class are graphics which have been prepared for display on a specific device. Instances of ImageData class are device-independent descriptions of images. They are typically used as an intermediate format between loading from or writing to streams and creating an Image.
Following code snippet shows creating Image reference as well as retrieving ImageData from image.
/* creating Image reference using image path */
Image imageRef = new Image(Display.getCurrent(), "image.jpg");
ImageData imageData = imageRef.getImageData();
//processing image data
//..
//creating image using image data
imageRef = new Image(Display.getCurrent(),imaimageDatage);

Following code snippet show scaling image

 
/**
 * An utility method to scale given image.
 * @param img
 * @param scale
 * @return
 */
public static Image scaleImage(Image img, int scale){
    Display display = Display.getCurrent();
    int width = img.getImageData().width;
    int height = img.getImageData().height;
    Image scaled = new Image(display,
            img.getImageData().scaledTo((int)(width*scale),(int)(height*scale)));
    return scaled;
}


Cropping image

private static Image cropImage(Image sourceImage, int x, int y, int height, int width){
    Image croppedImage = new Image(Display.getCurrent(), width, height);
    GC gc = new GC(sourceImage);
    gc.copyArea(croppedImage, x, y);
    gc.dispose();
    return croppedImage;
}

ImageData allows to make background of an image transparent. This can be achieved by marking particular color pixel as transparent. Every image’s color information is represented by color palette. Every image has color palette based on color depth information of image, for example 8 bit depth image will have 2^8 (256) palette. Following code snippet show  making image background as transparent .
//making white background transparent
Image transparentImage = transparentImage(sourceImage, new RGB(255,255,255));
 
/**
*
*/
private static Image transparentImage(Image sourceImage, RGB backgroundColor){
    ImageData data = sourceImage.getImageData();
    if(backgroundColor == null){
        backgroundColor = new RGB(192,192,192); //Grey
    }
    data.transparentPixel = data.palette.getPixel(new RGB(192,192,192));
    return new Image(Display.getCurrent(),data);
}

Comments

  1. 02-27 12:16:12.979: E/AndroidRuntime(544): FATAL EXCEPTION: main
    02-27 12:16:12.979: E/AndroidRuntime(544): java.lang.ExceptionInInitializerError
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display.internal_new_GC(Display.java:1178)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.graphics.Device.init(Device.java:541)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display.init(Display.java:1197)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.graphics.Device.(Device.java:90)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display.(Display.java:287)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display.(Display.java:283)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display.getDefault(Display.java:755)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.widgets.Display$1.run(Display.java:249)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.graphics.Device.getDevice(Device.java:66)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.graphics.Image.(Image.java:772)
    02-27 12:16:12.979: E/AndroidRuntime(544): at com.google.HelloImage.HelloImageActivity.onCreate(HelloImageActivity.java:21)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.Activity.performCreate(Activity.java:4465)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.ActivityThread.access$600(ActivityThread.java:122)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.os.Handler.dispatchMessage(Handler.java:99)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.os.Looper.loop(Looper.java:137)
    02-27 12:16:12.979: E/AndroidRuntime(544): at android.app.ActivityThread.main(ActivityThread.java:4340)
    02-27 12:16:12.979: E/AndroidRuntime(544): at java.lang.reflect.Method.invokeNative(Native Method)
    02-27 12:16:12.979: E/AndroidRuntime(544): at java.lang.reflect.Method.invoke(Method.java:511)
    02-27 12:16:12.979: E/AndroidRuntime(544): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    02-27 12:16:12.979: E/AndroidRuntime(544): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    02-27 12:16:12.979: E/AndroidRuntime(544): at dalvik.system.NativeStart.main(Native Method)
    02-27 12:16:12.979: E/AndroidRuntime(544): Caused by: java.lang.UnsatisfiedLinkError: Couldn't load swt-win32-2128: findLibrary returned null
    02-27 12:16:12.979: E/AndroidRuntime(544): at java.lang.Runtime.loadLibrary(Runtime.java:365)
    02-27 12:16:12.979: E/AndroidRuntime(544): at java.lang.System.loadLibrary(System.java:535)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.internal.Library.loadLibrary(Library.java:104)
    02-27 12:16:12.979: E/AndroidRuntime(544): at org.eclipse.swt.internal.win32.OS.(OS.java:42)
    02-27 12:16:12.979: E/AndroidRuntime(544):
    Plz gave me proper solution...
    I m getting this type of error when i run the above give code

    ReplyDelete
    Replies
    1. please send me complete program , so that i can look at it.

      Delete
  2. please send me complete program , so that i can look at it.

    ReplyDelete
  3. Hi, nice blog by the way is there any ways to join images using only SWT Image APIs. I have been looking for an answer and couldnt find one all I get is to store it as a jpeg and open it into BufferedImage for joining the images.

    ReplyDelete
    Replies
    1. I haven't tested it but i think it is possible.
      Right now i am unaware of it but i will find out information and reply over same blog or past new blog.

      Thanks for referring this blog.

      Regards,
      Yogesh Devatraj.

      Delete
    2. I haven't tested it but i think it is possible.
      Right now i am unaware of it but i will find out information and reply over same blog or past new blog.

      Thanks for referring this blog.

      Regards,
      Yogesh Devatraj.

      Delete
    3. Hi thanks for your reply...I think I found the answer here it is
      https://bugs.eclipse.org/bugs/attachment.cgi?id=47890
      I have tested it and it works!!

      Delete
    4. Thanks for reference, i will look at it.

      Delete

Post a Comment

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester