readbarcappview.cpp
来自「barcode readers [ from Image]」· C++ 代码 · 共 523 行
CPP
523 行
/*
ReadBarCAppView. View control for ReadBarC application.
This code is a derivative work of the SnapShot example
application distributed with the Symbian 6.0 SDK.
Copyright (C) 2006 Jon A. Webb
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "EikonEnvironment.h"
#include "ReadBarCAppView.h"
#include "3x3RgbAvg.h"
#include "Debug.h"
#include "FindBarcode.h"
#include "ImageSize.h"
#include "ReadBarcode.h"
#include <ReadBarC.rsg>
#include <aknwaitdialog.h>
#include <aknmessagequerydialog.h>
#include <aknnotewrappers.h>
#include <baclipb.h>
#include <coemain.h>
#include <e32base.h>
#include <gdi.h>
#include <ImageConversion.h>
#include <txtuids.h>
using namespace Core;
const TInt KMaxImageNames = 100;
_LIT(KImagesSavePath, "C:\\Nokia\\Images\\");
_LIT(KImagesSaveName, "RdBc");
_LIT(KImagesSearchName, "RdBc??");
_LIT(KJpgFileExtension, ".jpg");
_LIT(KBmpFileExtension, ".bmp");
// from CCoeControl
// ---------------------------------------------------------------------------
// CReadBarCAppView::Draw()
// The overloaded CCoeControl's Draw() method. Called by the CCoeControl.
// ---------------------------------------------------------------------------
//
void CReadBarCAppView::Draw(const TRect& aRect) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
// TRect drawRect(Rect());
TRect drawRect(aRect);
drawRect.Shrink(1,1);
gc.DrawRect(drawRect);
gc.Clear();
// Draw the captured still image bitmap on the display
if (iBitmap)
{
TPoint pos;
pos.iX = (drawRect.iBr.iX - iBitmap->SizeInPixels().iWidth) / 2;
pos.iY = (drawRect.iBr.iY - iBitmap->SizeInPixels().iHeight) / 2;
gc.DrawBitmap(drawRect, iBitmap);
if (!iFindBarcode->ProcessedImage()) {
CImage image(*iBitmap);
#ifdef FIND
iFindBarcode->DoItL(image);
#endif // FIND
CDebug::InitializeDebugging(gc, Rect());
CDebug::ToggleDebugging(false);
IImageSize size(image);
#ifdef FIND
TRect rect = iFindBarcode->BarcodeRectangle();
#else
TRect rect = iBarcodeFrame;
#endif
//#define SPEED
#ifdef SPEED
Algorithm::C3x3RgbAvg* pAvg = Algorithm::C3x3RgbAvg::NewL();
TTime time;
CleanupStack::PushL(pAvg);
time.UniversalTime();
pAvg->PushL(image);
TTime timeNow;
timeNow.UniversalTime();
TTimeIntervalMicroSeconds diff = timeNow.MicroSecondsFrom(time);
CleanupStack::PopAndDestroy();
TBuf<80> text;
TInt64 us = diff.Int64();
TUint ms = (us / 1000).Low();
text.Format(_L("3x3 average: %d."), ms);
CAknErrorNote* errorNote = new (ELeave) CAknErrorNote;
errorNote->ExecuteLD(text);
#endif
TInt reason = 0;
TRAP(reason, iReadBarcode->DoItL(image, rect));
CDebug::FinishDebugging();
iFindBarcode->SetProcessed();
}
#ifdef FIND
if (iFindBarcode->FoundBarcode()) {
#else
{
#endif
if (iReadBarcode->ReadOk()) {
gc.SetPenColor(KRgbRed);
//gc.SetPenStyle(CGraphicsContext::TPenStyle::ESolidPen);
gc.SetBrushColor(KRgbRed);
gc.SetBrushStyle(CGraphicsContext::TBrushStyle(0));
int actWidth = iBitmap->SizeInPixels().iWidth;
int drawWidth = drawRect.Width();
#ifdef FIND
TRect rect = iFindBarcode->BarcodeRectangle();
#else
TRect rect = iBarcodeFrame;
#endif
rect.iTl.iX = rect.iTl.iX * drawWidth / actWidth;
rect.iBr.iX = rect.iBr.iX * drawWidth / actWidth;
int actHeight = iBitmap->SizeInPixels().iHeight;
int drawHeight = drawRect.Height();
rect.iTl.iY = rect.iTl.iY * drawHeight / actHeight;
rect.iBr.iY = rect.iBr.iY * drawHeight / actHeight;
rect.Move(drawRect.iTl.iX, drawRect.iTl.iY);
gc.DrawRect(rect);
} else {
gc.SetPenColor(KRgbRed);
gc.DrawLine(TPoint(0,0), TPoint(drawRect.Width(), drawRect.Height()));
gc.DrawLine(TPoint(drawRect.Width(),0), TPoint(0, drawRect.Height()));
}
}
}
}
// from MMyCameraObserver
/*!
@function DrawImage
@discussion Show the current image to the user. Used by
the viewfinder.
@param aImage the image.
*/
void CReadBarCAppView::DrawImage(CFbsBitmap* aImage) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
gc.Activate(Window());
TRect drawRect(Rect());
TPoint pos;
pos.iX = (drawRect.iBr.iX - aImage->SizeInPixels().iWidth) / 2;
pos.iY = (drawRect.iBr.iY - aImage->SizeInPixels().iHeight) / 2;
gc.BitBlt(pos,aImage);
gc.SetPenColor(KRgbRed);
gc.SetBrushStyle(CGraphicsContext::TBrushStyle(0));
int actWidth = 640;
int drawWidth = drawRect.Width();
TRect rect = iBarcodeFrame;
rect.iTl.iX = rect.iTl.iX * drawWidth / actWidth;
rect.iBr.iX = rect.iBr.iX * drawWidth / actWidth;
int actHeight = 480;
int drawHeight = drawRect.Height();
rect.iTl.iY = rect.iTl.iY * drawHeight / actHeight;
rect.iBr.iY = rect.iBr.iY * drawHeight / actHeight;
rect.Move(drawRect.iTl.iX, drawRect.iTl.iY);
gc.DrawRect(rect);
gc.Deactivate();
iCoeEnv->WsSession().Flush();
}
// ---------------------------------------------------------------------------
// CReadBarCAppUi::ErrorNotifyL()
// Sends the error notify to the display.
// ---------------------------------------------------------------------------
//
void CReadBarCAppView::ErrorNotifyL(const TDesC16 &aErrorTxt, TInt aError, TBool aNotify)
{
if ((aError != KErrNone) || aNotify)
{
TBuf <20> errotText;
GetErrText(errotText, aError);
TBuf<80> text;
text.Format(_L("%S%d, %S."), &aErrorTxt, aError, &errotText);
CAknErrorNote* errorNote = new (ELeave) CAknErrorNote;
errorNote->ExecuteLD(text);
}
}
/*!
@function PictureTaken
@discussion Called when the camera has finished taking a picture
*/
void CReadBarCAppView::PictureTaken()
{
iFindBarcode->ResetProcessed();
DrawDeferred();
iState = ESnappedImage;
}
/*!
@function SetBitmap
@discussion Called when a new bitmap is provided by the
camera, or the old bitmap should be cleared.
@param aBitmap the bitmap
*/
void CReadBarCAppView::SetBitmap(CFbsBitmap* aBitmap)
{
delete iBitmap;
iBitmap = aBitmap;
}
// Standard Epoc construction sequence
/*!
@function CReadBarCAppView
@discussion Perform the first phase of two phase construction
*/
CReadBarCAppView::CReadBarCAppView()
{
// Add any construction code that can not leave here
// iBarcodeFrame = TRect(231,184,409,295);
iBarcodeFrame = TRect(220,178,420,303);
}
/*!
@function NewL
@discussion Create a CReadBarCAppView object, which will draw itself to aRect
@param aRect the rectangle this view will be drawn to
@result a pointer to the created instance of CReadBarCAppView
*/
CReadBarCAppView* CReadBarCAppView::NewL(const TRect& aRect)
{
CReadBarCAppView* self = CReadBarCAppView::NewLC(aRect);
CleanupStack::Pop();
return self;
}
/*!
@function NewLC
@discussion Create a CReadBarCAppView object, which will draw itself to aRect
@param aRect the rectangle this view will be drawn to
@result a pointer to the created instance of CReadBarCAppView
*/
CReadBarCAppView* CReadBarCAppView::NewLC(const TRect& aRect)
{
CReadBarCAppView* self = new (ELeave) CReadBarCAppView;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
/*!
@function ConstructL
@discussion Perform the second phase construction of a CReadBarCAppView object
@param aRect the rectangle this view will be drawn to
*/
void CReadBarCAppView::ConstructL(const TRect& aRect)
{
iCameraManager = CCameraManager::NewL(*this, aRect);
iState = EViewFinder;
iFindBarcode = CFindBarcode::NewL();
iReadBarcode = CReadBarcode::NewL();
// Create a window for this application view
CreateWindowL();
// Set the windows size
SetRect(aRect);
// Activate the window, which makes it ready to be drawn
ActivateL();
}
/*!
@function ~CReadBarCAppView
@discussion Destroy the object and release all memory objects
*/
CReadBarCAppView::~CReadBarCAppView()
{
delete iCameraManager;
iCameraManager = NULL;
delete iFindBarcode;
iFindBarcode = NULL;
delete iReadBarcode;
iReadBarcode = NULL;
delete iBitmap;
iBitmap = NULL;
}
// ---------------------------------------------------------------------------
// CCameraSnapShotAppView::DrawBorders()
// Draw the display border.
// ---------------------------------------------------------------------------
//
TPoint CReadBarCAppView::DrawBorders(const TSize& aSize) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
gc.Activate(Window());
TRect drawRect(Rect());
gc.Clear();
TPoint pos;
pos.iX = (drawRect.iBr.iX - aSize.iWidth) / 2;
pos.iY = (drawRect.iBr.iY - aSize.iHeight) / 2;
TRect border(pos,aSize);
border.Grow(1,1);
gc.SetPenColor(KRgbBlack);
gc.DrawRect(border);
gc.Deactivate();
iCoeEnv->WsSession().Flush();
return pos;
}
TKeyResponse CReadBarCAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType)
{
if (aType != EEventKey) {
return EKeyWasNotConsumed;
}
if (aKeyEvent.iScanCode == EStdKeyDevice3)
{
if (iState == EViewFinder) {
Snap();
return EKeyWasConsumed;
} else if (iState == ESnappedImage) {
ViewFinder();
return EKeyWasConsumed;
}
}
return EKeyWasNotConsumed;
}
/*!
@function Snap
@discussion Take a picture
*/
void CReadBarCAppView::Snap()
{
ASSERT(iCameraManager != NULL);
iCameraManager->Snap();
iState = ESnappingImage;
}
/*!
@function ViewFinder
@discussion Turn the viewfinder on
*/
void CReadBarCAppView::ViewFinder()
{
ASSERT(iCameraManager != NULL);
iCameraManager->PowerOnStartViewFinder();
iState = EViewFinder;
}
// ---------------------------------------------------------------------------
// CReadBarCAppView::GetErrText()
// Returns error text for given error code.
// ---------------------------------------------------------------------------
//
TDesC16& CReadBarCAppView::GetErrText(TDes16 &aText, TInt aError)
{
switch (aError)
{
case KErrNone:
aText.Copy(_L("OK"));
break;
case KErrNotFound:
aText.Copy(_L("KErrNotFound"));
break;
case KErrNoMemory:
aText.Copy(_L("KErrNoMemory"));
break;
case KErrInUse:
aText.Copy(_L("KErrInUse"));
break;
case KErrNotReady:
aText.Copy(_L("KErrNotReady"));
break;
case KErrNotSupported:
aText.Copy(_L("KErrNotSupported"));
break;
default:
aText = _L("Unknown");
}
return ((TDesC16&)aText);
}
/*
@function ViewState
@discussion the current state of the camera, as understood
by this view.
*/
TShowState CReadBarCAppView::ViewState() const
{
if (iCameraManager == NULL || !iCameraManager->IsReady()) {
return ENotReady;
} else {
return iState;
}
}
// ---------------------------------------------------------------------------
// CReadBarCAppView::SaveCapturedImageL()
// Saves captured image to BMP file.
// searchName format is drive:\directory\file??.bmp where ?? is a wild card replased
// with two characters.
// ---------------------------------------------------------------------------
//
void CReadBarCAppView::SaveCapturedImageL ()
{
if (iState != ESnappedImage)
{
ErrorNotifyL(_L("No image captured: "), KErrNotFound, ETrue);
return;
}
TFileName* imageName = new(ELeave) TFileName;
CleanupStack::PushL(imageName);
TFileName* searchName = new(ELeave) TFileName;
searchName->Format(_L("%S%S%S"),
(TDesC*)&KImagesSavePath, (TDesC*)&KImagesSearchName,
(TDesC*)&KJpgFileExtension);
GetNextFileName ((TDes16&)*imageName, (TDesC16&)*searchName);
delete (searchName);
TInt err = 0;
CImageEncoder* iEncoder = NULL;
TRAP(err,
iEncoder = CImageEncoder::FileNewL(
CEikonEnv::Static()->FsSession(),
(TDesC&)*imageName,
_L8("image/jpeg"),
CImageEncoder::EOptionAlwaysThread)
);
if (err) {
ErrorNotifyL(_L("Image save err: "), err, EFalse);
}
CleanupStack::PushL(iEncoder);
TRequestStatus aStatus = KErrNone;
iEncoder->Convert(&aStatus,*iBitmap);
User::WaitForRequest( aStatus );
if (aStatus != KErrNone) // A some system-wide error codes.
{
ErrorNotifyL(_L("Image save err: "), aStatus.Int(), EFalse);
}
else
{
TInt pos = imageName->LocateReverse('\\');
TBufC <15> name(imageName->Right(imageName->Length() - (pos + 1)));
TBuf <50> buf;
buf.Format(_L("Image %S saved."), &name);
CAknInformationNote* informationNote = new (ELeave)CAknInformationNote;
informationNote->ExecuteLD(buf);
}
CleanupStack::PopAndDestroy(iEncoder);
CleanupStack::PopAndDestroy(imageName);
}
// ---------------------------------------------------------------------------
// CReadBarCAppView::GetNextFileName()
// Search the next captured image BMP file name.
// This function searchs next filename for the file mask drive:\directory\file??.bmp
// where ?? is between 00-99.
// ---------------------------------------------------------------------------
//
TDesC16& CReadBarCAppView::GetNextFileName (TDes16& aName, TDesC16& aSearchName)
{
TParse fs;
fs.Set((TDesC&)aSearchName, NULL, NULL);
TFindFile fileFinder(CEikonEnv::Static()->FsSession());
TBuf <100> scanDir = fs.DriveAndPath();
CDir* fileList;
TInt fileCount = 0;
TInt err = fileFinder.FindWildByDir(aSearchName, scanDir, fileList);
if (err==KErrNone)
{
fileCount = fileList->Count();
delete fileList;
}
if ((fileCount < 0) || (fileCount > (KMaxImageNames - 1)))
{
fileCount = 0;
}
aName.Format(_L("%S%S%02d%S"), &scanDir, (TDesC*)&KImagesSaveName, fileCount, (TDesC*)&KJpgFileExtension);
return ((TDesC16&) aName);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?