Jump to content

Recommended Posts

In response to this:

 

http://www.woodus.com/forums/index.php?showtopic=30162

 

I've developed a script that allows you to capture a region of the screen, send it to an OCR(Tesseract in this case), and optionaly send the scraped text to Google Translate. The latter option for the moment is more or less useless. I've only included it to see if I could do it, and in the hope that it may one day be useful. At some point I might include other online translators, but that is in the distant future.

 

For now, I'd like to concentrate on Tesseract's precision. Not so much it's ability to recognize characters accurately, but its ability to recognize characters consistently. If it can do this, then there is hope for the project. So long as 12345 is always translated as q325tu, and "q325tu" is unique, then a database can be built up and 「translated.〠I think that this could be be used for 「translating〠the menu and sub menus. It could potentially be used to 「translate〠NPC text as well, but that's a massive undertaking for the translators.

 

Those of you interested in testing:

Install Mono and Tesseract.

 

Save the following as dqxscreen.cs (Or whatever you want to. Just be sure to change the corresponding text in the appropriate places.)

Change the reference noted below to tesseract executable.* (Note, this works as is on Linux)

 

*Might work as is with Windows. Not sure, I've been corresponding with Woodus on this, and I don't know if he changed the path. I think he probably did.

 

Compile with:

gmcs -r:System.Windows.Forms.dll -r:System.Drawing.dll ./dqxscreen.cs

Run with:

mono dqxscreen.cs

 

Optionally run this:

mozroots --import --ask-remove

If that doesn't work try:

mono mozroots --import --ask-remove

or:

mozroots.exe --import --ask-remove

or:

mono mozroots.exe --import --ask-remove

 

(One of those has to work, right? :P)

 

Info here: http://www.mono-project.com/docs/faq/security/

This is to get the web interface to work. It grabs the default certificates from a mozilla installation. If you don't plan on trying out Google translate, you can skip this. (I've been able to get this to work for me, but it hasn't been tested on Windows yet.)

 

Press Z and X to change the mouse coordinates. Z is that's intended as the upper part of the window. X is the lower. It can be fine tuned using the buttons to adjust the coordinates.

 

One note: I've noticed that the output of the OCR is different depending on how much space is given for the characters. Giving it a 1px margin vs 10px+ seems to give a mostly consistent, but not entirely consistent reading. I haven't had the time to see if I can script my way around this. It'll probably involve automating the GIMP to add a border around the text. There's a few thing I'd like to experiment around with when I get the chance to see if consistency can be improved.

 

Also, for those interested in improving the accuracy: Tesseract will probably have to be trained. It's not strictly necessary, and we should probably focus on increasing the precision first, but don't let me stop you. If every thing works out, it may eventually be useful.

 

So, here's what I need the testers to do:

Take screen shots of the entire game with menus open. Post them here. Use the application to capture the Japanese text, and post that here under the picture. Do the same for other screen shots that others have posted, noting the link of the screen shot. Also, please note if you were close to the text, or allowed for more padding. Here's an example:

 

 

 

http://www.woodus.com/forums/uploads/gallery/album_62/gallery_2021_62_60479.png

v1(lots of padding):

 

 

�� Buui off) mode

The ��Ru the Minorutodoroki straw

Off Ru <a | of NukiTodorokiwara

The Sho�� a l i of legs

ⅎ` Cho��Ru

 

 

v2(very little of padding):

 

 

�� Fuui off) mode

The ��Ru the Minorutodoroki straw

Off Ru <a | of SemeTodorokiwara

The Sho�� a l i of legs

ⅎ` Cho��Ru

 

 

 

v3:

 

 

I don't know which version is which. :P I took these yesterday, and this thought recording lots of padding vs not a lot only occurred to me today. :worried: Oh, and actualyl this is what Google translate spit out. I don't think I saved the Japanese text. For your versions, use the Japanese text. I'll probably correct this at some point in the near future. Maybe. If I feel like it. :P

 

 

 

 

 

 

I've tried to work out as many errors as I've seen, but there may still be a few lurking here and there and I think I might have just found a bug. I'll revisit my code on the weekend. It has to do with the y+-1 buttons. It's probably a simple error, but I don't have time to look at it at the moment. *Sigh* Without further ado, the code:

 

 

 

