Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » GC Captcha and all toher stuff
GC Captcha and all toher stuff [message #631724] Fri, 08 October 2010 14:32 Go to next message
Martijn Cremer is currently offline Martijn Cremer
Messages: 77
Registered: January 2010
Location: Breda
Member

Good day,
Its been a while since I posted something but I have been busy with getting my hands dirty with EMF & RAP (and it works Wink).

We also have been playing with the new Canvas. We touch it would be cool use it for a CAPTCHA function. However that was a tad bit disappointing. Here is a code sniplet of our first attempt:

canvas.setLayout(new GridLayout(1, false));
		
		canvas.addPaintListener(new PaintListener() {
			public void paintControl(PaintEvent event) {
				GC gc = event.gc;
				gc.drawImage(image, 0, 0);
				int widthGIF = image.getBounds().width;
				int startPosition = NO_OF_PIX_FROM_BORDER_CANVAS;
				for (int i = 0; i < generatedCAPTCHACode.length(); i++) {
					Font font = new Font(canvas.getDisplay(), FONT_NAME, fontSizeArray[i], fontStyle[i]);
					gc.setFont(font);
					gc.setForeground(canvas.getDisplay().getSystemColor(fontColor[i]));
					char charToPrint = generatedCAPTCHACode.substring(i, i + 1).toCharArray()[0];
					
					// char in pixels : for the same Font, this method sometimes
					// returns different values, so for every redraw, we recalculate.
					int charWidthInPixel = event.gc.getCharWidth(charToPrint);
					
					// char will be printed outside gif
					if (startPosition + NO_OF_PIX_BETWEEN + charWidthInPixel > widthGIF) {
						generatedCAPTCHACode = generatedCAPTCHACode.substring(0, i - 1); // update the generated code
						break;
					}
					gc.drawText(generatedCAPTCHACode.substring(i, i + 1), startPosition, VERTICAL_POSITION, true);
					startPosition = startPosition + NO_OF_PIX_BETWEEN + charWidthInPixel;
				}
			}
		});


This looked realy like a Captcha however if you look at the generated html body. (Using Chrome for example). We discovered that the painted letters would be in a DIF (each letter/character in there own respected dif).

This is of course for a CAPTCHA not something you want. So we went in search of a Captcha library and we found one in the form of (http://jcaptcha.sourceforge.net/) the downside of this is of course that it gives us a AWT image. So wilt some trial and error we created the following code to convert the AWT to SWT in RCP.

static ImageData convertToSWT(BufferedImage bufferedImage) {
		if (bufferedImage.getColorModel() instanceof DirectColorModel) {
			DirectColorModel colorModel = (DirectColorModel) bufferedImage
					.getColorModel();
			PaletteData palette = new PaletteData(colorModel.getRedMask(),
					colorModel.getGreenMask(), colorModel.getBlueMask());
			ImageData data = new ImageData(bufferedImage.getWidth(),
					bufferedImage.getHeight(), colorModel.getPixelSize(),
					palette);
			WritableRaster raster = bufferedImage.getRaster();
			int[] pixelArray = new int[3];
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					raster.getPixel(x, y, pixelArray);
					int pixel = palette.getPixel(new RGB(pixelArray[0],
							pixelArray[1], pixelArray[2]));
					data.setPixel(x, y, pixel);
				}
			}
			return data;
		} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
			IndexColorModel colorModel = (IndexColorModel) bufferedImage
					.getColorModel();
			int size = colorModel.getMapSize();
			byte[] reds = new byte[size];
			byte[] greens = new byte[size];
			byte[] blues = new byte[size];
			colorModel.getReds(reds);
			colorModel.getGreens(greens);
			colorModel.getBlues(blues);
			RGB[] rgbs = new RGB[size];
			for (int i = 0; i < rgbs.length; i++) {
				rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
						blues[i] & 0xFF);
			}
			PaletteData palette = new PaletteData(rgbs);
			ImageData data = new ImageData(bufferedImage.getWidth(),
					bufferedImage.getHeight(), colorModel.getPixelSize(),
					palette);
			data.transparentPixel = colorModel.getTransparentPixel();
			WritableRaster raster = bufferedImage.getRaster();
			int[] pixelArray = new int[1];
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					raster.getPixel(x, y, pixelArray);
					data.setPixel(x, y, pixelArray[0]);
				}
			}
			return data;
		}
		return null;
	}


