Using Tesseract OCR in C# - c#

I have trying to deploy tesseract for reading the clipboard image through the code below in a C# window.form. But, a black commandline window appears and nothing happens.
private void b1_Click(object sender, EventArgs e)
{
if (ofd1.ShowDialog() == DialogResult.OK)
{
var img = new Bitmap(ofd1.FileName);
var ocr = new TessBaseAPI("./tessdata", "eng", OcrEngineMode.DEFAULT);
var page = ocr.SetImage(img);
tb1.Text = page.ToString();
}
}
The error it gives is cannot convert from 'System.Drawing.Bitmap' to 'Leptonica.Pix' hope this can be improved.

Instead of creating a bitmap, try using a Pix object like:
var img = Tesseract.Pix.LoadFromFile(ofd1.FileName)

Related

c# how to convert PNG / JPG to TGA 32bit?

private void ConvertButton_Click(object sender, EventArgs e)
{
Bitmap original = new Bitmap(#"filepath.cover.png");
Bitmap clone = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppPArgb);
using (Graphics conv = Graphics.FromImage(clone))
{
conv.DrawImage(original, new Rectangle(0, 0, clone.Width, clone.Height));
}
}
Hello everyone, i need some help.
I trying to convert PNG or JPEG files to TGA 32bit files and save them, I am still new to programming and could not find an answer here. I found only this code snip here and tried to get him to run, i tried alot of versions to get an output file, but nothing worked, sometimes I got a blank file, sometimes a corrupt one
thank you to everyone who helps me.
Edit:
First of all, thank you, I tried your code and it gives me an empty file. I'm just trying this:
private void TgaConvert_Click(object sender, EventArgs e)
{
TGA original = new TGA(#"file.path.cover.png");
TGA clone = new TGA(original.Width, original.Height, TgaPixelDepth.Bpp32,
TgaImageType.Uncompressed_TrueColor);
using (??? conv = ???(clone))
{
conv.???(original, new ???(0, 0, clone.Width, clone.Height));
clone.Save(#"file.path.cover.tga");
}
}
at the places with "???" I can't get any further
Unfortunately there is no write support for TGA included in the .net framefork.
But there are other open source libraries available. Have a look at TGASharpLib from Zelenskyi Alexandr (https://github.com/ALEXGREENALEX/TGASharpLib).
If a apply his sample to your code, then this is the result:
using TGASharpLib;
...
private void ConvertButton_Click(object sender, EventArgs e)
{
var tga = new TGA(#"filepath.cover.png");
tga.Save(#"filepath.cover.tga");
}
I did it:
using TGASharpLib;
....
TGA T;
private void ConvertButton_Click(object sender, EventArgs e)
{
using (Bitmap original = new Bitmap("file.path.jpg"))
using (Bitmap clone = new Bitmap(original))
using (Bitmap newbmp = clone.Clone(new Rectangle(0, 0, clone.Width, clone.Height), PixelFormat.Format32bppArgb))
T = (TGA)newbmp;
T.Save("file.path.cover.tga");
}

C# WebBrowser Copy Image to Clipboard-full codes

I am a newer of C#,I want to use WebBrowser control to copy an image and save to local disk,after I googled in stackoverflow is this code I need to use,but I am a newer,could anyone can provider a Full C# Codes to make it work?(ConsoleApplication type),thanks in advance.
I want to COPY in webbrowser (not download) this image file
to
C:\google.png
The source is here:
WebBrowser Copy Image to Clipboard
string image_name = "temp.bmp";
IHTMLDocument2 document = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)document.body).createControlRange();
imgRange.add(document.all.item(HTML_IMAGE_ID));
imgRange.execCommand("Copy");
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(image_name);
}
Here is the full code of working sample
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}
private void button2_Click(object sender, EventArgs e)
{
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
foreach (IHTMLImgElement img in doc.images)
{
imgRange.add((IHTMLControlElement)img);
imgRange.execCommand("Copy", false, null);
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
bmp.Save(#"C:\" + img.nameProp);
}
}
}
You need to add namespace using mshtml;

Orientation of PDF when viewing with Ghostscript

My application shows PDFs in a picture box by using Ghostscript. The PDFs I use are scanned images with a text layer. created by the OCR function from Acrobat Pro. This OCR function automatically sets the orientation according to the direction of the text. When the page is displayed in the picture box this information is lost. It just displays every page in portrait mode.
Is there a way for Ghostscript to access this property of the PDF and display it in the correct orientation in the picture box?
public Form1()
{
InitializeComponent();
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
NumberOfPagesToExtract = 1;
_viewer.Open("NoFile.pdf", _gsVersion, true);
}
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblTotalNmbPages.Text = " / " + LastPageNumber.ToString();
txtCurrentPgNmbr.Text = currentPageNumber.ToString();
}
The code to open the file:
private void btnOpenPdfGhostScript_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Open PDF file";
ofd.Filter = "PDF, PS, EPS files|*.pdf;*.ps;*.eps";
if (ofd.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
btnCLose_Click(this, null);
_viewer.Open(ofd.FileName, _gsVersion, true);
currentFilePath = ofd.FileName;
currentPageNumber = _viewer.CurrentPageNumber;
LastPageNumber = _viewer.LastPageNumber;
lblCurrentFIle.Text = ofd.FileName; //System.IO.Path.GetFileName(ofd.FileName);
if (backgroundWorker1.IsBusy != true) backgroundWorker1.RunWorkerAsync();
}
currentPageNumber = 1;
progressBar1.Value = 0;
}
I'm not sure if I should answer my own questions, but the problem is solved by updating to version 1.1.8. Thanks to HABJAN. Thanks very much.
The first thing to do is to use Ghostscript directly from the command line, rather than in an application. The main reason is that you can then supply a GS command line which other people can use, experiment with and comment on.
I can't see from your code how Ghostscript is invoked.
Ghostscript should, in general, honour the /MediaBox (and optionally /CropBox etc.) as well as the /Rotate attribute of pages.
But without a sample file and command line, I can't give you any suggestions.

Windows phone picturebox

I'm developing an windows phone 8.1 application in C#. I'm using the camera to take a picture. The picture is than saved on the device and I'm trying to show it in a picturebox.
I have tested it on a HTC phone and it worked nice, but when i tried it on a Nokia Lumia the picture would never load.
Does anyone have an idea how to solve that?
Here is the code I am sing to take a picture:
private void snap_task_Click(object sender, EventArgs e)
{
cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += cameraCaptureTask_Completed;
cameraCaptureTask.Show();
}
void cameraCaptureTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
NavigationService.Navigate(new Uri("/Slika.xaml?fotka=" + e.OriginalFileName, UriKind.Relative));
}
}
And this is the code where I try toload the picture.
public Slika()
{
InitializeComponent();
string slika = string.Empty;
string slika2 = string.Empty;
this.Loaded += (s, e) =>
{
if (NavigationContext.QueryString.TryGetValue("fotka", out slika))
{
putanja = slika; /*"/Resources/" + slika + ".png";/**/
int x = putanja.Length;
if (x == 1)
{
putanja = "/Resources/" + putanja + ".png";
uriPutanja = new Uri(putanja, UriKind.Relative);
fotka = new BitmapImage(uriPutanja);
}
else
{
uriPutanja = new Uri(putanja, UriKind.Relative);
porukaTextBox.Text = putanja;
fotka = new BitmapImage(uriPutanja);
}
}
img1.Source = fotka;
};
}
PS
the loading from local resources works fine on both phones, it is just the "else" part of the if that is causing problems on the Nokia.
You are saving the image in the Camera roll folder in your phone, try saving it on your memory card instead and try if that works (you an just change it in the setting of the phone and say to save the new pictures on the SD card) If that works, try using the PhotoChooserTask in order to get the image.
I hope that the following code will help you:
using Microsoft.Phone.Tasks;
using System.IO;
using System.Windows.Media.Imaging;
...
PhotoChooserTask selectphoto = null;
private void button1_Click(object sender, RoutedEventArgs e)
{
selectphoto = new PhotoChooserTask();
selectphoto.Completed += new EventHandler(selectphoto_Completed);
selectphoto.Show();
}
void selectphoto_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
BinaryReader reader = new BinaryReader(e.ChosenPhoto);
image1.Source = new BitmapImage(new Uri(e.OriginalFileName));
}
}
You can try changing the UriKind from Relative to Absolute. If I have understood your code, you will get the absolute path to the picture.
As I understand the code you have forgotten the .png in the else case.

OCR reading using C#

I have a project which is to read character in a captured image but I'm stuck at the button which is to scan image. I ended up tesseract dll in c#, but I don't know how can I code it. I'm a newbie to this programming.
private void Browse_Click(object sender, EventArgs e)
{
//FileInfo fi = new FileInfo(string.Format(#"C:\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\{0}", imageName));
OpenFileDialog fi = new OpenFileDialog();
fi.InitialDirectory = #"C:\\Documents and Settings\JOrce0201610\My Documents\Visual Studio 2005\Projects\OCR Reader\Card";
fi.Filter = "BMP Image|*.bmp";
fi.FilterIndex = 2;
fi.RestoreDirectory = true;
if (fi.ShowDialog() == DialogResult.OK)
{
//image file path
textBox1.Text = fi.FileName;
//display image in picture box
pictureBox1.Image = new Bitmap(fi.FileName);
}
}
private void Scan_Click(object sender, EventArgs e)
{
Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image
FiltersSequence seq = new FiltersSequence();
seq.Add(Grayscale.CommonAlgorithms.BT709); //First add GrayScaling filter
seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
temp = seq.Apply(source); // Apply filters on source image
If you are a 'newbie' to programming, OCR is not the best place to start. The best I can suggest is that you use a webservice or existing library that can do this for you.
Microsoft has project Hawaii, Hawaii has an OCR service which is quite easy to use.

Categories

Resources