camerauis/cameraxui/cxengine/src/cxesnapshotcontrolprivate.cpp
author hgs
Thu, 15 Jul 2010 01:55:05 +0300
changeset 43 0e652f8f1fbd
permissions -rw-r--r--
201027_1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     1
/*
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     3
* All rights reserved.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     8
*
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
     9
* Initial Contributors:
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    11
*
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    12
* Contributors:
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    13
*
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    14
* Description:
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    15
*
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    16
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    17
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    18
#include <QSize>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    19
#include <QPixmap>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    20
// Note: Keep atleast one Qt include before preprocessor flags
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    21
#ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    22
#include <e32base.h>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    23
#include <fbs.h>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    24
#include <ecam.h>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    25
#include <ecam/camerasnapshot.h>
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    26
#endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    27
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    28
#include "cxutils.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    29
#include "cxeerror.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    30
#include "cxeexception.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    31
#include "cxeerrormappingsymbian.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    32
#include "cxecameradevice.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    33
#include "cxestate.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    34
#include "cxesnapshotcontrol.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    35
#include "cxesnapshotcontrolprivate.h"
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    36
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    37
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    38
namespace
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    39
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    40
    const int MAINTAIN_ASPECT = false;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    41
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    42
    const QSize ASPECT_RATIO_SIZE_4BY3  = QSize(4,3);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    43
    const QSize ASPECT_RATIO_SIZE_16BY9 = QSize(16, 9);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    44
    const QSize ASPECT_RATIO_SIZE_11BY9 = QSize(11, 9);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    45
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    46
#ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    47
    /*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    48
    * Helper class for cleaning up MCameraBuffer instances.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    49
    */
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    50
    class CxeCameraBufferCleanup
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    51
    {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    52
    public:
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    53
        explicit CxeCameraBufferCleanup(MCameraBuffer *buffer)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    54
            : mBuffer(buffer)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    55
        {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    56
        }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    57
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    58
        ~CxeCameraBufferCleanup()
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    59
        {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    60
            if (mBuffer) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    61
                CX_DEBUG(("CxeCameraBufferCleanup - releasing MCameraBuffer.."));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    62
                mBuffer->Release();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    63
                mBuffer = NULL;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    64
            }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    65
        }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    66
    private:
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    67
        Q_DISABLE_COPY(CxeCameraBufferCleanup)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    68
        MCameraBuffer *mBuffer;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    69
    };
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    70
#endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    71
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    72
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    73
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    74
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    75
* Constructor.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    76
* @param parent Public interface of Snapshot control.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    77
* @param device Camera device interface, used for getting hold of adaptation snapshot API.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    78
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    79
CxeSnapshotControlPrivate::CxeSnapshotControlPrivate(CxeSnapshotControl *parent,
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    80
                                                     CxeCameraDevice& device)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    81
    : CxeStateMachine("CxeSnapshotControlPrivate"),
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    82
      q(parent),
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    83
      mDevice(device)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    84
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    85
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    86
    CX_ASSERT_ALWAYS(q);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    87
    initializeStates();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    88
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    89
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    90
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    91
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    92
* Destructor.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    93
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    94
CxeSnapshotControlPrivate::~CxeSnapshotControlPrivate()
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    95
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    96
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    97
    stop();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    98
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
    99
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   100
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   101
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   102
* Called when state changes.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   103
* @param newStateId Id of the new state.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   104
* @param error Error code or CxeError::None in successful case.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   105
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   106
void CxeSnapshotControlPrivate::handleStateChanged(int newStateId, CxeError::Id error)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   107
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   108
    emit q->stateChanged(static_cast<CxeSnapshotControl::State>(newStateId), error);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   109
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   110
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   111
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   112
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   113
* Get the state of Snapshot Control.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   114
* @return The current state.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   115
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   116
CxeSnapshotControl::State CxeSnapshotControlPrivate::state() const
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   117
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   118
    return static_cast<CxeSnapshotControl::State> (stateId());
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   119
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   120
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   121
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   122
* Initialize Snapshot Control states
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   123
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   124
void CxeSnapshotControlPrivate::initializeStates()
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   125
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   126
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   127
    // addState( id, name, allowed next states )
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   128
    addState(new CxeState(CxeSnapshotControl::Idle, "Idle", CxeSnapshotControl::Active));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   129
    addState(new CxeState(CxeSnapshotControl::Active, "Active", CxeSnapshotControl::Idle));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   130
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   131
    setInitialState(CxeSnapshotControl::Idle);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   132
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   133
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   134
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   135
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   136
* Calculate snapshot size based on diplay size and image / video output resolution.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   137
* @param displaySize Display size in pixels.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   138
* @param Cxe::AspectRatio Aspect ratio of image/video resolution.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   139
* @return Proposed best snapshot size.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   140
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   141
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   142
QSize CxeSnapshotControlPrivate::calculateSnapshotSize(const QSize& displaySize, Cxe::AspectRatio aspectRatio) const
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   143
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   144
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   145
    CX_DEBUG(("CxeSnapshotControlPrivate - display size      (%d,%d)", displaySize.width(), displaySize.height()));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   146
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   147
    // Take resolution as reference for aspect ratio.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   148
    // Scale keeping aspect ratio to just fit display.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   149
    QSize size;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   150
    
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   151
    if (aspectRatio == Cxe::AspectRatio4to3) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   152
        size = ASPECT_RATIO_SIZE_4BY3;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   153
    } else if (aspectRatio == Cxe::AspectRatio16to9) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   154
        size = ASPECT_RATIO_SIZE_16BY9;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   155
    } else if (aspectRatio == Cxe::AspectRatio11to9) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   156
        size = ASPECT_RATIO_SIZE_11BY9;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   157
    }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   158
    size.scale(displaySize, Qt::KeepAspectRatio);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   159
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   160
    CX_DEBUG(("CxeSnapshotControlPrivate - adjusted final size, (%d,%d)", size.width(), size.height()));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   161
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   162
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   163
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   164
    return size;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   165
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   166
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   167
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   168
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   169
* Start getting snapshots from camera.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   170
* Throws CxeException with CxeError::Id if error encountered.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   171
* @param size Size of the snapshot in pixels.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   172
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   173
void CxeSnapshotControlPrivate::start(const QSize &size)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   174
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   175
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   176
#ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   177
    CCamera::CCameraSnapshot *ss = mDevice.cameraSnapshot();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   178
    CX_ASSERT_ALWAYS(ss);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   179
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   180
    if (ss->IsSnapshotActive()) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   181
        CX_DEBUG(("Stop currently active snapshot.."));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   182
        ss->StopSnapshot();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   183
    }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   184
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   185
    // Prepare snapshot
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   186
    CCamera::TFormat snapFormat = CCamera::EFormatFbsBitmapColor16MU;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   187
    TSize snapSize = TSize(size.width(), size.height());
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   188
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   189
    CX_DEBUG(("Prepare snapshot, size (%d x %d)..", size.width(), size.height()));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   190
    TRAPD(status, ss->PrepareSnapshotL(snapFormat, snapSize, MAINTAIN_ASPECT));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   191
    CxeException::throwIfError(CxeErrorHandlingSymbian::map(status));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   192
    CX_DEBUG(("After prepare ECAM modified size to (%d x %d)..", size.width(), size.height()));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   193
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   194
    CX_DEBUG(("Start snapshot.."));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   195
    ss->StartSnapshot();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   196
