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 10:32  |
Eclipse User |
|
|
|
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(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.
|
|
|
Re: GC Captcha and all toher stuff [message #631799 is a reply to message #631724] |
Fri, 08 October 2010 15:40   |
Eclipse User |
|
|
|
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 10:34   |
Eclipse User |
|
|
|
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 11:10   |
Eclipse User |
|
|
|
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
|
|
| | |
Goto Forum:
Current Time: Wed Jul 23 09:53:49 EDT 2025
Powered by FUDForum. Page generated in 0.09048 seconds
|