//GPL Version 3: https://www.gnu.org/licenses/gpl.html
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Specialized;
public class Form1 : Form{
   public string ripped="screen";
   public string rippedimage;
   public string rippedtxt;
   public string translatedtxt;
   private Button printButton = new Button();
   private Button printButton2 = new Button();
   private Button printButton3 = new Button();
   private Button printButton4 = new Button();
   private Button printButton5 = new Button();
   private Button printButton6 = new Button();
   private Button printButton7 = new Button();
   private Button printButton8 = new Button();
   private Button printButton9 = new Button();
   private Button printButton10 = new Button();
   private Button printButton11 = new Button();
   private Label text1, text2, text3, text4;
   Bitmap memoryImage;
   //Controls where image is ripped from screen
   int x = 0;//Start coordinates
   int y = 0;
   int maxwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;//Rectangle controls
   int maxheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
   int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;//Rectangle controls
   int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
   int x1 = 0;
   int y1 = 0;
   int x2 = 0;
   int y2 = 0;
   public Form1(){
      Text = "Screen Capture";
      Size = new Size(300, 200);
      KeyPreview = true;
      CenterToScreen();
      rippedimage = "./"+this.ripped+".bmp";
      rippedtxt = "./"+this.ripped+".txt";
      translatedtxt = "./"+this.ripped+"translated.txt";
      text1 = new Label();
      text1.Parent = this;
      text1.Text = "x:"+x;
      text1.Location = new Point(30, 40);
      text1.AutoSize = true;
      text2 = new Label();
      text2.Parent = this;
      text2.Text = "y:"+y;
      text2.Location = new Point(130, 40);
      text2.AutoSize = true;
      text3 = new Label();
      text3.Parent = this;
      text3.Text = "width:"+width;
      text3.Location = new Point(30, 60);
      text3.AutoSize = true;
      text4 = new Label();
      text4.Parent = this;
      text4.Text = "height:"+height;
      text4.Location = new Point(130, 60);
      text4.AutoSize = true;
      this.KeyUp += new KeyEventHandler(OnKeyUp1);
      printButton.Text = "Capture Screen";
      printButton.Click += new EventHandler(this.printButton_Click);
      this.Controls.Add(printButton);
      printButton2.Text = "Preview";
      printButton2.Location = new Point(100, 0);
      printButton2.Click += new EventHandler(this.Preview);
      this.Controls.Add(printButton2);
      printButton11.Text = "Translate";
      printButton11.Location = new Point(200, 0);
      printButton11.Click += new EventHandler(this.translateButton_Click);
      this.Controls.Add(printButton11);
      printButton3.Text = "x+1";
      printButton3.Location = new Point(30, 80);
      printButton3.Click += new EventHandler(this.xp1);
      this.Controls.Add(printButton3);
      printButton4.Text = "x-1";
      printButton4.Location = new Point(130, 80);
      printButton4.Click += new EventHandler(this.xm1);
      this.Controls.Add(printButton4);
      printButton5.Text = "y+1";
      printButton5.Location = new Point(30, 100);
      printButton5.Click += new EventHandler(this.yp1);
      this.Controls.Add(printButton5);
      printButton6.Text = "y-1";
      printButton6.Location = new Point(130, 100);
      printButton6.Click += new EventHandler(this.ym1);
      this.Controls.Add(printButton6);
      printButton7.Text = "width+1";
      printButton7.Location = new Point(30, 120);
      printButton7.Click += new EventHandler(this.wp1);
      this.Controls.Add(printButton7);
      printButton8.Text = "width-1";
      printButton8.Location = new Point(130, 120);
      printButton8.Click += new EventHandler(this.wm1);
      this.Controls.Add(printButton8);
      printButton9.Text = "height+1";
      printButton9.Location = new Point(30, 140);
      printButton9.Click += new EventHandler(this.hp1);
      this.Controls.Add(printButton9);
      printButton10.Text = "height-1";
      printButton10.Location = new Point(130, 140);
      printButton10.Click += new EventHandler(this.hm1);
      this.Controls.Add(printButton10);
   }
   void UpdateLabels(){
      this.text1.Text = "x:"+this.x;
      this.text2.Text = "y:"+this.y;
      this.text3.Text = "width:"+(this.width);
      this.text4.Text = "height:"+(this.height);
   }

