⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ocrexampleimageview.cpp

📁 基于symbian 平台 ocr 示例程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
* ==============================================================================
*  Name        : OcrExampleImageView.cpp
*  Part of     : OcrExampleImageView
*  Interface   :
*  Description :
*  Version     :
*
*  Copyright (c) 2006 Nokia Corporation.
*  This material, including documentation and any related
*  computer programs, is protected by copyright controlled by
*  Nokia Corporation.
* ==============================================================================
*/

// INCLUDE FILES
#include <aknutils.h>
#include <f32file.h>
#include <s32file.h>
#include <aknnotewrappers.h>
#include <akncommondialogs.h>

#include "OcrExampleAppUi.h"
#include <OcrExample.rsg>
#include "OcrExample.hrh"
#include "OcrExampleUids.h"
#include "OcrExampleImageContainer.h"
#include "OcrExampleSettingsData.h"
#include "OcrExampleImageView.h"
#include "LayoutForm.h"

// ========================= MEMBER FUNCTIONS ================================

// ---------------------------------------------------------------------------
// COcrExampleImageView::COcrExampleImageView
// C++ constructor
// ---------------------------------------------------------------------------
//
COcrExampleImageView::COcrExampleImageView(TOcrExampleSettingsData& aSettings)
    : iContainer(0)
    , iBitMap(0)
    , iHandler(0)
    , iEngine(0)
    , iSettings(aSettings)
    , iState(EInit)
    , iCrosshairEnabled(EFalse)
    , iBackgroundTaskDone(EFalse)
    {
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView.::NewLC
// Symbian OS two-phased constructor.
// ---------------------------------------------------------------------------
//
COcrExampleImageView* COcrExampleImageView::NewL(TOcrExampleSettingsData& aSettings)
    {
    COcrExampleImageView* self = new (ELeave) COcrExampleImageView(aSettings);
    self->ConstructL();
    return self;
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::ConstructL
// Symbian OS 2nd phase constructor
// ---------------------------------------------------------------------------
//
void COcrExampleImageView::ConstructL()
    {
    BaseConstructL(R_OCR_EXAMPLE_IMAGE_VIEW);
    iContainer = COcrExampleImageContainer::NewL(ClientRect(), iSettings, iData);
    iContainer->SetMopParent( this );

    AppUi()->AddToStackL( *this, iContainer );

    // create a new log-file
	User::LeaveIfError(iFs.Connect());
    RFile file;
	User::LeaveIfError(file.Replace(iFs, KLogFile, EFileWrite|EFileShareAny ));
	file.Close();

    // bitmap for the original image
    iBitMap = new (ELeave) CFbsBitmap;

    // bitmap for the image fitted to the screen
    iData.iScaledBitMap = new (ELeave) CFbsBitmap;

    // create handler to deal with asynchronous image loading and scaling
    iHandler = NOCRUtils::CImageHandler::NewL( iBitMap,
                                               iData.iScaledBitMap,
                                               iFs,
                                               *this );
    iContainer->ActivateL();
    }


// Destructor
COcrExampleImageView::~COcrExampleImageView()
    {
    if (iContainer)
        {
        AppUi()->RemoveFromStack(iContainer);
        delete iContainer;
        iContainer = NULL;
        }
    if (iEngine)
        {
        OCREngineFactory::ReleaseOCREngine(iEngine);
        }
    delete iHandler;
    delete iBitMap;
    delete iData.iScaledBitMap;
    delete iData.iCrosshair;
    iFs.Close();
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::Id
// From CAknView, returns Uid of View
// ---------------------------------------------------------------------------
//
TUid COcrExampleImageView::Id() const
    {
    return KUidImageView;
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::HandleCommandL
// From MEikMenuObserver delegate commands from the menu
// ---------------------------------------------------------------------------
//
void COcrExampleImageView::HandleCommandL(TInt aCommand)
    {
    switch (aCommand)
        {
        case EOcrExampleCmdOpenFile:
            OpenImageL();
        	break;

        case EOcrExampleCmdAnalyzeImage:
            AnalyzeImageLayoutL();
            break;

        case EOcrExampleCmdRecognizeImage:
            RecognizeImageL();
            break;

        case EOcrExampleCmdRecognizeBlock:
            RecognizeBlockL();
            break;

        case EOcrExampleCmdRecognizeRegion:
            RecognizeSpecialRegionL();
            break;

        case EOcrExampleCmdToggleCrosshair:
            if (iData.iCrosshair)
                {
                delete iData.iCrosshair;
                iData.iCrosshair = 0;
                }
            else
                {
                // note! not leaving new. if it fails, it fails.
                iData.iCrosshair = new TRect;
                }
            iCrosshairEnabled = (iData.iCrosshair != NULL);
            iContainer->DrawNow();
            break;

        case EOcrExampleCmdSettings:
            AppUi()->ActivateLocalViewL(KUidSettingsView);
            break;

        default:
            AppUi()->HandleCommandL( aCommand );
            break;
        }
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::DoActivateL
// Activate this view
// ---------------------------------------------------------------------------
//
void COcrExampleImageView::DoActivateL( const TVwsViewId& /* aPrevViewId */,
                                        TUid /*aCustomMessageId*/,
                                        const TDesC8& /*aCustomMessage*/ )
    {
    ASSERT(iContainer);
    iContainer->MakeVisible(ETrue);
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::DoDeactivate
// Deactivate this view
// Note! The container is not deleted, because we want to maintain existing
// view until re-activated.
// ---------------------------------------------------------------------------
//
void COcrExampleImageView::DoDeactivate()
    {
    if (iContainer)
        {
        iContainer->MakeVisible(EFalse);
        }
    }


// ---------------------------------------------------------------------------
// COcrExampleImageView::DynInitMenuPaneL
// Dynamically customize menu items
// ---------------------------------------------------------------------------
//
void COcrExampleImageView::DynInitMenuPaneL( TInt aResourceId,
                                             CEikMenuPane* aMenuPane )
    {
    if ( aResourceId == R_IMAGE_VIEW_MENU )
        {
        aMenuPane->SetItemDimmed( EOcrExampleCmdAnalyzeImage,
                                  ((iState == EInit) || iCrosshairEnabled));
        aMenuPane->SetItemDimmed( EOcrExampleCmdRecognizeImage,
                                  ((iState != EImageAnalyzed) || iCrosshairEnabled));
        aMenuPane->SetItemDimmed( EOcrExampleCmdToggleCrosshair,
                                  (iState == EInit));
        aMenuPane->SetItemDimmed( EOcrExampleCmdRecognizeBlockMenu,
                                  ((iState == EInit) || (!iCrosshairEnabled)));
        }
    }


// -----------------------------------------------------------------------------
// COcrExampleImageView::HandleSizeChange
// Handle screen size change.
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::HandleSizeChange()
    {
    if (iContainer)
        {
        TRect mainPaneRect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);

        // if image is loaded, re-scale it
        if ((iHandler) && (iBitMap))
            {
            TRAP_IGNORE(iHandler->FitToScreenL( mainPaneRect.Size() ));
            }

        iContainer->SetRect(mainPaneRect);
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::ImageOperationCompleteL()
// callback function called by ImageHandler, when an image has been loaded or
// scaled
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::ImageOperationCompleteL(TInt aError)
    {
    iBackgroundTaskDone = ETrue; // to dismiss wait-note
    if (aError)
        {
        _LIT(KFailed,"Loading/scaling image failed");
        DisplayWarningL(KFailed, aError);
        }
    else
        {
        // we should now have the image and also another one (resized to fit
        // the screen). Calculate the factors (needed later, when adjusting
        // TRect-info analyzed from the original image)
        //
        iData.iRectScaler.CalculateFactor(iBitMap->SizeInPixels(),
                                          iData.iScaledBitMap->SizeInPixels());
        if (iState == EInit)
            {
            iState = EImageOpened;
            }
        iContainer->DrawDeferred();
        }
    }

void COcrExampleImageView::DialogDismissedL(TInt /*aButtonId*/)
    {
    // no implementation needed
    }

TBool COcrExampleImageView::IsProcessDone() const
    {
    return iBackgroundTaskDone;
    }

void COcrExampleImageView::ProcessFinished()
    {
    iBackgroundTaskDone = EFalse;
    }

void COcrExampleImageView::StepL()
    {
    // no implementation needed
    }

TBool COcrExampleImageView::ExecuteWaitNoteL()
    {
	CAknWaitNoteWrapper* waitNote = CAknWaitNoteWrapper::NewL();

	// Required reinterpret_cast as CAknWaitNoteWrapper inherits privately
	// from CActive
	CleanupStack::PushL(reinterpret_cast<CBase*>(waitNote));

	// this is a blocking call
	TBool completed = waitNote->ExecuteL(R_OCR_WAITNOTE, *this);

	CleanupStack::PopAndDestroy(waitNote);

	return completed;
    }

// -----------------------------------------------------------------------------
// COcrExampleImageView::DisplayWarningL()
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::DisplayWarningL( const TDesC &aDescr, TInt aErr) const
    {
    ASSERT( aErr != KErrNone );

    _LIT(KFailed,"%S: %d");
    CAknWarningNote* note = new (ELeave) CAknWarningNote(ETrue);
    TBuf<128> text;
    text.Format(KFailed, &aDescr, aErr);
    note->ExecuteLD( text );
    }

// -----------------------------------------------------------------------------
// COcrExampleImageView::OpenImageL()
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::OpenImageL()
    {
    // use dialog
    _LIT(KTitle, "Select image file");
    TFileName file(KNullDesC);
    if (AknCommonDialogs::RunSelectDlgLD(file, R_OCR_SELECT_DIALOG, KTitle))
        {
        // close possible previously loaded image
        CloseImage();

        TRect mainPaneRect;
        AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
        TRAPD(err, iHandler->LoadFileAndScaleL( file, mainPaneRect.Size() ));
        if (err)
            {
            _LIT(KFileLoadFailed,"Opening image file failed");
            DisplayWarningL(KFileLoadFailed, err);
            }
        else
            {
            // this is a blocking call
            if (! ExecuteWaitNoteL())
                {
                // cancelled
                iHandler->Cancel();
                }
            }
        }
    }

// -----------------------------------------------------------------------------
// COcrExampleImageView::CloseImage()
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::CloseImage()
    {
    iBitMap->Reset();
    iData.iScaledBitMap->Reset();
    iState = EInit;
    ReleaseOCREngine();
    }

// -----------------------------------------------------------------------------
// COcrExampleImageView::ReleaseOCREngine()
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::ReleaseOCREngine()
    {
    if (iEngine)
        {
        OCREngineFactory::ReleaseOCREngine(iEngine);
        iEngine = NULL;

        if (iData.iBlockInfo != NULL)
            {
            iData.iBlockInfo = NULL;
            iData.iBlockCount = 0;
            // content changed in UI
            iContainer->DrawDeferred();
            }
        }
    }

// -----------------------------------------------------------------------------
// COcrExampleImageView::InitializeOCREngineL()
// -----------------------------------------------------------------------------
//
void COcrExampleImageView::InitializeOCREngineL(
                            OCREngineFactory::TEngineType aEngineType )
    {
    TOcrEngineEnv env;
    env.iPriority = EPriorityLess;          // thread's priority
    env.iMaxHeapSize = 1200*KMinHeapGrowBy; // thread's heap maximum size

    // release create the OCR engine instance.
    ReleaseOCREngine();

    iEngine = OCREngineFactory::CreateOCREngineL(*this, env, aEngineType);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -