How to convert TIF image to char array in c++ - arrays

I'm working with tesseract ocr.I have done it by console. But during GUI of C++ I faced some problem. In console I have used char array to get input image. But now I'm uploading an image by openFileDialog. I need to convert it in char array. But giving many error. I'm giving my code which is written under a recogition button. How can I solve my problem? I have seen this. It's not helpful for me.
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
char image[256];
//cin>>image;//while I'm taking input in console
image=im;//im is the bitmap image which I have loaded.
PIX *pixs = pixRead(image);
STRING text_out;
api.ProcessPages(image, NULL, 0, &text_out);
//cout<<text_out.string();
ofstream myfile;
myfile.open ("example.txt");
myfile << text_out.string();
myfile.close();
}

Related

Declare VideoCapture as a global variable

I am trying to stream webcam at C++/CLI winform application with using picture-box.
With the help of timer,i can stream webcam on picture-box but VideoCapture cap(0); Mat frame; are declare continuously. How can i transfer these two to a button click and use inside this timer.
private: System::Void Timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
VideoCapture cap(0);
Mat frame;
cap.read(frame);
System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
System::IntPtr ptr2(frame.ptr());
System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(frame.cols,
frame.rows, frame.step, System::Drawing::Imaging::PixelFormat::Format24bppRgb, ptr2);
System::Drawing::RectangleF rect2(0, 0, pictureBox1->Width, pictureBox1->Height);
graphics2->DrawImage(b2, rect2);
}

How to get the senders' position (coordinates) in Windows Form Application?

I just began learning WinForms and am currently baffled on how to get the senders' (mouses') position (coordinates). I tried searching but to no avail.
This is my, somewhat, of a try but, sadly, it ended up with an error:
private: System::Void pictureBox1_MouseHover(System::Object^ sender, System::EventArgs^ e) {
this->pictureBox1->Location = System::Drawing::Point(sender::Position.X - 5, sender::Position.Y - 5);
MessageBox::Show("Foo", "Bar", MessageBoxButtons::OK, MessageBoxIcon::Stop);
}
So my question here is quite clear, I think: how can I get the senders' position (in this case, the mouses'). Explanations would also be of help. Thank you.
Since I didn't find a valid answer I took the longer route.
Firstly, I declared a boolean in the namespace with the value of false (it will change to true when the mouse will touch the picture). Then I create two new methods: one to get the X and Y of the mouse and execute code if the mouse is touching the picture and the second one to determine whether the mouse is touching the picture or not.
private: System::Void picture_MouseMove(Object^ sender, System::Windows::Forms::MouseEventArgs^ e) {
int VMouseX = e->X,
VMouseY = e->Y;
if (VMouseEntered) {
VMouseEntered = false;
this->picture->Location = System::Drawing::Point(VMouseX + 17, VMouseY + 17);
}
}
private: System::Void picture_MouseEnter(System::Object^ sender, System::EventArgs^ e) {
VMouseEntered = true;
}
Then, I create two new EventHandlers for the picture. The first EventHandler is to listen for mouse movement, the second one is to check whether the mouse is touching the picture.
this->picture->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::picture_MouseMove); // Checks for mouse movement.
this->picture->MouseEnter += gcnew System::EventHandler(this, &Form1::picture_MouseEnter); // Checks whether the mouse is touching the picture.
Done. I hope that this will help someone.

OpenCV IMread issue with windows form app

I have an application that loads an image, so i use:
image1 = imread("Cam_Pic.jpeg", CV_LOAD_IMAGE_COLOR);
And then I do some face detection code and save it to a file to then show it on my windows form. It works perfectly if i only do it once. To give you a little background. This is kinda how my code works:
I "click" button on form
my camera takes pic & saves it as a JPEG image
Picture is read by imread
haar cascade is used to detect face
rectangle is placed around detectedface
THAT image is saved to new JPEG
Form loads the the new JPEG image (with rectangle around face)
This works perfectly if I only do it ONCE. If i press the button again, the camera will take the picture, but my "face_detect" function will gave me an IMREAD error this time saying it can't read the pic taken by the camera, but it is there. Here's my code, or part of it:
void Face_detect()
{
Mat image1;
Mat grayscaleFrame;
//image1 = cam_cap;
//G E T I M A G E
image1 = imread("Cam_Pic.jpeg", CV_LOAD_IMAGE_COLOR);
if (image1.empty())
{
cout << "!!! Failed imread(): image not found" << endl;
}
.
.
.
.
imwrite("Detected_image.jpeg", image1);
}
/////////// W I N D O W S F O R M /////////////////
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
// snap picture
PlaySound(L"Eagle_img_analyzing.wav", NULL, SND_FILENAME | SND_ASYNC);
Eagle.saveImage("Cam_Pic.jpeg");
Face_detect();
PlaySound(L"Eagle_Analysis_complete.wav", NULL, SND_FILENAME | SND_ASYNC);
pictureBox1-> Load("Detected_image.jpeg");
}
Any idea how to troubleshoot this?