   void OnKeyUp1(object sender, KeyEventArgs e){
      if(e.KeyCode == Keys.Z){
         this.x1=Cursor.Position.X;
         this.y1=Cursor.Position.Y;
         Console.WriteLine("First:");
         Console.WriteLine(Cursor.Position);
      }
      if(e.KeyCode == Keys.X){
         this.x2=Cursor.Position.X;
         this.y2=Cursor.Position.Y;
         Console.WriteLine("Second:");
         Console.WriteLine(Cursor.Position);
      }
         if(this.x1<=this.x2){
               this.x=this.x1;
               this.width=this.x2-this.x;
         }else{
               this.x=this.x2;
               this.width=this.x1-this.x;
         }
         if(this.y1<=this.y2){
               this.y=this.y1;
               this.height=this.y2-this.y;
         }else{
               this.y=this.y2;
               this.height=this.y1-this.y;
         }
         UpdateLabels();
   }
   private void Preview(object sender, EventArgs e){
      try{
         CaptureScreen();
         OpenFile(@rippedimage);
      }catch(Exception ex){
         //Console.WriteLine(ex);
         MessageBox.Show(ex.ToString());
      }
   }
   private void xp1(object sender, EventArgs e){
      this.x=this.x+1;
      if(this.x+this.width==this.maxwidth+1){
         this.width=this.width-1;
      }
      if(this.x>=width){
         this.x=width-1;
      }
      UpdateLabels();
   }
   private void xm1(object sender, EventArgs e){
      this.x=this.x-1;
      if(this.x<0){
         this.x=0;
      }
      UpdateLabels();
   }
   private void yp1(object sender, EventArgs e){
      this.y=this.y+1;
      if(this.y+this.height==this.maxheight+1){
         this.height=this.height-1;
      }
      if(this.y>=height){
         this.y=height-1;
      }
      UpdateLabels();
   }
   private void ym1(object sender, EventArgs e){
      this.y=this.y-1;
      if(this.y<0){
         this.y=0;
      }
      UpdateLabels();
   }
   private void wp1(object sender, EventArgs e){
      this.width=this.width+1;
      if(this.width>maxwidth){
         this.width=maxwidth;
      }
      if(this.x+this.width>this.maxwidth){
         this.x=this.x-1;
      }
      UpdateLabels();
   }
   private void wm1(object sender, EventArgs e){
      this.width=this.width-1;
      if(this.width<=this.x){
         this.width=this.x+1;
      }
      UpdateLabels();
   }
   private void hp1(object sender, EventArgs e){
      this.height=this.height+1;
      if(this.height>this.maxheight){
         this.height=this.maxheight;
      }
      if(this.y+this.height>this.maxheight){
         this.y=this.y-1;
      }
      UpdateLabels();
   }
   private void hm1(object sender, EventArgs e){
      this.height=this.height-1;
      if(this.height<=this.y){
         this.height=this.y+1;
      }
      UpdateLabels();
   }
   void printButton_Click(object sender, EventArgs e){
      CaptureScreen();
      try{
         OpenTesseract(@rippedimage);
         //OpenFile(@rippedtxt);
         /*string translateme=ReadFile(@rippedtxt);
         //Console.WriteLine(translateme);
         string translated=Translate(translateme);
         //Console.WriteLine(translated);
         string decoded=Decode(translated);
         //Console.WriteLine(decoded);
         WriteFile(@translatedtxt,decoded);
         OpenFile(@translatedtxt);*/
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
      }
   }
   void translateButton_Click(object sender, EventArgs e){
      try{
         string translateme=ReadFile(@rippedtxt);
         string translated=Translate(translateme);
         string decoded=Decode(translated);
         WriteFile(@translatedtxt,decoded);
         OpenFile(@translatedtxt);
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
      }
   }
   private void CaptureScreen(){
        Graphics myGraphics = this.CreateGraphics();
        memoryImage = new Bitmap(width, height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
         try{
            memoryGraphics.CopyFromScreen(x, y, 0, 0,//0,0,**,** adjusts the location on the program window
            new Size(width,height));
            try{
               if (memoryImage != null){
                  memoryImage.Save(rippedimage, ImageFormat.Bmp);
               }
            }catch(Exception){
               MessageBox.Show("There was a problem saving the file.\nCheck the file permissions.");
            }
         }catch(Exception e){
            MessageBox.Show("Null sizes are not allowed."+e);
         }
    }
   static void OpenFile(string file){
      Process.Start(file);
   }

   static void OpenTesseract(string file){
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.CreateNoWindow = false;
      startInfo.FileName = "tesseract";//Replace tesseract with C:/path/to/tesseract.exe or other OCR program.
      startInfo.WindowStyle = ProcessWindowStyle.Hidden;
      startInfo.Arguments = file+" "+file.Substring(0,file.Length-4)+" -l jpn";
      try{
         using (Process exeProcess = Process.Start(startInfo)){
            exeProcess.WaitForExit();
         }
      }catch{return;}
   }
   string ReadFile(string file){
      // Open the file to read from.
      try{
         using (StreamReader sr = File.OpenText(file)){
            string s = "";
            StringBuilder s1 = new StringBuilder();
            while ((s = sr.ReadLine()) != null){
               s1.Append(s);//Console.WriteLine(s);
               s1.Append("\n");//Console.WriteLine(s);
            }
            s=s1.ToString();
            //Console.WriteLine(s);
            return s;
         }
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
         throw(ex);
      }
   }
   void WriteFile(string file, string text){
         try{
            using (StreamWriter sw = File.CreateText(file)){
               sw.WriteLine(text);
            }
         }catch(Exception ex){
            MessageBox.Show(ex.ToString());
            throw(ex);
         }
   }
  
   public static string FindMe(string str){
      int a=str.IndexOf("<span id=result_box");
      int b=str.IndexOf("</span></span></div></div>");
      try{
         return str.Substring(a, b-a);
      }catch(Exception ex){
         Console.WriteLine("Congratulations! You've found an error! Let's show you what you've won! \nLength1:"+a+"\nb:"+b+"\n"+ex);
         MessageBox.Show(ex.ToString());
         return "";
      }
   }
   public static string RemoveMe(string str){
      int a=0;
      int b=0;
      str=str.Replace("<br>","\n");
      while(a!=-1){
         a=str.IndexOf("<");
         b=str.IndexOf(">");
         try{
         str=str.Remove(a, b+1-a);
         }catch(Exception){break;}
      }
      return str;
   }
   string Decode(string a){
      a=a.Replace("%3A",":");
      a=a.Replace("%2F","/");
      a=a.Replace("%3F","?");
      a=a.Replace("%3D","=");
      a=a.Replace("%26","&");
      a=a.Replace("%2B","+");
      a=a.Replace("%21","!");
      a=a.Replace("%23","#");
      a=a.Replace("%24","$");
      a=a.Replace("%27","'");
      a=a.Replace("%28","(");
      a=a.Replace("%29",")");
      a=a.Replace("%2A","*");
      a=a.Replace("%2C",",");
      a=a.Replace("%3B",";");
      a=a.Replace("%40","@");
      a=a.Replace("%5B","[");
      a=a.Replace("%5D","]");
      a=a.Replace("%20"," ");
      a=a.Replace("%22","\"");
      a=a.Replace("%25","%");
      a=a.Replace("%2D","-");
      a=a.Replace("%2E",".");
      a=a.Replace("%3C","<");
      a=a.Replace("%3E",">");
      a=a.Replace("%5C","\\");
      a=a.Replace("%5E","^");
      a=a.Replace("%5F","_");
      a=a.Replace("%60","`");
      a=a.Replace("%7B","{");
      a=a.Replace("%7C","|");
      a=a.Replace("%7D","}");
      a=a.Replace("%7E","~");
      a=a.Replace("%3a",":");
      a=a.Replace("%2f","/");
      a=a.Replace("%3f","?");
      a=a.Replace("%3d","=");
      a=a.Replace("%26","&");
      a=a.Replace("%2b","+");
      a=a.Replace("%21","!");
      a=a.Replace("%23","#");
      a=a.Replace("%24","$");
      a=a.Replace("%27","'");
      a=a.Replace("%28","(");
      a=a.Replace("%29",")");
      a=a.Replace("%2a","*");
      a=a.Replace("%2c",",");
      a=a.Replace("%3b",";");
      a=a.Replace("%40","@");
      a=a.Replace("%5b","[");
      a=a.Replace("%5d","]");
      a=a.Replace("%20"," ");
      a=a.Replace("%22","\"");
      a=a.Replace("%25","%");
      a=a.Replace("%2d","-");
      a=a.Replace("%2e",".");
      a=a.Replace("%3c","<");
      a=a.Replace("%3e",">");
      a=a.Replace("%5c","\\");
      a=a.Replace("%5e","^");
      a=a.Replace("%5f","_");
      a=a.Replace("%60","`");
      a=a.Replace("%7b","{");
      a=a.Replace("%7c","|");
      a=a.Replace("%7d","}");
      a=a.Replace("%7e","~");
      a=a.Replace("%0A","\n");
      a=a.Replace("%0a","\n");
      a=a.Replace("amp;","&");
      a=a.Replace("quot;","\"");
      a=a.Replace(""","\"");
      a=a.Replace("apos;","'");
      a=a.Replace("'","'");
      a=a.Replace("lt;","<");
      a=a.Replace("gt;",">");
      a=a.Replace("copy;","©");
      a=a.Replace("reg;","®");
      a=a.Replace("mdash;","—");
      a=a.Replace("
","\n");
      a=a.Replace(" "," ");
      //a=a.Replace("","");
      return a;
   }
  
   //TODO: Pick a translation service
   //TODO: Dictionary reference
   string Translate(string text){
      string urlAddress = "https://translate.google.com/";
      string sourcelang = "ja";
      string tolang = "en";
      string js = "n";
      string prev = "_t";
      string hl = "en";
      string ie = "UTF-8";
     
      try{
         using (WebClient client = new WebClient()){
            client.Encoding = System.Text.Encoding.UTF8;
            NameValueCollection postData = new NameValueCollection(){
               { "sl", sourcelang }, //order: {"parameter name", "parameter value"}
               { "tl", tolang },
               { "js", js },
               { "prev", prev },
               { "hl", hl },
               { "ie", ie },
               { "text", text },
            };
            // client.UploadValues returns page's source as byte array (byte[])
            // so it must be transformed into a string
            string pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, postData));
            pagesource = FindMe(pagesource);
            pagesource = RemoveMe(pagesource);
            return pagesource;
         }
      }catch(Exception e){Console.WriteLine("Something's gone terribly wrong. This may or may not help:\n mozroots --import --ask-remove\n"+e);throw(e);}
     
   }
   public static void Main(){
      Application.Run(new Form1());
   }
}

 

 

Thanks for making it to the end! And for those of you who didn't, you're welcome! Pleasant dreams! :snooze: :snooze: :snooze:

Edited by Faolan

Share this post


Link to post
Share on other sites

Well, no one seems interested. All the same, I didn't want to leave code out there with such an obvious bug. So, here is the fixed code:

 

 

 

//GPL Version 3: https://www.gnu.org/licenses/gpl.html
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Specialized;

public class Form1 : Form{
   public string ripped="screen";
   public string rippedimage;
   public string rippedtxt;
   public string translatedtxt;
   private Button printButton = new Button();
   private Button printButton2 = new Button();
   private Button printButton3 = new Button();
   private Button printButton4 = new Button();
   private Button printButton5 = new Button();
   private Button printButton6 = new Button();
   private Button printButton7 = new Button();
   private Button printButton8 = new Button();
   private Button printButton9 = new Button();
   private Button printButton10 = new Button();
   private Button printButton11 = new Button();
   private Label text1, text2, text3, text4;
   Bitmap memoryImage;
   //Controls where image is ripped from screen
   int x = 0;//Start coordinates
   int y = 0;
   int maxwidth = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;//Rectangle controls
   int maxheight = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
   int width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;//Rectangle controls
   int height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
   int x1 = 0;
   int y1 = 0;
   int x2 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
   int y2 = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;

   public Form1(){
      Text = "Screen Capture";
      Size = new Size(300, 200);
      KeyPreview = true; 
      CenterToScreen();
      rippedimage = "./"+this.ripped+".bmp";
      rippedtxt = "./"+this.ripped+".txt";
      translatedtxt = "./"+this.ripped+"translated.txt";

      text1 = new Label();
      text1.Parent = this;
      text1.Text = "x:"+x;
      text1.Location = new Point(30, 40);
      text1.AutoSize = true;

      text2 = new Label();
      text2.Parent = this;
      text2.Text = "y:"+y;
      text2.Location = new Point(130, 40);
      text2.AutoSize = true;

      text3 = new Label();
      text3.Parent = this;
      text3.Text = "width:"+width;
      text3.Location = new Point(30, 60);
      text3.AutoSize = true;

      text4 = new Label();
      text4.Parent = this;
      text4.Text = "height:"+height;
      text4.Location = new Point(130, 60);
      text4.AutoSize = true;

      this.KeyUp += new KeyEventHandler(OnKeyUp1);

      printButton.Text = "Capture Screen";
      printButton.Click += new EventHandler(this.printButton_Click);
      this.Controls.Add(printButton);
      printButton2.Text = "Preview";
      printButton2.Location = new Point(100, 0);
      printButton2.Click += new EventHandler(this.Preview);
      this.Controls.Add(printButton2);
      printButton11.Text = "Translate";
      printButton11.Location = new Point(200, 0);
      printButton11.Click += new EventHandler(this.translateButton_Click);
      this.Controls.Add(printButton11);
      printButton3.Text = "x+1";
      printButton3.Location = new Point(30, 80);
      printButton3.Click += new EventHandler(this.xp1);
      this.Controls.Add(printButton3);
      printButton4.Text = "x-1";
      printButton4.Location = new Point(130, 80);
      printButton4.Click += new EventHandler(this.xm1);
      this.Controls.Add(printButton4);
      printButton5.Text = "y+1";
      printButton5.Location = new Point(30, 100);
      printButton5.Click += new EventHandler(this.yp1);
      this.Controls.Add(printButton5);
      printButton6.Text = "y-1";
      printButton6.Location = new Point(130, 100);
      printButton6.Click += new EventHandler(this.ym1);
      this.Controls.Add(printButton6);
      printButton7.Text = "width+1";
      printButton7.Location = new Point(30, 120);
      printButton7.Click += new EventHandler(this.wp1);
      this.Controls.Add(printButton7);
      printButton8.Text = "width-1";
      printButton8.Location = new Point(130, 120);
      printButton8.Click += new EventHandler(this.wm1);
      this.Controls.Add(printButton8);
      printButton9.Text = "height+1";
      printButton9.Location = new Point(30, 140);
      printButton9.Click += new EventHandler(this.hp1);
      this.Controls.Add(printButton9);
      printButton10.Text = "height-1";
      printButton10.Location = new Point(130, 140);
      printButton10.Click += new EventHandler(this.hm1);
      this.Controls.Add(printButton10);
   }

   void UpdateLabels(){
      this.text1.Text = "x:"+this.x;
      this.text2.Text = "y:"+this.y;
      this.text3.Text = "width:"+(this.width);
      this.text4.Text = "height:"+(this.height);
   }


   void OnKeyUp1(object sender, KeyEventArgs e){
      if(e.KeyCode == Keys.Z){
         this.x1=Cursor.Position.X;
         this.y1=Cursor.Position.Y;
         Console.WriteLine("First:");
         Console.WriteLine(Cursor.Position);
      }
      if(e.KeyCode == Keys.X){
         this.x2=Cursor.Position.X;
         this.y2=Cursor.Position.Y;
         Console.WriteLine("Second:");
         Console.WriteLine(Cursor.Position);
      }
      Coordinator(this.x1,this.y1,this.x2,this.y2);
   }

   void Coordinator(int x1,int y1,int x2,int y2){
      if(x1<x2){
         this.x=x1;
         if(this.x<0){
            this.x=0;
         }else if(this.x>=this.maxwidth){
            this.x=this.maxwidth-1;
         }
         this.width=x2-x;
      }else if(x1==x2){
         if(y1==y2){
            x2=x2+1;
            y2=y2+1;
         }
      }else{
         this.x=x2;
         if(this.x<0){
            this.x=0;
         }else if(this.x>=this.maxwidth){
            this.x=this.maxwidth-1;
         }
         this.width=x1-x;
      }
      if(y1<=y2){
         this.y=y1;
         if(this.y<0){
            this.y=0;
         }
         this.height=y2-y;
      }else{
         this.y=y2;
         if(this.y<0){
            this.y=0;
         }else if(this.y>=this.maxheight){
         this.y=this.maxheight-1;
         }
         this.height=y1-y;
      }
      if(this.width+this.x>this.maxwidth){
         this.width=this.maxwidth-this.x;
      }
      if(this.height+this.y>this.maxheight){
         this.height=this.maxheight-this.y;
      }
      UpdateLabels();
   }

   private void Preview(object sender, EventArgs e){
      try{
         CaptureScreen();
         OpenFile(@rippedimage);
      }catch(Exception ex){
         //Console.WriteLine(ex);
         MessageBox.Show(ex.ToString());
      }
   }

   private void xp1(object sender, EventArgs e){
      Coordinator(this.x+1,this.y,this.x+this.width,this.y+this.height);
   }

   private void xm1(object sender, EventArgs e){
      Coordinator(this.x-1,this.y,this.x+this.width,this.y+this.height);
   }

   private void yp1(object sender, EventArgs e){
      Coordinator(this.x,this.y+1,this.x+this.width,this.y+this.height);
   }

   private void ym1(object sender, EventArgs e){
      Coordinator(this.x,this.y-1,this.x+this.width,this.y+this.height);
   }

   private void wp1(object sender, EventArgs e){
      Coordinator(this.x,this.y,this.x+this.width+1,this.y+this.height);
   }

   private void wm1(object sender, EventArgs e){
      Coordinator(this.x,this.y,this.x+this.width-1,this.y+this.height);
   }

   private void hp1(object sender, EventArgs e){
      Coordinator(this.x,this.y,this.x+this.width,this.y+this.height+1);
   }

   private void hm1(object sender, EventArgs e){
      Coordinator(this.x,this.y,this.x+this.width,this.y+this.height-1);
   }

   void printButton_Click(object sender, EventArgs e){
      CaptureScreen();
      try{
         OpenTesseract(@rippedimage);
         //OpenFile(@rippedtxt);
         /*string translateme=ReadFile(@rippedtxt);
         //Console.WriteLine(translateme);
         string translated=Translate(translateme);
         //Console.WriteLine(translated);
         string decoded=Decode(translated);
         //Console.WriteLine(decoded);
         WriteFile(@translatedtxt,decoded);
         OpenFile(@translatedtxt);*/
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
      }
   }

   void translateButton_Click(object sender, EventArgs e){
      try{
         string translateme=ReadFile(@rippedtxt);
         string translated=Translate(translateme);
         string decoded=Decode(translated);
         WriteFile(@translatedtxt,decoded);
         OpenFile(@translatedtxt);
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
      }
   }

   private void CaptureScreen(){
        Graphics myGraphics = this.CreateGraphics();
        memoryImage = new Bitmap(width, height, myGraphics);
        Graphics memoryGraphics = Graphics.FromImage(memoryImage);
         try{
            memoryGraphics.CopyFromScreen(x, y, 0, 0,//0,0,**,** adjusts the location on the program window
            new Size(width,height));
            try{
               if (memoryImage != null){
                  memoryImage.Save(rippedimage, ImageFormat.Bmp);
               }
            }catch(Exception){
               MessageBox.Show("There was a problem saving the file.\nCheck the file permissions.");
            }
         }catch(Exception e){
            MessageBox.Show("Null sizes are not allowed."+e);
         }
    }

   static void OpenFile(string file){
      Process.Start(file);
   }


   static void OpenTesseract(string file){
      ProcessStartInfo startInfo = new ProcessStartInfo();
      startInfo.CreateNoWindow = false;
      startInfo.FileName = "tesseract";//Replace tesseract with C:/path/to/tesseract.exe or other OCR program. 
      startInfo.WindowStyle = ProcessWindowStyle.Hidden;
      startInfo.Arguments = file+" "+file.Substring(0,file.Length-4)+" -l jpn";
      try{
         using (Process exeProcess = Process.Start(startInfo)){
            exeProcess.WaitForExit();
         }
      }catch{return;}
   }

   string ReadFile(string file){
      // Open the file to read from. 
      try{
         using (StreamReader sr = File.OpenText(file)){
            string s = "";
            StringBuilder s1 = new StringBuilder();
            while ((s = sr.ReadLine()) != null){
               s1.Append(s);//Console.WriteLine(s);
               s1.Append("\n");//Console.WriteLine(s);
            }
            s=s1.ToString();
            //Console.WriteLine(s);
            return s;
         }
      }catch(Exception ex){
         MessageBox.Show(ex.ToString());
         throw(ex);
      }
   }

   void WriteFile(string file, string text){
         try{
            using (StreamWriter sw = File.CreateText(file)){
               sw.WriteLine(text);
            }
         }catch(Exception ex){
            MessageBox.Show(ex.ToString());
            throw(ex);
         }
   }
   
   public static string FindMe(string str){
      int a=str.IndexOf("<span id=result_box");
      int b=str.IndexOf("</span></span></div></div>");
      try{
         return str.Substring(a, b-a);
      }catch(Exception ex){
         Console.WriteLine("Congratulations! You've found an error! Let's show you what you've won! \nLength1:"+a+"\nb:"+b+"\n"+ex);
         MessageBox.Show(ex.ToString());
         return "";
      }
   }

   public static string RemoveMe(string str){
      int a=0;
      int b=0;
      str=str.Replace("<br>","\n");
      while(a!=-1){
         a=str.IndexOf("<");
         b=str.IndexOf(">");
         try{
         str=str.Remove(a, b+1-a);
         }catch(Exception){break;}
      }
      return str;
   }

   string Decode(string a){
      a=a.Replace("%3A",":");
      a=a.Replace("%2F","/");
      a=a.Replace("%3F","?");
      a=a.Replace("%3D","=");
      a=a.Replace("%26","&");
      a=a.Replace("%2B","+");
      a=a.Replace("%21","!");
      a=a.Replace("%23","#");
      a=a.Replace("%24","$");
      a=a.Replace("%27","'");
      a=a.Replace("%28","(");
      a=a.Replace("%29",")");
      a=a.Replace("%2A","*");
      a=a.Replace("%2C",",");
      a=a.Replace("%3B",";");
      a=a.Replace("%40","@");
      a=a.Replace("%5B","[");
      a=a.Replace("%5D","]");
      a=a.Replace("%20"," ");
      a=a.Replace("%22","\"");
      a=a.Replace("%25","%");
      a=a.Replace("%2D","-");
      a=a.Replace("%2E",".");
      a=a.Replace("%3C","<");
      a=a.Replace("%3E",">");
      a=a.Replace("%5C","\\");
      a=a.Replace("%5E","^");
      a=a.Replace("%5F","_");
      a=a.Replace("%60","`");
      a=a.Replace("%7B","{");
      a=a.Replace("%7C","|");
      a=a.Replace("%7D","}");
      a=a.Replace("%7E","~");

      a=a.Replace("%3a",":");
      a=a.Replace("%2f","/");
      a=a.Replace("%3f","?");
      a=a.Replace("%3d","=");
      a=a.Replace("%26","&");
      a=a.Replace("%2b","+");
      a=a.Replace("%21","!");
      a=a.Replace("%23","#");
      a=a.Replace("%24","$");
      a=a.Replace("%27","'");
      a=a.Replace("%28","(");
      a=a.Replace("%29",")");
      a=a.Replace("%2a","*");
      a=a.Replace("%2c",",");
      a=a.Replace("%3b",";");
      a=a.Replace("%40","@");
      a=a.Replace("%5b","[");
      a=a.Replace("%5d","]");
      a=a.Replace("%20"," ");
      a=a.Replace("%22","\"");
      a=a.Replace("%25","%");
      a=a.Replace("%2d","-");
      a=a.Replace("%2e",".");
      a=a.Replace("%3c","<");
      a=a.Replace("%3e",">");
      a=a.Replace("%5c","\\");
      a=a.Replace("%5e","^");
      a=a.Replace("%5f","_");
      a=a.Replace("%60","`");
      a=a.Replace("%7b","{");
      a=a.Replace("%7c","|");
      a=a.Replace("%7d","}");
      a=a.Replace("%7e","~");
      a=a.Replace("%0A","\n");
      a=a.Replace("%0a","\n");

      a=a.Replace("amp;","&");
      a=a.Replace("quot;","\"");
      a=a.Replace(""","\"");
      a=a.Replace("apos;","'");
      a=a.Replace("'","'");
      a=a.Replace("lt;","<");
      a=a.Replace("gt;",">");
      a=a.Replace("copy;","©");
      a=a.Replace("reg;","®");
      a=a.Replace("mdash;","—");
      a=a.Replace("
","\n");
      a=a.Replace(" "," ");
      //a=a.Replace("","");
      return a;
   }
   
   //TODO: Pick a translation service
   //TODO: Dictionary reference
   string Translate(string text){
      string urlAddress = "https://translate.google.com/";
      string sourcelang = "ja";
      string tolang = "en";
      string js = "n";
      string prev = "_t";
      string hl = "en";
      string ie = "UTF-8";
      
      try{
         using (WebClient client = new WebClient()){
            client.Encoding = System.Text.Encoding.UTF8;
            NameValueCollection postData = new NameValueCollection(){
               { "sl", sourcelang }, //order: {"parameter name", "parameter value"}
               { "tl", tolang },
               { "js", js },
               { "prev", prev },
               { "hl", hl },
               { "ie", ie },
               { "text", text },
            };
            // client.UploadValues returns page's source as byte array (byte[])
            // so it must be transformed into a string
            string pagesource = Encoding.UTF8.GetString(client.UploadValues(urlAddress, postData));
            pagesource = FindMe(pagesource);
            pagesource = RemoveMe(pagesource);
            return pagesource;
         }
      }catch(Exception e){Console.WriteLine("Something's gone terribly wrong. This may or may not help:\n mozroots --import --ask-remove\n"+e);throw(e);}
      
   }

   public static void Main(){
      Application.Run(new Form1());
   }
}

 

 

Since there isn't that much interest, I probably won't be adding many more features in the forseeable future. It's pretty much done any ways. Well, I hope at least a few of you find it useful.

Share this post


Link to post
Share on other sites

That sounds really handy actually, however I personally wouldn't have much use for it.

 

The reason probably why no one has responded is because most people here aren't really tech guys.

Edited by Mr. RPG

Share this post


Link to post
Share on other sites

I'd like to help, but I don't have X right now. Waiting until I've worked at least a month and I know what my budget will be. (If I try the demo I'll just want to play more.)

Share this post


Link to post
Share on other sites

Hmm. That is true, Mr. RPG. Still, I had thought there'd be at least one or two people willing to try it out. Woodus has tried previous versions of it out, and has been able to get it to work without too much trouble.

 

Erdrick, I can't run X myself. I had planned on operating off screen shots and still frames of movies. This is just to test how consistent the output from Tesseract is, given inputs from random users using the same pictures. Actually being able to run X isn't required.

From the picture I posted above the Japanese output is:

 

 

 

フ†フã†ã‚¤)モード
實轟è—を竇る
貫轟è—ã®|をフ<ã‚‹
i蘆ã®lを消竇
竇é¤ä¸ç«‡ã‚‹

 

 

 

 

