2014-07-11 6 views
0

Je crée un décodeur de code qr en C++ à des fins de bureau et je voulais utiliser un fichier batch pour exécuter zbar sur windows pour lire l'image.zbar QR code decode issue

Il sera utilisé avec Qt et je voulais savoir si je pouvais changer la sortie du fichier batch pour qu'il apparaisse dans un textEdit, j'ai déjà changé la sortie standard std :: cout pour apparaître dans textEdit et je pensais que cela ferait l'affaire.

S'il n'y a pas moyen de le faire apparaître directement est-il possible de récupérer le résultat du fichier batch et de le ramener à mon programme C++?

est le code que je trouve ici en ligne pour tester l'évolution std :: Cout:

main.cpp:

#include "qr.h" 
#include <QtGui/QApplication> 
#include <QtGui> 
#include "qdebugstream.h" 

int main(int argc, char *argv[]) 
{ 
    QApplication app(argc, argv); 
    app.connect(&app,SIGNAL(lastWindowClosed()),&app,SLOT(quit())); 
    //QR w; 
    //w.show(); 

    QMainWindow* mainWindow = new QMainWindow(); 

    QTextEdit* myTextEdit = new QTextEdit(mainWindow); 
    myTextEdit->setReadOnly(true); 
    QDebugStream qout(std::cout, myTextEdit); 
    std::clog<<"hello"; 
    mainWindow->show(); 
    std::cout << "TEST" << std::endl; 
    //system("D:\\QRCode\\JQR_Gen\\Debug\\runZbar.bat"); 


    return app.exec(); 
} 

qdebugstream.h:

#ifndef QDEBUGSTREAM_H 
#define QDEBUGSTREAM_H 

#include <iostream> 
#include <streambuf> 
#include <string> 

#include "qtextedit.h" 

class QDebugStream : public std::basic_streambuf<char> 
{ 
public: 
QDebugStream(std::ostream &stream, QTextEdit* text_edit) : m_stream(stream) 
{ 
    log_window = text_edit; 
    m_old_buf = stream.rdbuf(); 
    stream.rdbuf(this); 
} 
~QDebugStream() 
{ 
    // output anything that is left 
    if (!m_string.empty()) 
    log_window->append(m_string.c_str()); 

    m_stream.rdbuf(m_old_buf); 
} 

protected: 
virtual int_type overflow(int_type v) 
{ 
    if (v == '\n') 
    { 
    log_window->append(m_string.c_str()); 
    m_string.erase(m_string.begin(), m_string.end()); 
    } 
    else 
    m_string += v; 

    return v; 
} 

virtual std::streamsize xsputn(const char *p, std::streamsize n) 
{ 
    m_string.append(p, p + n); 

    int pos = 0; 
    while (pos != std::string::npos) 
    { 
    pos = m_string.find('\n'); 
    if (pos != std::string::npos) 
    { 
    std::string tmp(m_string.begin(), m_string.begin() + pos); 
    log_window->append(tmp.c_str()); 
    m_string.erase(m_string.begin(), m_string.begin() + pos + 1); 
    } 
    } 

    return n; 
} 

private: 
std::ostream &m_stream; 
std::streambuf *m_old_buf; 
std::string m_string; 


    QTextEdit* log_window; 
    }; 

#endif 

et est ici la code de lot:

@set PATH=%PATH%;C:\Program Files (x86)\ZBar\bin 
@cd D:\QRCode\JQR_Gen 
@zbarimg "test.bmp" 

Toute aide serait grandement appréciée, espérait le faire de cette façon car il semblait que ce serait beaucoup plus facile que d'utiliser zbar correctement dans C++ comme je l'ai lu, il n'a pas de très bonnes fonctionnalités sur Windows.

Merci d'avance.

+0

Vous pouvez exécuter « zbarimg « test.bmp » » avec QProcess sans utiliser un fichier de commandes, et lisez la sortie en utilisant l'API QProcess. – vahancho

+0

Je suis ce http://qt-project.org/doc/qt-5/QProcess.html et je ne peux pas travailler sur ce qui doit aller dans le programme QString atm afin d'exécuter cette commande, atm je suis juste en utilisant le fichier batch, mais vous dites que cela peut être fait directement? Merci pour la réponse rapide avant – bd99

Répondre

0

Voici un exemple simple et même pas compilé d'utilisation de QProcess.

#include <QProcess> 

int main(int argc, char *argv[]) 
{ 
    QProcess qCommandZbarimg; 
    qCommandZbarimg.start("C:\\Program Files (x86)\\ZBar\\bin\\zbarimg.exe", 
          QStringList() << "Path\\To\\Bitmap\\test.bmp"); 
    // Wait only up to 2000 ms for successful termination of the command. 
    if(qCommandZbarimg.waitForFinished(2000)) 
    { 
     // Okay, "zbarimg.exe test.tmp" was executed successfully. 
     // Read output of this command to stdout into an array of bytes. 
     QByteArray qCommandOuput = qCommandZbarimg.readAll(); 
     qCommandZbarimg.close(); 
     // Has the command written anything to stdout? 
     if(qCommandOuput.size() > 0) 
     { 
     // Do whatever you want to do with the output. 
     } 
    } 
    // Dummy code to have no warnings on build. 
    if(argv[0][1] == ' ') return argc; 
    return 0; 
}