How to convert a System::String to char* and use it with fopen? [duplicate]

This question already has answers here:
C++/CLI String Conversions
(2 answers)
Closed 9 years ago.
I wanna convert a System::String to char* as in order to open a file with fopen and then read it. I've tried with (char *)System::Convert::ToCharand it compiles fine but when I run the app it crashes. Here is the code:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) //Convertir
{
FILE * fichero = fopen((char*)Convert::ToChar(openFileDialog1->FileName), "r");
if(fichero)
{
But that crashes the app, so how can i fix it?
To get a string pointer usable by fopen(), you should use the marshal_as template function.
You can obtain a const char * thusly:
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
std::string fileName = marshal_as<std::string>(openFileDialog1->FileName);
FILE * fichero = fopen(fileName.c_str(), "r");
if(fichero)
{
However, do you really want to convert the string to narrow characters, therefore losing wide-character compatibility? No, you don't. You should actually use _wfopen, and marshal_as<std::wstring> (to get a wchar_t pointer) instead:
#include <wchar.h>
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
std::wstring fileName = marshal_as<std::wstring>(openFileDialog1->FileName);
FILE * fichero = _wfopen(fileName.c_str(), L"r");
if(fichero)
{
This is the correct code you should be using.
Note: To actually get a non-const char*, or if you only have Visual 2005, you should use Marshal::StringToHGlobalAnsi or Marshal::StringToHGlobalUni instead (don't forget the Marshal::FreeHGlobal when you're done with it) and cast the IntPtr into a char*/wchar_t* (probably via its ToPointer method).

why is this C++/CLI code not adding the files to a listbox

I have this c++ code that is supposed to detect when a file has been modified in a directory and add the file name to a list box.
This is the file watcher part which is inside a button that starts the monitoring process
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
array<String^>^ args = Environment::GetCommandLineArgs();
FileSystemWatcher^ fsWatcher = gcnew FileSystemWatcher( );
fsWatcher->Path = "C:\\users\\patchvista2";
fsWatcher->IncludeSubdirectories = true;
fsWatcher->NotifyFilter = static_cast<NotifyFilters>
(NotifyFilters::FileName |
NotifyFilters::Attributes |
NotifyFilters::LastAccess |
NotifyFilters::LastWrite |
NotifyFilters::Security |
NotifyFilters::Size );
Form1^ handler = gcnew Form1();
fsWatcher->Changed += gcnew FileSystemEventHandler(handler, &Form1::OnChanged);
fsWatcher->Created += gcnew FileSystemEventHandler(handler, &Form1::OnChanged);
fsWatcher->EnableRaisingEvents = true;
}
then for the onchange part I have this code
void OnChanged (Object^ source, FileSystemEventArgs^ e)
{
// Here is the problem
MessageBox::Show(e->FullPath);
listBox1->Items->Add(e->FullPath);
// End problem
System::Security::Cryptography::MD5 ^ md5offile = MD5CryptoServiceProvider::Create();
array<Byte>^ hashValue;
FileStream^ fs = File::Open(e->FullPath, IO::FileMode::Open, IO::FileAccess::Read, IO::FileShare::ReadWrite);
fs->Position = 0;
hashValue = md5offile->ComputeHash(fs);
PrintByteArray(hashValue);
fs->Close();
Application::DoEvents();
}
It will message box me the file name but it will not add it to the list box. I tried having it display the file name to a label but that did not work either. it seems like the screen is not refreshing once this code loop is started. I have this code in vb.net and it does add the file name to the list box. can someone show me why the file name is not getting added tot the list box.
Two things:
You need to keep the FileSystemWatcher alive. It's liable to be garbage collected where you've got it now. (Create a class field and stick it there.)
You need to Invoke onto the UI thread whenever you do anything with a UI component.
Here's the approximate syntax for #2 (I'm away from a compiler at the moment, this may not be exact.)
void Form1::AddToListBox(String^ filename)
{
listBox1->Items->Add(filename);
}
void Form1::OnChanged(Object^ source, FileSystemEventArgs^ e)
{
Action<String^>^ addDelegate = gcnew Action<String^>(this, &Form1::AddToListBox);
this->Invoke(addDelegate, e->FullPath);
...
}

Resources