フ†ブã†ã‚¤)モード
實轟è—を竇る
貫轟è—ã®|をフ<ã‚‹
i蘆ã®iを消竇
竇é¤ä¸ç«‡ã‚‹

 

 

 

 

フ†フã†ã‚¤)モード
實轟è—を竇る
貫轟è—ã®|をフ<ã‚‹
i蘆ã®lを消竇
竇é¤ä¸ç«‡ã‚‹

 

 

 

 

This one is probably a little too close.

ズフã†ã‚¤ãƒ³ãƒ¢ãƒ¼ãƒ‰
崑検を竇る
董陳ã®ç«‡ã‚’コ<ã‚‹
實検ã®ç«‡ã‚’消竇
樟ä¸ç«‡ã‚‹

 

 

 

If you get similar results (ideally exactly the same as one of the above, or very close), then there's hope for the project. These results were obtained by messsing around with the spacing of my program, increasing or decreasing the borders, so that more blank space is included.

Thanks for showing interest!

Share this post


Link to post
Share on other sites

å´‘ looks kind of like the DQ1 king sprite...

Share this post


Link to post
Share on other sites

Sorry for the delay in my testing, year end for us at work and it has been killer.

Share this post


Link to post
Share on other sites

I figured it was probably something like that on your part. No worries. ;) I didn't really want to wait, and it really was about time to ask for others to help out any ways. I've been pretty swamped with work too, but I don't think my load is going to change any time soon... >.< Limiting coding to the weekends is kind of a drag, but it is what it is. I just don't have the motivation or time after coming home from work. Hahaha.

