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);
}
02-27 12:16:12.979: E/AndroidRuntime(544): FATAL EXCEPTION: main
ReplyDelete02-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
please send me complete program , so that i can look at it.
Deleteplease send me complete program , so that i can look at it.
ReplyDeleteHi, 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.
ReplyDeleteI haven't tested it but i think it is possible.
DeleteRight 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.
I haven't tested it but i think it is possible.
DeleteRight 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.
Hi thanks for your reply...I think I found the answer here it is
Deletehttps://bugs.eclipse.org/bugs/attachment.cgi?id=47890
I have tested it and it works!!
Thanks for reference, i will look at it.
DeleteVpromopKcentpa-1986 Andy Smith https://wakelet.com/wake/41OtnQgPWtkgv5qODSNk8
ReplyDeletebelviatrilaf
neustigPna-ze Jose Kadlecik Download crack
ReplyDeleteinasruduf
rosttiotempna-Scottsdale Melissa White click
ReplyDeleteclick
click here
download
minvidysno
bebuFmis-zu Derek Taniguchi Autodesk 3ds Max
ReplyDeleteCCleaner pro
Internet Download Manager
proddannecon
meviPprovbo Thomas Newman
ReplyDeleteThis is there
maesuncopa
bitlis
ReplyDeletekastamonu
çorum
van
sakarya
H85FJ
ordu evden eve nakliyat
ReplyDeletebursa evden eve nakliyat
konya evden eve nakliyat
osmaniye evden eve nakliyat
bitlis evden eve nakliyat
4E4M
C5E70
ReplyDeleteSinop Şehir İçi Nakliyat
Nevşehir Şehirler Arası Nakliyat
Gümüşhane Şehir İçi Nakliyat
Burdur Şehirler Arası Nakliyat
Mardin Evden Eve Nakliyat
Kırklareli Evden Eve Nakliyat
İzmir Şehir İçi Nakliyat
Kırklareli Şehir İçi Nakliyat
Çankırı Lojistik
F34C3
ReplyDeletedianabol methandienone
Kocaeli Evden Eve Nakliyat
buy oxandrolone anavar
buy testosterone enanthate
Silivri Fayans Ustası
order oxandrolone anavar
Iğdır Evden Eve Nakliyat
buy trenbolone enanthate
Bolu Evden Eve Nakliyat
4B79C
ReplyDeleteBolu Şehir İçi Nakliyat
Muğla Şehir İçi Nakliyat
Keep Coin Hangi Borsada
Tokat Lojistik
Düzce Şehirler Arası Nakliyat
Cate Coin Hangi Borsada
Ünye Çelik Kapı
Isparta Evden Eve Nakliyat
Edirne Lojistik
95777
ReplyDeleteuniswap
defillama
pancakeswap
pancakeswap
spookyswap
sushiswap
dappradar
uwulend finance
poocoin
افضل شركة تسليك مجاري بالاحساء IxuqB8mhoS
ReplyDelete