examples/Qt/facerecgapp/dummythumbnailer.cpp

00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the examples of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:BSD$
00010 ** You may use this file under the terms of the BSD license as follows:
00011 **
00012 ** "Redistribution and use in source and binary forms, with or without
00013 ** modification, are permitted provided that the following conditions are
00014 ** met:
00015 **   * Redistributions of source code must retain the above copyright
00016 **     notice, this list of conditions and the following disclaimer.
00017 **   * Redistributions in binary form must reproduce the above copyright
00018 **     notice, this list of conditions and the following disclaimer in
00019 **     the documentation and/or other materials provided with the
00020 **     distribution.
00021 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
00022 **     the names of its contributors may be used to endorse or promote
00023 **     products derived from this software without specific prior written
00024 **     permission.
00025 **
00026 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00030 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
00037 ** $QT_END_LICENSE$
00038 **
00039 ****************************************************************************/
00040 
00041 #include "dummythumbnailer.h"
00042 #include <QImageReader>
00043 #include <QFile>
00044 #include <QFileInfo>
00045 #include <QDebug>
00046 #include <QDir>
00047 
00048 
00049 #if defined (__SYMBIAN32__) || defined(_WIN32)
00050 const QString gThumbnailPath("E:/Thumbnails/");
00051 #else
00052 const QString gThumbnailPath("./Thumbnails/");
00053 #endif
00054 
00055 DummyFaceThumbnailer::DummyFaceThumbnailer(QObject *parent)
00056     : XQFaceThumbnailer(parent)
00057 {
00058 
00059 }
00060 
00061 DummyFaceThumbnailer::~DummyFaceThumbnailer()
00062 {
00063 
00064 }
00065 
00066 void DummyFaceThumbnailer::createThumbnail(const QString &sourceId, XQFaceThumbnailer::ThumbnailSizeHint sizeHint, const QSize &preferredSize)
00067 {
00068     /*create requested thumbnail size*/
00069     QImageReader reader(sourceId);
00070 
00071     /*get the size information*/
00072     QSize imageSize = reader.size();
00073 
00074     /*If the image size is not valid or the image's size > preferred size, set preferred size as scaled one*/
00075     if(!(imageSize.isValid()) || ((imageSize.width() * imageSize.height()) > (preferredSize.width()* preferredSize.height())))
00076     {
00077         const QSize prefszpreserveAR (preferredSize.width(),
00078                                         (imageSize.height()*preferredSize.width())/imageSize.width());
00079 
00080         reader.setScaledSize(prefszpreserveAR);
00081     }
00082 
00083     /*emit thumbnail created*/
00084     emit thumbnailCreated(reader.read(), sourceId, sizeHint);
00085 }
00086 
00087 void DummyFaceThumbnailer::createFaceThumbnail(const QString &sourceId, const QSize &sourceSize, const QRect &thumbnailRect)
00088 {
00089     /*If thumbnail rect passed by user is not valid, return null string*/
00090     if (!(thumbnailRect.isValid()) || thumbnailRect.isNull())
00091     {
00092         /*emit a empty string for this image*/
00093         emit faceThumbnailCreated(QString(), sourceId, sourceSize, thumbnailRect);
00094         return;
00095     }
00096 
00097     /*Read image*/
00098     QImageReader reader(sourceId);
00099     reader.setScaledSize(sourceSize);
00100     QImage image = reader.read();
00101 
00102     /*if image is null*/
00103     if (image.isNull())
00104     {
00105         /*emit a empty string for this image*/
00106         emit faceThumbnailCreated(QString(), sourceId, sourceSize, thumbnailRect);
00107         return;
00108     }
00109 
00110 
00111     QImage faceimg;
00112 
00113     /*Prior calculation for extracting the thumbnail from right area*/
00114     int perw =  thumbnailRect.width() * 0.4;
00115     int perh =  thumbnailRect.height() * 0.4;
00116 
00117     int x1 = thumbnailRect.x()- perw;
00118     if (x1 < 0)
00119     {
00120         x1 = 0;
00121     }
00122 
00123     int y1 = thumbnailRect.y()- 2*perh;
00124     if (y1 < 0)
00125     {
00126         y1 = 0;
00127     }
00128 
00129     int x2 = thumbnailRect.width() + thumbnailRect.x() + perw;
00130     if (x2 > image.width())
00131     {
00132         x2 = image.width();
00133     }
00134 
00135     int y2 = thumbnailRect.height() + thumbnailRect.y() + 2*perh;
00136     if (y2 > image.height())
00137     {
00138         y2 = image.height();
00139     }
00140 
00141 
00142     int tmpx = x1;
00143     int tmpy = y1;
00144     int tmpw = x2-x1;
00145     int tmph = y2-y1;
00146 
00147 
00148     if(tmpx < 0)
00149     {
00150         tmpx=0;
00151     }
00152 
00153     if (tmpy < 0)
00154     {
00155         tmpy=0;
00156     }
00157 
00158     if(tmpx + tmpw > image.width())
00159     {
00160         tmpw = image.width()-tmpx;
00161     }
00162 
00163     if (tmpy + tmph > image.height())
00164     {
00165         tmph = image.height()-tmpy;
00166     }
00167 
00168     /*create the required size*/
00169     QRect temp(tmpx,tmpy,tmpw,tmph);
00170     faceimg = image.copy(temp);
00171 
00172 
00173     /*create thumbnailpath*/
00174     QString tnPath(gThumbnailPath);
00175 
00176     QDir dir(gThumbnailPath);
00177 
00178     if(!dir.exists())
00179       {
00180       dir.mkpath(gThumbnailPath);
00181       }
00182 
00183     /*Append the basename of the sourcefile*/
00184     QFileInfo fileInfo(sourceId);
00185     tnPath.append(fileInfo.completeBaseName());
00186     tnPath.append('_');
00187 
00188     /*Append top left x coordinate*/
00189     tnPath.append(QString().setNum(thumbnailRect.x()));
00190     tnPath.append('_');
00191 
00192     /*Append top left y coordinate*/
00193     tnPath.append(QString().setNum(thumbnailRect.y()));
00194     tnPath.append('_');
00195 
00196     /*Append width info*/
00197     tnPath.append(QString().setNum(thumbnailRect.width()));
00198     tnPath.append('_');
00199 
00200     /*Append height info*/
00201     tnPath.append(QString().setNum(thumbnailRect.height()));
00202     tnPath.append(QString(".jpg"));
00203 
00204     /*Save the image in that path and emit appropriate signal*/
00205     if(faceimg.save(tnPath, "JPG"))
00206     {
00207         emit faceThumbnailCreated(tnPath, sourceId, sourceSize, thumbnailRect);
00208     }
00209     else
00210     {
00211         qDebug()<<"Saving the thumbnail failed in dummy thumbnailer. Check if path exists!";
00212         emit faceThumbnailCreated(QString(), sourceId, sourceSize, thumbnailRect);
00213     }
00214 }
00215 
00216 void DummyFaceThumbnailer::thumbnailNotUsed(const QStringList &thumbnailPaths)
00217 {
00218     foreach(QString tnPath, thumbnailPaths)
00219     {
00220         /*Check whether the file exists*/
00221         if(QFile::exists(tnPath))
00222         {
00223             /*remove that file*/
00224             QFile file(tnPath);
00225             file.remove();
00226         }
00227     }
00228 }

Generated by  doxygen 1.6.2