Share this post


Link to post
Share on other sites

Limiting coding to the weekends is kind of a drag, but it is what it is. I just don't have the motivation or time after coming home from work. Hahaha.

This is me with DQVII-2.

Share this post


Link to post
Share on other sites

I've thought this before, but you and I are alike in more ways than I care to admit. :P I'll give you this though, you seem to be more motivated than me. At the very least more motivated than I was at 18. :thumbsup:

Edited by Faolan

Share this post


Link to post
Share on other sites

Sometimes I'm a bit too motivated. I tend to bite off more than I can chew, and then never finish any projects. I've even thought of dropping DQVII-2 a few times now. Thankfully every time I'm about to give up somebody posts about it and the fact that people actually care about it motivates me to continue working.

Share this post


Link to post
Share on other sites

I've been really slammed too and haven't had a chance to touch this, but I really want to give it a go.  Wondering if I could use it with other emulated games like Torneko 3 and what not.

Share this post


Link to post
Share on other sites

Young Yangus.

Share this post


Link to post
Share on other sites

Sometimes I'm a bit too motivated. I tend to bite off more than I can chew, and then never finish any projects. I've even thought of dropping DQVII-2 a few times now. Thankfully every time I'm about to give up somebody posts about it and the fact that people actually care about it motivates me to continue working.

That's called being over ambitious. A problem I know all too well. *Sigh* I've made efforts to combat it, but it's hard. :P Getting organized and planning out what you want helps me a lot.

 

KZ: If you're planning to do what I said above, then I don't think there's a reason why it shouldn't. Post a screen capture of the entire screen, and we'll post and compare our respective results.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...