PDA

View Full Version : [C#] Neopets OCR



Zachafer
08-02-2012, 12:39 PM
using System.Drawing;
using System.IO;
using System.Text;

namespace NeoFunctions
{
static class NeoOCR
{
public static Point OCR(string img)
{
using (MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(img)))
{
return OCR(new Bitmap(ms));
}
}

public static Point OCR(Bitmap img)
{
Point oS = new Point();
int width = img.Width;
int height = img.Height;
float darkPixel = 1;
for (int x = 0; x < width; x += 10)
{
for (int y = 0; y < height; y += 10)
{
float curPixel = img.GetPixel(x, y).GetBrightness();
if (curPixel < darkPixel)
{
darkPixel = curPixel;
oS.X = x;
oS.Y = y;
}
}
}
return oS;
}

public static string AlternatePrice(string price)
{
string oldprice = price.Replace(",", string.Empty);
string newprice = string.Empty;
int alt = 1;
while(newprice.Length < oldprice.Length)
newprice += oldprice[alt = alt == 1 ? 0 : 1];
return newprice;
}
}
}

OCR(string img) - string parameter is the HTML of the captcha image (headers excluded). Returns the coordinates (as a Point) of the pet.
OCR(Bitmap img) - Bitmap parameter is the captcha image. Returns the coordinates (as a Point) of the pet.

AlternatePrice(string price) isn't really part of the OCR process but this takes the price and alternates the first two characters.
Input: 28429 Output: 28282

DarkByte
08-17-2012, 05:52 AM
same thing in vb.net



Public Function docaptcha(ByVal myBitmap As Bitmap) As Point
Dim lowestbrightness As Single = 1000000
Dim Height As Int32 = myBitmap.Width
Dim Width As Int32 = myBitmap.Height
Dim lowestbrightness_x As Integer
Dim lowestbrightness_y As Integer
For y = 0 To Width - 1 Step 4
For x = 1 To Height - 1 Step 4
If x >= Height Then Exit For 'Debug shouldnt hit this
If y >= Width Then Exit For 'Debug shouldnt hit this
'myBitmap.SetPixel(x, y, Color.ForestGreen) 'Draw scan points
Dim pixelbright As Single = myBitmap.GetPixel(x, y).GetBrightness
If pixelbright < lowestbrightness Then
lowestbrightness = pixelbright
lowestbrightness_x = x
lowestbrightness_y = y
End If
Next x
Next y

Return New Point(lowestbrightness_x, lowestbrightness_y)
End Function


implementation e.g;


Dim newimage As Bitmap = thewrapper.getimage(captchaurl, thewrapper.LastPage)
Dim ocrpoint As New Point
ocrpoint = abhandler.docaptcha(newimage)
strhtml = thewrapper.Request("POST", "[Only registered and activated users can see links]" & tempprice & "&x=" & ocrpoint.X & "&y=" & ocrpoint.Y, currentitemurl)
If strhtml.Contains("I accept your") = True Then
'Item bought
buylog.Add("Bought item - " & currentitemname)
End If