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

📄 ocrexampleimageview.cpp

📁 基于symbian 平台 ocr 示例程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    // set active languages
    RArray<TLanguage> languages;
    CleanupClosePushL(languages);

    #ifdef _DEBUG
    _LIT(KInstalledLangs,"OCR: Installed languages:");
    _LIT(KLangId,"OCR: language id: %d");
    RDebug::Print(KInstalledLangs);
    iEngine->Base()->GetInstalledLanguage(languages);
    for (TInt i = 0; i < languages.Count(); ++i)
        {
        RDebug::Print(KLangId, languages[i]);
        }
    languages.Reset();
    #endif

    iSettings.GetLanguages(languages);

    #ifdef _DEBUG
    _LIT(KLangs,"OCR: languages to be set:");
    RDebug::Print(KLangs);
    for (TInt i = 0; i < languages.Count(); ++i)
        {
        RDebug::Print(KLangId, languages[i]);
        }
    #endif

    iEngine->Base()->SetActiveLanguageL(languages);

    CleanupStack::PopAndDestroy(&languages);
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::AnalyzeImageLayoutL()
//
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::AnalyzeImageLayoutL()
    {
    ASSERT(iBitMap->Handle());

    TRAPD(err, InitializeOCREngineL( OCREngineFactory::EEngineLayoutRecognize ));
    if (err)
        {
        _LIT(KInitFailed,"Initializing OCR Engine failed");
        DisplayWarningL(KInitFailed, err);
        }
    else
        {
        // convert the instance from MOCREngineInterface point to
        // MOCREngineLayoutRecognize
        MOCREngineLayoutRecognize* engine =
            static_cast<MOCREngineLayoutRecognize*>(iEngine);

        // layout analysis
        TOCRLayoutSetting layoutSettings;
        layoutSettings.iBrightness = TOCRLayoutSetting::ENormal;
        layoutSettings.iSkew = ETrue;

        // create and execute form. Don't care about return value
        NOCRUtils::CLayoutForm::ExecuteFormLD( layoutSettings,
                                               R_ANALYZE_LAYOUT_FORM_DIALOG);

        #ifdef _DEBUG
        _LIT(KSettings,"OCR: settings skew: %d, brightness: %d");
        RDebug::Print(KSettings, layoutSettings.iSkew, layoutSettings.iBrightness);
        #endif

        // async operation. LayoutComplete() will be called, when ready
        TRAP(err, engine->LayoutAnalysisL(iBitMap->Handle(), layoutSettings));
        if (err)
            {
            _LIT(KLayoutFailed,"Starting analysis failed");
            DisplayWarningL(KLayoutFailed, err);
            }
        else
            {
             // this is a blocking call
            if (! ExecuteWaitNoteL())
                {
                // cancelled by user
                engine->Base()->CancelOperation();
                }
            }
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeImageL()
//
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeImageL()
    {
    ASSERT(iData.iBlockCount > 0);
    ASSERT(iData.iBlockInfo != NULL);
    ASSERT(iEngine != NULL);

    RArray<TInt> blockIndex;
    CleanupClosePushL(blockIndex);

    for (TInt i = 0; i < iData.iBlockCount; ++i)
        {
        blockIndex.Append(i);
        }

    TOCRRecognizeSetting dummy;
    dummy.iAny = NULL;

    // convert the instance from MOCREngineInterface point to
    // MOCREngineLayoutRecognize
    MOCREngineLayoutRecognize* engine = static_cast<MOCREngineLayoutRecognize*>(iEngine);

    // async operation. RecognizeComplete() will be called, when ready
    TRAPD(err, engine->RecognizeL(dummy, blockIndex));
    if (err)
        {
        _LIT(KFailed,"Starting recognition failed");
        DisplayWarningL(KFailed, err);
        }

    CleanupStack::PopAndDestroy(&blockIndex);
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeBlockL()
//
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeBlockL()
    {
    ASSERT( iBitMap->Handle() );
    ASSERT( iData.iCrosshair != NULL );

    TRAPD(err, InitializeOCREngineL( OCREngineFactory::EEngineRecognizeBlock ));
    if (err)
        {
        _LIT(KInitFailed,"Initializing OCR Engine failed");
        DisplayWarningL(KInitFailed, err);
        }
    else
        {
        // convert the instance from MOCREngineInterface point to
        // MOCREngineLayoutRecognize
        MOCREngineRecognizeBlock* engine =
            static_cast<MOCREngineRecognizeBlock*>(iEngine);

        // fill struct with default values
        TOCRLayoutBlockInfo layoutInfo;
        layoutInfo.iLayout = EOcrLayoutTypeH;
        layoutInfo.iText = EOcrTextMultiLine;
        layoutInfo.iBackgroundColor = EOcrBackgroundLight;

        // calculate rect based on scaled crosshair
        layoutInfo.iRect = iData.iRectScaler.ScaleUp( *(iData.iCrosshair) );

        // create and execute form. Don't care about return value
        NOCRUtils::CLayoutForm::ExecuteFormLD( layoutInfo,
                                               R_RECOGNIZE_BLOCK_FORM_DIALOG);

        #ifdef _DEBUG
        _LIT(KSettings,"OCR: settings layout: %d, text: %d, brightness: %d");
        RDebug::Print(KSettings, layoutInfo.iLayout, layoutInfo.iText, layoutInfo.iBackgroundColor);
        #endif

        // async operation. LayoutComplete() will be called, when ready
        TRAP(err, engine->RecognizeBlockL(iBitMap->Handle(), layoutInfo));
        if (err)
            {
            _LIT(KFailed,"Starting recognize block failed");
            DisplayWarningL(KFailed, err);
            }
        else
            {
             // this is a blocking call
            if (! ExecuteWaitNoteL())
                {
                // cancelled by user
                engine->Base()->CancelOperation();
                }
            }
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeSpecialRegionL()
//
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeSpecialRegionL()
    {
    ASSERT( iBitMap->Handle() );
    ASSERT( iData.iCrosshair != NULL );

    TRAPD(err, InitializeOCREngineL( OCREngineFactory::EEngineRecognizeBlock ));
    if (err)
        {
        _LIT(KInitFailed,"Initializing OCR Engine failed");
        DisplayWarningL(KInitFailed, err);
        }
    else
        {
        // convert the instance from MOCREngineInterface point to
        // MOCREngineLayoutRecognize
        MOCREngineRecognizeBlock* engine =
            static_cast<MOCREngineRecognizeBlock*>(iEngine);

        // fill struct with default values
        TRegionInfo regionInfo;
        regionInfo.iBackgroundColor = EOcrBackgroundLight;
        regionInfo.iType = TRegionInfo::EEmailAddress;

        // calculate rect based on scaled crosshair
        regionInfo.iRect = iData.iRectScaler.ScaleUp( *(iData.iCrosshair) );

        // create and execute form. Don't care about return value
        NOCRUtils::CLayoutForm::ExecuteFormLD( regionInfo,
                                               R_RECOGNIZE_REGION_FORM_DIALOG);

        #ifdef _DEBUG
        _LIT(KSettings,"OCR: settings type: %d, brightness: %d");
        RDebug::Print(KSettings, regionInfo.iType, regionInfo.iBackgroundColor);
        #endif

        // async operation. LayoutComplete() will be called, when ready
        TRAP(err, engine->RecognizeSpecialRegionL(iBitMap->Handle(), regionInfo));
        if (err)
            {
            _LIT(KFailed,"Starting recognize special region failed");
            DisplayWarningL(KFailed, err);
            }
        else
            {
             // this is a blocking call
            if (! ExecuteWaitNoteL())
                {
                // cancelled by user
                engine->Base()->CancelOperation();
                }
            }
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::LayoutComplete()
// callback function called by OCR Engine
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::LayoutComplete( TInt aError,
                                           const TOCRBlockInfo* aBlocks,
                                           const TInt aBlockCount )
    {
    TRAP_IGNORE( LayoutCompleteL(aError, aBlocks, aBlockCount));
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::LayoutCompleteL()
//
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::LayoutCompleteL( TInt aError,
                                            const TOCRBlockInfo* aBlocks,
                                            const TInt aBlockCount )
    {
    iBackgroundTaskDone = ETrue; // to dismiss wait-note

    if (aError)
        {
        _LIT(KFailed,"Layout analyzis of the image failed");
        DisplayWarningL(KFailed, aError);
        }
    else
        {
        iState = EImageAnalyzed;

        iData.iBlockInfo = aBlocks;
        iData.iBlockCount = aBlockCount;

        // content changed in UI
        iContainer->DrawDeferred();

        RFile       file;
        TFileText   fileText;

	    TInt err = file.Open(iFs, KLogFile, EFileWrite);
	    if (err == KErrNone)
	        {
    	    fileText.Set(file);

            // in a real life this should by dynamic
            TBuf<64> tmp;
            _LIT(KFormat1, "Layout analyzed, block count %d:");
            _LIT(KFormat2, " %d: layout %S");
            _LIT(KHor, "horizontal");
            _LIT(KVer, "vertical");
            tmp.Format(KFormat1, aBlockCount);

            fileText.Write(tmp);
            for (TInt i=0; i<aBlockCount; ++i)
                {
                tmp.Zero();
                tmp.Format(KFormat2, i+1,
                          ((aBlocks[i].iType == EOcrLayoutTypeH) ? &KHor : &KVer));
                fileText.Write(tmp);
                }
            file.Close();

            // switch to text-view to show the results
            AppUi()->ActivateLocalViewL(KUidTextView);
            }
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeDoneL()
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeDoneL( TInt aError,
                                           const TOCRTextRgnInfo* aBlocks,
                                           const TInt aBlockCount)
    {
    iBackgroundTaskDone = ETrue; // to dismiss wait-note

    if ((aError) && (aError != KErrCancel))
        {
        _LIT(KFailed,"Recognition of the image failed");
        DisplayWarningL(KFailed, aError);
        }
    else if (!aError)
        {
        iState = EImageRecognized;

        RFile       file;
        TFileText   fileText;

	    TInt err = file.Open(iFs, KLogFile, EFileWrite);
	    if (err == KErrNone)
	        {
    	    fileText.Set(file);

            // in a real life this should by dynamic
            TBuf<256> tmp;

            _LIT(KFormat2, "Block %d recognized, line count %d:");
            _LIT(KText, " %d: %S");
            for (TInt i = 0; i < aBlockCount; ++i)
                {
                tmp.Zero();
                TInt lines = aBlocks[i].iLineCount;
                tmp.Format(KFormat2, i+1, lines);
                fileText.Write(tmp);

                TOCRTextLineInfo *texts = aBlocks[i].iLines;
                for (TInt j = 0; j < lines; ++j)
                    {
                    tmp.Zero();
                    tmp.Format(KText, j+1, texts[j].iText);
                    fileText.Write(tmp);
                    }
                }
            file.Close();

            // switch to text-view to show the results
            AppUi()->ActivateLocalViewL(KUidTextView);
            }
        else
            {
            // file open failed, there's no switch to text-view.
            // we need to order draw...
            iContainer->DrawDeferred();
            }
        }
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeProcess()
// callback function called by OCR Engine
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeProcess(const TUint aPercent)
    {
    _LIT(KFormat, "OCR: Progress: %d%%\n");
    RDebug::Print(KFormat, aPercent);
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeComplete()
// callback function called by OCR Engine
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeComplete( TInt aError,
                                           const TOCRTextRgnInfo* aBlocks,
                                           const TInt aBlockCount)
    {
    TRAP_IGNORE( RecognizeDoneL( aError, aBlocks, aBlockCount ));
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeBlockComplete()
// callback function called by OCR Engine
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeBlockComplete( TInt aError,
                                                const TOCRTextRgnInfo* aBlocks,
                                                const TInt aBlockCount)
    {
    TRAP_IGNORE( RecognizeDoneL( aError, aBlocks, aBlockCount ));
    }

// ----------------------------------------------------------------------------
// COcrExampleImageView::RecognizeSpecialRegionComplete()
// callback function called by OCR Engine
// ----------------------------------------------------------------------------
//
void COcrExampleImageView::RecognizeSpecialRegionComplete(TInt aError,
                                            const TOCRTextRgnInfo* aBlocks,
                                            const TInt aBlockCount)
    {
    TRAP_IGNORE( RecognizeDoneL( aError, aBlocks, aBlockCount ));
    }

// End of File

⌨️ 快捷键说明

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