This works like a charm in RCP however it seems not to give the same results in RAP. I know there are some differences between Image in RCP and RAP (in RCP the Image is extended by Drawable and thus editable by GC). But is there any other way to get the preferred function?

Eny ideas how we could still create CAPTCHA? I mean it would be great to have this.


hm. I've lost a machine.. literally _lost_. it responds to ping, it works completely, I just can't figure out where in my apartment it is.
Re: GC Captcha and all toher stuff [message #631799 is a reply to message #631724] Fri, 08 October 2010 19:40 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan Furnadjiev
Messages: 1839
Registered: July 2009
Senior Member
Hi Martijn,
you could try to draw in an AWT image and then convert the AWT image in
to SWT image.
An example can be found in this attachment:
https://bugs.eclipse.org/bugs/attachment.cgi?id=138591
HTH,
Ivan

On 10/08/2010 5:32 PM, Martijn Cremer wrote:
> Good day,
> Its been a while since I posted something but I have been busy with
> getting my hands dirty with EMF & RAP (and it works ;)).
>
> We also have been playing with the new Canvas. We touch it would be
> cool use it for a CAPTCHA function. However that was a tad bit
> disappointing. Here is a code sniplet of our first attempt:
>
>
> canvas.setLayout(new GridLayout(1, false));
>
> canvas.addPaintListener(new PaintListener() {
> public void paintControl(PaintEvent event) {
> GC gc = event.gc;
> gc.drawImage(image, 0, 0);
> int widthGIF = image.getBounds().width;
> int startPosition = NO_OF_PIX_FROM_BORDER_CANVAS;
> for (int i = 0; i < generatedCAPTCHACode.length(); i++) {
> Font font = new Font(canvas.getDisplay(),
> FONT_NAME, fontSizeArray[i], fontStyle[i]);
> gc.setFont(font);
>
> gc.setForeground(canvas.getDisplay().getSystemColor(fontColo r[i]));
> char charToPrint =
> generatedCAPTCHACode.substring(i, i + 1).toCharArray()[0];
>
> // char in pixels : for the same Font, this method
> sometimes
> // returns different values, so for every redraw,
> we recalculate.
> int charWidthInPixel =
> event.gc.getCharWidth(charToPrint);
>
> // char will be printed outside gif
> if (startPosition + NO_OF_PIX_BETWEEN +
> charWidthInPixel > widthGIF) {
> generatedCAPTCHACode =
> generatedCAPTCHACode.substring(0, i - 1); // update the generated code
> break;
> }
> gc.drawText(generatedCAPTCHACode.substring(i, i +
> 1), startPosition, VERTICAL_POSITION, true);
> startPosition = startPosition + NO_OF_PIX_BETWEEN
> + charWidthInPixel;
> }
> }
> });
>
>
> This looked realy like a Captcha however if you look at the generated
> html body. (Using Chrome for example). We discovered that the painted
> letters would be in a DIF (each letter/character in there own
> respected dif).
>
> This is of course for a CAPTCHA not something you want. So we went in
> search of a Captcha library and we found one in the form of
> (http://jcaptcha.sourceforge.net/) the downside of this is of course
> that it gives us a AWT image. So wilt some trial and error we created
> the following code to convert the AWT to SWT in RCP.
>
>
> static ImageData convertToSWT(BufferedImage bufferedImage) {
> if (bufferedImage.getColorModel() instanceof DirectColorModel) {
> DirectColorModel colorModel = (DirectColorModel)
> bufferedImage
> .getColorModel();
> PaletteData palette = new
> PaletteData(colorModel.getRedMask(),
> colorModel.getGreenMask(), colorModel.getBlueMask());
> ImageData data = new ImageData(bufferedImage.getWidth(),
> bufferedImage.getHeight(), colorModel.getPixelSize(),
> palette);
> WritableRaster raster = bufferedImage.getRaster();
> int[] pixelArray = new int[3];
> for (int y = 0; y < data.height; y++) {
> for (int x = 0; x < data.width; x++) {
> raster.getPixel(x, y, pixelArray);
> int pixel = palette.getPixel(new RGB(pixelArray[0],
> pixelArray[1], pixelArray[2]));
> data.setPixel(x, y, pixel);
> }
> }
> return data;
> } else if (bufferedImage.getColorModel() instanceof
> IndexColorModel) {
> IndexColorModel colorModel = (IndexColorModel) bufferedImage
> .getColorModel();
> int size = colorModel.getMapSize();
> byte[] reds = new byte[size];
> byte[] greens = new byte[size];
> byte[] blues = new byte[size];
> colorModel.getReds(reds);
> colorModel.getGreens(greens);
> colorModel.getBlues(blues);
> RGB[] rgbs = new RGB[size];
> for (int i = 0; i < rgbs.length; i++) {
> rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
> blues[i] & 0xFF);
> }
> PaletteData palette = new PaletteData(rgbs);
> ImageData data = new ImageData(bufferedImage.getWidth(),
> bufferedImage.getHeight(), colorModel.getPixelSize(),
> palette);
> data.transparentPixel = colorModel.getTransparentPixel();
> WritableRaster raster = bufferedImage.getRaster();
> int[] pixelArray = new int[1];
> for (int y = 0; y < data.height; y++) {
> for (int x = 0; x < data.width; x++) {
> raster.getPixel(x, y, pixelArray);
> data.setPixel(x, y, pixelArray[0]);
> }
> }
> return data;
> }
> return null;
> }
>
>
> This works like a charm in RCP however it seems not to give the same
> results in RAP. I know there are some differences between Image in RCP
> and RAP (in RCP the Image is extended by Drawable and thus editable by
> GC). But is there any other way to get the preferred function?
>
> Eny ideas how we could still create CAPTCHA? I mean it would be great
> to have this.
Re: GC Captcha and all toher stuff [message #1022792 is a reply to message #631799] Fri, 22 March 2013 14:34 Go to previous messageGo to next message
Christoph Spielmann is currently offline Christoph Spielmann
Messages: 5
Registered: November 2012
Junior Member
Hi,

I know this is an old thread but i would still be interested in getting it to work. What i have now is code to generate a awt-image and fill it with a captcha. The awt-side seems to be okey because if i save the awt-image i get a png-file with my captcha but somehow during the transfer to an swt-image the color-info is lost and i just get a white image. The code i use for transforming the BufferedImage to a Image-object looks like this.

if (bufferedImage.getColorModel() instanceof DirectColorModel) {
			DirectColorModel colorModel = (DirectColorModel)bufferedImage.getColorModel();
			PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel.getBlueMask());
			ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					int rgb = bufferedImage.getRGB(x, y);
					int pixel = palette.getPixel(new RGB((rgb >> 16) & 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF)); 
					data.setPixel(x, y, pixel);
					if (colorModel.hasAlpha()) {
						data.setAlpha(x, y, (rgb >> 24) & 0xFF);
					}
				}
			}
			return new Image(Display.getCurrent(),data);		
		} else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
			IndexColorModel colorModel = (IndexColorModel)bufferedImage.getColorModel();
			int size = colorModel.getMapSize();
			byte[] reds = new byte[size];
			byte[] greens = new byte[size];
			byte[] blues = new byte[size];
			colorModel.getReds(reds);
			colorModel.getGreens(greens);
			colorModel.getBlues(blues);
			RGB[] rgbs = new RGB[size];
			for (int i = 0; i < rgbs.length; i++) {
				rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
			}
			PaletteData palette = new PaletteData(rgbs);
			ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
			data.transparentPixel = colorModel.getTransparentPixel();
			WritableRaster raster = bufferedImage.getRaster();
			int[] pixelArray = new int[1];
			for (int y = 0; y < data.height; y++) {
				for (int x = 0; x < data.width; x++) {
					raster.getPixel(x, y, pixelArray);
					data.setPixel(x, y, pixelArray[0]);
				}
			}
			return new Image(Display.getCurrent(),data);
		}
		return null;
	  }