#else
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   197
    Q_UNUSED(size);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   198
#endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   199
    setState(CxeSnapshotControl::Active);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   200
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   201
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   202
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   203
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   204
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   205
* Stop getting snapshots from camera.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   206
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   207
void CxeSnapshotControlPrivate::stop()
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   208
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   209
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   210
#ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   211
    if (mDevice.cameraSnapshot()) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   212
        mDevice.cameraSnapshot()->StopSnapshot();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   213
    }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   214
#endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   215
    setState(CxeSnapshotControl::Idle);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   216
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   217
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   218
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   219
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   220
* Helper method for getting the snapshot.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   221
* Throws exception if fetching the snapshot fails.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   222
* @return QImage containing the snapshot.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   223
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   224
QImage CxeSnapshotControlPrivate::snapshot()
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   225
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   226
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   227
    QImage image;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   228
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   229
    #ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   230
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   231
        CFbsBitmap *snapshot = NULL;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   232
        TRAPD(status, {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   233
            RArray<TInt> frameIndex;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   234
            CleanupClosePushL(frameIndex);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   235
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   236
            MCameraBuffer &buffer(mDevice.cameraSnapshot()->SnapshotDataL(frameIndex));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   237
            // Make sure buffer is released on leave / exception.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   238
            // Buffer is released once the cleanup item goes out of scope.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   239
            CxeCameraBufferCleanup cleaner(&buffer);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   240
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   241
            TInt firstImageIndex(frameIndex.Find(0));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   242
            snapshot = &buffer.BitmapL(firstImageIndex);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   243
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   244
            CleanupStack::PopAndDestroy(); // frameIndex
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   245
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   246
            TSize size = snapshot->SizeInPixels();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   247
            TInt sizeInWords = size.iHeight * CFbsBitmap::ScanLineLength(size.iWidth, EColor16MU) / sizeof(TUint32);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   248
            CX_DEBUG(("size %d x %d, sizeInWords = %d", size.iWidth, size.iHeight, sizeInWords ));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   249
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   250
            CX_DEBUG(("Creating QImage"));
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   251
            image = QImage(size.iWidth, size.iHeight, QImage::Format_RGB32);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   252
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   253
            // Convert to QImage
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   254
            snapshot->LockHeap();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   255
            const uchar *dataPtr = (const uchar*) snapshot->DataAddress();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   256
            uchar *dst = image.bits();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   257
            memcpy(dst, dataPtr, image.numBytes());
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   258
            snapshot->UnlockHeap();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   259
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   260
        });
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   261
        // We throw error with the Symbian error code if there was problems.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   262
        CxeException::throwIfError(status);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   263
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   264
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   265
    #endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   266
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   267
        CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   268
        return image;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   269
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   270
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   271
/*!
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   272
* Handle camera snapshot events.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   273
* @param id Event uid.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   274
* @param error Status code of the event.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   275
*/
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   276
void CxeSnapshotControlPrivate::handleCameraEvent(int id, int error)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   277
{
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   278
    CX_DEBUG_ENTER_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   279
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   280
    // Ignoring all events if not active.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   281
    if (state() == CxeSnapshotControl::Active) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   282
#ifdef Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   283
        if (id == KUidECamEventSnapshotUidValue) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   284
            QImage ss;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   285
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   286
            if (!error) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   287
                try {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   288
                    ss = snapshot();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   289
                } catch (const CxeException& e) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   290
                    // Note: Normally CxeException carries CxeError::Id,
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   291
                    // but we intentionally use Symbian code in getSnapshot
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   292
                    // as it's easier to handle here.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   293
                    error = e.error();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   294
                } catch (...) {
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   295
                    error = KErrGeneral;
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   296
                }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   297
            }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   298
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   299
            // Emit snapshot ready signal through the public interface.
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   300
            emit q->snapshotReady(CxeErrorHandlingSymbian::map(error), ss);
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   301
        }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   302
#else
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   303
        Q_UNUSED(id)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   304
        Q_UNUSED(error)
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   305
#endif // Q_OS_SYMBIAN
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   306
    }
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   307
    CX_DEBUG_EXIT_FUNCTION();
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   308
}
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   309
0e652f8f1fbd 201027_1
hgs
parents:
diff changeset
   310
// end of file