Exemple #1
0
/* This function works the same as the addHighlight, but instead of highlighting
  text in a note, it highlights the text in an image. */
void NoteFormatter::addImageHighlight(qint32 resLid, QFile &f) {
    if (enSearch.hilightWords.size() == 0)
        return;

    // Get the image resource recognition data.  This tells where to highlight the image
    ResourceTable resTable;
    Resource recoResource;
    resTable.getResourceRecognition(recoResource, resLid);
    if (!recoResource.__isset.recognition || !recoResource.data.__isset.size || !recoResource.data.__isset.body ||
            recoResource.data.size == 0) {
        return;
    }

    // Now we have the recognition data.  We need to go through it
    QByteArray recoData;
    recoData.append(QString::fromStdString(recoResource.recognition.body));
    QString xml(recoData);

    // Get a painter for the image.  This the background for what we are painting on (in other words,
    // the initial image
    QPixmap pix(f.fileName());
    QPixmap highlightedPix(pix.size());
    QPainter p(&highlightedPix);
    p.drawPixmap(0,0,pix);

    // Create a transparent pixmap.  The only non transparent piece is teh
    // highlight that will be overlaid on the old image
    QPixmap overlayPix(pix.size());
    overlayPix.fill(Qt::transparent);
    QPainter p2(&overlayPix);
    p2.setBackgroundMode(Qt::TransparentMode);
    p2.setRenderHint(QPainter::Antialiasing,true);
    QColor yellow(Qt::yellow);
    p2.setBrush(yellow);

    // Now, we have the image.  We need to go through all the recognition data to highlight
    // what we've found.
    QDomDocument doc;
    doc.setContent(xml);

    // Go through the "item" nodes
    QDomNodeList anchors = doc.elementsByTagName("item");
    for (unsigned int i=0; i<anchors.length(); i++) {
        QDomElement element = anchors.at(i).toElement();
        int x = element.attribute("x").toInt();
        int y = element.attribute("y").toInt();
        int w = element.attribute("w").toInt();
        int h = element.attribute("h").toInt();

        // Get all children ("t" nodes)
        QDomNodeList children = element.childNodes();
        for (unsigned int j=0; j<children.length(); j++) {
            QDomElement child = children.at(j).toElement();
            if (child.nodeName().toLower() == "t") {
                QString text = child.text();
                int weight = child.attribute("w").toInt(); // Image weight
                if (weight > MINIMUM_RECOGNITION_WEIGHT) {

                    // Check to see if this word matches something we're looking for
                    for (int k=0; k<enSearch.hilightWords.size(); k++) {
                        QString searchWord = enSearch.hilightWords[k].toLower();
                        if (searchWord.endsWith("*"))
                            searchWord.chop(1);
                        if (text.toLower().contains(searchWord))
                            p2.drawRect(x,y,w,h);
                    }
                }
            }
        }
    }
    p2.end();

    // Paint the highlight onto the background & save over the original
    p2.setOpacity(0.4);
    p2.drawPixmap(0,0,overlayPix);
    p.end();
    highlightedPix.save(f.fileName());
}