📄 cameramanager.cpp
字号:
/*
CameraManager. Controls cellphone camera.
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 <CameraServ.h>
#include "CameraManager.h"
#include "ReadBarCAppView.h"
// These are the image capture formats whose display modes I know
// how to convert to gray. They are be listed in order of preference.
//
// Note that these are capture formats. The corresponding
// display modes, which are used elsewhere in the software,
// are shown in the comments.
//
// Note that the other capture formats might be acceptable because they
// might return images in the four display modes I can handle. These
// formats are documented in CCamera::TFormat as producing the CDisplayModes
// listed -- except for EFormatMonochrome.
const CCamera::TFormat KUsableCaptureFormats[] = {
CCamera::EFormatMonochrome // doesn't require any conversion, 8 bpp: EGray256 (presumably)
// ,EFormat16bitRGB444
// ,EFormat16BitRGB565
// ,EFormat32BitRGB888
// ,EFormatJpeg
,CCamera::EFormatFbsBitmapColor16M // requires one conversion step, 8 bpp: EColor16M
,CCamera::EFormatFbsBitmapColor64K // requires two steps, 5-6 bpp: EColor64K
,CCamera::EFormatFbsBitmapColor4K // requires two steps, 4 bpp: EColor4K
// ,EFormatYUV420Interleaved
// ,EFormatYUV420Planar
// ,EFormatYUV422
// ,EFormatYUV422Reversed
// ,EFormatYUV444
};
const CCamera::TFormat KReadBarCDefaultImageFormat = CCamera::EFormatFbsBitmapColor64K;
const TInt KReadBarCDefaultImageIndex = 1;
const TInt KDefaultImageWidth = 640;
const TInt KDefaultImageHeight = 480;
/*!
@function FrameBufferReady
@discussion Not implemented
*/
void CCameraManager::FrameBufferReady(MFrameBuffer*, TInt)
{
}
/*!
@function ImageReady
@discussion Transfers the current image from the camera to the client.
The capture is always through a CFbsBitmap so the second parameter
is not used.
@param aBitmap the captured image
@param aError error code if not successful
*/
void CCameraManager::ImageReady(CFbsBitmap* aBitmap, HBufC8*, TInt aError)
{
iObserver.ErrorNotifyL(_L("Image capture error: "), aError, EFalse);
if (aError == KErrNone) {
iObserver.SetBitmap(aBitmap);
iObserver.PictureTaken();
}
iCameraState = ETookPicture;
}
/*!
@function PowerOnComplete
@discussion Camera power on is complete. We immediately start
the viewfinder.
@param aError error code if not successful
*/
void CCameraManager::PowerOnComplete(TInt aError)
{
if (aError != KErrNotReady) {
iObserver.ErrorNotifyL(_L("Power on error: "), aError, EFalse);
}
if (aError == KErrNone) {
iCamera->SetContrastL(0);
TCameraInfo aInfo;
iCamera->CameraInfo(aInfo);
// digital zoom factor doesn't seem to help much at all
//iCamera->SetDigitalZoomFactorL(aInfo.iMaxDigitalZoom);
iCamera->SetZoomFactorL(aInfo.iMaxZoom);
iCamera->SetJpegQuality(100);
iCamera->SetExposureL(CCamera::EExposureAuto);
// determine image capture characteristics
int i=0;
iBestSizeIndex = -1;
TSize size;
TUint j;
iBestViewfinderSize = TSize(0,0);
for (j=0; j<sizeof(KUsableCaptureFormats)/sizeof(CCamera::TFormat); j++) {
if (aInfo.iImageFormatsSupported & KUsableCaptureFormats[j]) {
for (i=0; i<aInfo.iNumImageSizesSupported; i++) {
iCamera->EnumerateCaptureSizes(size, i, KUsableCaptureFormats[j]);
if (iBestSizeIndex == -1 || size.iHeight > iBestSize.iHeight) {
iBestSize = size;
iBestFormat = KUsableCaptureFormats[j];
iBestSizeIndex = i;
}
if (size.iHeight <= iRect.Height() && size.iHeight > iBestViewfinderSize.iHeight) {
iBestViewfinderSize = size;
}
}
}
}
if (iBestSizeIndex == -1) {
iObserver.ErrorNotifyL(_L("No usable image capture format was found"),
KErrGeneral,
ETrue);
}
iObserver.DrawBorders(iBestViewfinderSize);
StartViewFinder();
}
}
/*!
@function ReserveComplete
@discussion Camera reserve is complete. We immediately turn
the camera on.
@param aError error code if not successful
*/
void CCameraManager::ReserveComplete(TInt aError)
{
iObserver.ErrorNotifyL(_L("Reserve error: "), aError, EFalse);
if (aError == KErrNone) {
iCamera->PowerOn();
}
}
/*!
@function ViewFinderFrameReady
@discussion View finder image is ready. We send it to the
app view to be displayed.
@param aBitmap the captured image
@param aError error code if not successful
*/
void CCameraManager::ViewFinderFrameReady(CFbsBitmap& aFrame)
{
iCameraState = EReady;
iObserver.DrawImage(&aFrame);
}
/*!
@function CCameraManager
@discussion Perform the first phase of two phase construction
@param aView the view that is to be updated
*/
CCameraManager::CCameraManager(MMyCameraObserver& aObserver, TRect aRect) :
iCamera(NULL),
iCameraState(EUninitalised),
iObserver(aObserver),
iRect(aRect)
{
// Implementation not required
}
/*!
@function ConstructL
@discussion Perform the second phase construction of a CCameraManager object
*/
void CCameraManager::ConstructL()
{
iCamera = CCamera::NewL(*this,0);
iCameraState = EReservingCamera;
iCamera->Reserve();
}
/*!
@function NewL
@discussion Create a CCameraManager object
@param aObserver the object to be notified when a picture is taken, or an error
occurs
@result a pointer to the created instance of CCameraManager
*/
CCameraManager* CCameraManager::NewL(MMyCameraObserver& aObserver, TRect aRect)
{
CCameraManager* self = CCameraManager::NewLC(aObserver, aRect);
CleanupStack::Pop();
return self;
}
/*!
@function NewLC
@discussion Create a CCameraManager object
@param aObserver the object to be notified when a picture is taken, or an error
occurs
@result a pointer to the created instance of CCameraManager
*/
CCameraManager* CCameraManager::NewLC(MMyCameraObserver& aObserver, TRect aRect)
{
CCameraManager* self = new (ELeave) CCameraManager(aObserver, aRect);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
/*!
@function ~CCameraManager
@discussion Destroy the object and release all memory objects
*/
CCameraManager::~CCameraManager()
{
if (iCamera) {
if (iCameraState == ETakingPicture) {
iCamera->CancelCaptureImage();
}
// Connected to the camera so shut the camera down
if (iCamera->ViewFinderActive()) {
iCamera->StopViewFinder();
}
if (iCameraState >= EStartingCamera) {
iCamera->PowerOff();
}
if (iCameraState >= EReservingCamera) {
iCamera->Release();
}
delete iCamera;
iCamera = NULL;
}
}
/*!
@function PowerOnStartViewFinder
@discussion Power cycle the camera, then start the View Finder.
*/
void CCameraManager::PowerOnStartViewFinder()
{
// sometimes the camera turns itself off and doesn't tell us.
// so we have to turn it off ourselves and turn it back on
// every time we start the viewfinder, in case this happened.
iCamera->PowerOff();
iCamera->PowerOn();
}
// ---------------------------------------------------------------------------
// CReadBarCAppUi::PrepareCaptureImage()
// The client prepares a still image capture.
// ---------------------------------------------------------------------------
//
void CCameraManager::PrepareCaptureImage()
{
TInt err;
TRAP(err, iCamera->PrepareImageCaptureL(iBestFormat, iBestSizeIndex));
iObserver.ErrorNotifyL(_L("Prepare capture err: "), err, EFalse);
iCameraState = ETakingPicture;
}
/*!
@function Snap
@discussion Take a picture.
*/
void CCameraManager::Snap()
{
ASSERT(IsReady());
if (iCamera->ViewFinderActive())
{
TInt err;
TRAP(err, iCamera->StopViewFinder());
iObserver.ErrorNotifyL(_L("View Finder stop err: "), err, EFalse);
}
iCameraState = ETakingPicture;
PrepareCaptureImage();
iCamera->CaptureImage();
}
//
// ---------------------------------------------------------------------------
// CReadBarCAppUi::StartViewFinder()
// Starts the View Finder.
// ---------------------------------------------------------------------------
//
void CCameraManager::StartViewFinder()
{
if (!iCamera) {
return;
}
if (iCamera->ViewFinderActive())
{
return;
}
iObserver.DrawBorders(iBestViewfinderSize);
TInt err;
iObserver.SetBitmap(NULL); // Reset captured image
TRAP(err, iCamera->StartViewFinderBitmapsL(iBestViewfinderSize));
iObserver.ErrorNotifyL(_L("View Finder start err: "), err, EFalse);
if (!iCamera->ViewFinderActive())
{
iObserver.ErrorNotifyL(_L("View Finder not active: "), KErrNotReady, ETrue);
}
}
/*!
@function IsReady
@discussion Check if the camera is ready to take a picture.
@result ETrue if the camera is ready, otherwise EFalse
*/
TBool CCameraManager::IsReady() const
{
return iCameraState != EUninitalised &&
iCameraState != EReservingCamera &&
iCameraState != EStartingCamera &&
iCameraState != EError;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -