File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Can't get ImageIcon to show up from captcha Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can Watch "Can New topic
Author

Can't get ImageIcon to show up from captcha

Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Hello!

I have an assigment were I'm supposed to validate a captcha. But I can't get the captcha image to get loaded into my GUI. I have two classes so far. First the ParsCaptcha.java class that contains the method getImage()

And the GUI:



But I can't get the image to show up in the frame that my GUI creates. At the end of the GUIconstructor I have put a system.out.println("TestKonstruktor"); just to check that the program goes through the methods. And I do get TestKonstruktor in the console. And there is n NullPointerException. The method getTextBetween returns "" if startindex is null. But with System.out.println("start:" + start); the console prints out
start:<image>
start:<url>

So, it seems to be OK there as well. Does anyone understand were to find the solution? Or, is there something with the GUI?
( I am not using the actionPerformed method right now )

Anders
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Just after submitting I saw that in the method getTextBetween startindex and endindex do get values -1 so getTextBetween will return "". But why then do they get the values -1?

Anders
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 177
    
    6

Because of how indexOf behaves. If it can find the string, it will return the index. If it can't find the string it returns garbage data (-1), which means that the substring you're looking for doesn't exist.
What is the content of sb (line 99) and does it contain the text "<image>"?


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Thank you for answering,

If I do a System.out.println(sb.toString()); after line 100 ie


Nothing happens ie it doesn't seem to be a string there. ?? So , for some reason, my piece of code isn't reading what I want it to. I can't figure out what?

Anders
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
I saw a mistake here. I had the wrong class in main in the GUI class. Now I've fixed that. So, here is what I get when putting in system.out.println(sb.toString()) at createParser():


ÿØÿà a lot of squares CREATOR: gd-jpeg v1.0 (using IJG JPEG v80), default qualityÿÛ a lot of asquares plus much more


So, there is something being read. But I'm obviously not getting the imageicon. So, am I not using the correct code to read the stream, in createPareser() ie:


or should I change the tags, to read inbetween, ie should it be something else instead of <image> and <url> in the getImage() method.
Or, is it something wrong in my handling in GUI?

Someone?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Or, is it something wrong in my handling in GUI?

easy to test.

put an image in the same folder, then change

//label.setIcon(parser.getImage());
label.setIcon(new ImageIcon("Test.gif"));//your image name e.g test.gif

also, comment out
//createParser();

so, when run, it's only job is to set the label's icon

if image appears OK, then GUI is not the problem
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 177
    
    6

It's something fundamentally wrong with your assumption of what you are reading. It's not HTML (check the content-type of the HTTP header below).

I used Fiddler to check the content that is sent over the HTTP connection: it returned the following:
HTTP/1.1 200 OK
Date: Sun, 12 Feb 2012 22:44:56 GMT
Server: Apache/2.2.20 (FreeBSD) PHP/5.3.5 with Suhosin-Patch mod_ssl/2.2.20 OpenSSL/0.9.8q
X-Powered-By: PHP/5.3.5
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Last-Modified: Sun, 12 Feb 2012 22:44:56 GMT
Content-Length: 6266
Connection: close
Content-Type: image/jpeg

...

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Tina Smith wrote:It's something fundamentally wrong with your assumption of what you are reading. It's not HTML (check the content-type of the HTTP header below).

I used Fiddler to check the content that is sent over the HTTP connection: it returned the following:
HTTP/1.1 200 OK
Date: Sun, 12 Feb 2012 22:44:56 GMT
Server: Apache/2.2.20 (FreeBSD) PHP/5.3.5 with Suhosin-Patch mod_ssl/2.2.20 OpenSSL/0.9.8q
X-Powered-By: PHP/5.3.5
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Last-Modified: Sun, 12 Feb 2012 22:44:56 GMT
Content-Length: 6266
Connection: close
Content-Type: image/jpeg

...



Hmm...


luck, db
There are no new questions, but there may be new answers.
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
Yes, the picture is there. So, the GUI is working, but this is not HTML code. I compared this to another piece of code I have were I read RSS. And in that code I saw the HTML tags. But here there are no html tags. I'm stuck. I have to read the captcha code but I do not understand how?
Anyone?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

Did you really think you would read the captcha code from whatever the server serves up? That would defeat the very purpose of a captcha, which is to verify that a human is responding.

What exactly are you trying to achieve?
Anders Kviback
Ranch Hand

Joined: Mar 21, 2011
Posts: 45
hello, again!

I misunderstood the assignment. The captcha was created at a server and we had to use a special webaddress to get the captcha and then to validate the captcha with another address. Thanks a lot though that you've taken you're time here. Though, I solved the assignment.

Anders
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can't get ImageIcon to show up from captcha