I assume that the problem is this
return new Image(Display.getCurrent(),data);
but i haven't found another way to get an Image-method...
Re: GC Captcha and all toher stuff [message #1022814 is a reply to message #1022792] Fri, 22 March 2013 15:10 Go to previous messageGo to next message
Rüdiger Herrmann is currently offline Rüdiger Herrmann
Messages: 565
Registered: July 2009
Senior Member
Christoph,

did you try to write the BufferedImage to an output stream and then
re-load it with the SWT ImageLoader?

HTH
Rüdiger

On 22.03.2013 15:34, Christoph Spielmann wrote:
> Hi,
>
> I know this is an old thread but i would still be interested in getting
> it to work. What i have now is code to generate a awt-image and fill it
> with a captcha. The awt-side seems to be okey because if i save the
> awt-image i get a png-file with my captcha but somehow during the
> transfer to an swt-image the color-info is lost and i just get a white
> image. The code i use for transforming the BufferedImage to a
> Image-object looks like this.
>
>
> if (bufferedImage.getColorModel() instanceof DirectColorModel) {
> DirectColorModel colorModel =
> (DirectColorModel)bufferedImage.getColorModel();
> PaletteData palette = new
> PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(),
> colorModel.getBlueMask());
> ImageData data = new ImageData(bufferedImage.getWidth(),
> bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
> for (int y = 0; y < data.height; y++) {
> for (int x = 0; x < data.width; x++) {
> int rgb = bufferedImage.getRGB(x, y);
> int pixel = palette.getPixel(new RGB((rgb >> 16) &
> 0xFF, (rgb >> 8) & 0xFF, rgb & 0xFF));
> data.setPixel(x, y, pixel);
> if (colorModel.hasAlpha()) {
> data.setAlpha(x, y, (rgb >> 24) & 0xFF);
> }
> }
> }
> return new Image(Display.getCurrent(),data);
> } else if (bufferedImage.getColorModel() instanceof
> IndexColorModel) {
> IndexColorModel colorModel =
> (IndexColorModel)bufferedImage.getColorModel();
> int size = colorModel.getMapSize();
> byte[] reds = new byte[size];
> byte[] greens = new byte[size];
> byte[] blues = new byte[size];
> colorModel.getReds(reds);
> colorModel.getGreens(greens);
> colorModel.getBlues(blues);
> RGB[] rgbs = new RGB[size];
> for (int i = 0; i < rgbs.length; i++) {
> rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF,
> blues[i] & 0xFF);
> }
> PaletteData palette = new PaletteData(rgbs);
> ImageData data = new ImageData(bufferedImage.getWidth(),
> bufferedImage.getHeight(), colorModel.getPixelSize(), palette);
> data.transparentPixel = colorModel.getTransparentPixel();
> WritableRaster raster = bufferedImage.getRaster();
> int[] pixelArray = new int[1];
> for (int y = 0; y < data.height; y++) {
> for (int x = 0; x < data.width; x++) {
> raster.getPixel(x, y, pixelArray);
> data.setPixel(x, y, pixelArray[0]);
> }
> }
> return new Image(Display.getCurrent(),data);
> }
> return null;
> }
>
>
> I assume that the problem is this return new
> Image(Display.getCurrent(),data); but i haven't found another way to get
> an Image-method...


--
Rüdiger Herrmann
http://codeaffine.com
Re: GC Captcha and all toher stuff [message #1022822 is a reply to message #1022814] Fri, 22 March 2013 15:37 Go to previous messageGo to next message
Christoph Spielmann is currently offline Christoph Spielmann
Messages: 5
Registered: November 2012
Junior Member
I've tested your suggestion with the following code-snippet:

ImageLoader loader=new ImageLoader();
ImageData[] datas=loader.load("/tmp/test.png");
return new Image(Display.getCurrent(),datas[0]);


I save the awt-image like this:

ImageIO.write(awtImage, "PNG", new File("/tmp/test.png"));


the ImageLoader seems to be able to read the png-file (datas contains one object) but still the resulting image is empty.

[Updated on: Fri, 22 March 2013 15:37]

Report message to a moderator

Re: GC Captcha and all toher stuff [message #1022831 is a reply to message #1022822] Fri, 22 March 2013 15:52 Go to previous message
Rüdiger Herrmann is currently offline Rüdiger Herrmann
Messages: 565
Registered: July 2009
Senior Member
Hm, I thought I used the save to stream and reload approach somewhere
successfully, only I can't find the code any more.

However, did you derive your code from the SWT image conversion snippet
[1]? If not you may have a look at it so see if there is any difference.

Rüdiger

[1]
http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet156.java

On 22.03.2013 16:37, Christoph Spielmann wrote:
> I've tested your suggestion with the following code-snippet:
>
>
> ImageLoader loader=new ImageLoader();
> ImageData[] datas=loader.load("/tmp/test.png");
> return new Image(Display.getCurrent(),datas[0]);
>
>
> I write out the awt-image like this:
> ImageIO.write(awtImage, "PNG", new File("/tmp/test.png"));
>
> the ImageLoader seems to be able to read the png-file (datas contains
> one object) but still the resulting image is empty.--
Rüdiger Herrmann
http://codeaffine.com
Previous Topic:RAP ApplicationConfiguration and automed WorkbenchApplicationConfigurator
Next Topic:Weird situation - Icons disappearing
Goto Forum:
  


Current Time: Thu Oct 23 13:58:56 GMT 2014

Powered by FUDForum. Page generated in 0.01854 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software