照相机_设置焦距

//可以调焦距


#ifndef __CAMERACONTAINER_H__
#define __CAMERACONTAINER_H__

// INCLUDES
#include <aknview.h>
#include <ECam.h>  // CCamera, MCameraObserver
#include "Camera.hrh"

class CMaxIcon;
class CCameraView;
class CControl;

class CCameraContainer : public CCoeControl, public MCameraObserver
    {
public:
    // Constructors and destructor
    CCameraContainer(CControl * aControl, CCameraView * aView);
    virtual ~CCameraContainer();
    void ConstructL(const TRect& aRect);
    TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType);
private:
    // Functions from base classes
    void SizeChanged();
    TInt CountComponentControls() const;
    CCoeControl* ComponentControl(TInt aIndex) const;
    void Draw(const TRect& aRect) const;

private:

    //控制类
    CControl * iControl;
    CCameraView * iView;

    //------照相机-----
    CCamera* iCamera;
    TEngineState iState;
    TPoint iFinderPosition;
    CFbsBitmap* iSnapBitmap;

    TBool iDrawState; //初始化为ETrue,保证Draw函数中的Clear只执行一次
    CPeriodic* iTimer; //1秒以后把iDrawState设置为EFalse
    //5秒后,把iIsResponse设置为ETrue,就是5秒后设置为可以响应按键事件
    TInt iTimerCount; //定时器调用的次数,当为6的时候,就释放定时器,初始化为0

    TFileName iCurPath; //当前的路径,为空是根目录,保存图片的路径和文件名
    TFileName iUploadPath; //图片的路径,保存图片的路径
    TFileName iFileName; //保存图片文件名,名称格式:
    //如果有经纬度:时间_经度_纬度_偏移量.jpg
    //如果没有经纬度:时间_n_n_n.jpg
    //时间格式:20110307083012

    TRect iRCamera; //照相机窗口的大小
    CMaxIcon * iBmpBack;

    TInt iMaxZoom;
    TInt iCurZoom;
public:
    TBool iIsResponse; //是否响应按键,EFalse不响应,ETrue响应。初始化为EFalse.
    //进入页面后后初始化为EFalse,然后启动照相机5秒后设置为ETrue,当点击过按钮后设置为EFalse
    //当拍过照片后设置为EFalse,就直接跳转到缩略图页面


private:
    //-----------------------------拍照-----------------------------
    virtual void ReserveComplete(TInt aError);

    virtual void PowerOnComplete(TInt aError);

    virtual void ViewFinderFrameReady(CFbsBitmap& aFrame);

    virtual void ImageReady(CFbsBitmap* aBitmap, HBufC8* aData, TInt aError);

    virtual void FrameBufferReady(MFrameBuffer* aFrameBuffer, TInt aError);

    //保存图片
    void SaveJgp(HBufC8* aData);

    //查找出有效路径,并设置文件名
    void FindAvailablePath();

    //-----------------------------定时器-----------------------------
    //用于第一次进入此页面刷新页面,以后就不刷新页面了。
    static TInt Loop(TAny* aPtr);

    //-----------------------------页面跳转-----------------------------
    //当图片保存成功后,就需要跳转到缩略图页面,跳转前需要设置Control中一系列的配置
    void JumpToMinPicView();

    //在拍过照片后,就生成文件名,经度_纬度_时间.jpg
    void GenerationFileName();

public:
    //拍照
    void SnapL();

    //------------------------------------------------------------------
    //初始化图片
    void InitBmp();

    };

#endif      // __CameraCONTAINER_H__
// End of File







#include <BAUTILS.H>
#include <ECam.h>
#include <gulicon.h>
#include <aknutils.h>
#include <avkon.hrh>

#include "datastruct/FilePath.h"
#include "view/CameraContainer.h"
#include "manager/Control.h"

#define MEM_FREE(a) if(a){delete a; a=NULL;};
CCameraContainer::CCameraContainer(CControl * aControl, CCameraView * aView)
    {
    iControl = aControl;
    iView = aView;
#ifndef __WINS__
    iSnapBitmap = NULL;
    iCamera = NULL;
#endif
    iDrawState = ETrue;
    iTimer = NULL;
    iIsResponse = EFalse;
    iTimerCount = 0;
    }

// EPOC default constructor can leave.
void CCameraContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

#ifndef __WINS__
    iState = EEngineNotReady;
    iCamera = CCamera::NewL(*this, 0);
    iCamera->Reserve();
#endif

    iRCamera = aRect;

    //判断定时器是否为空,为空的时候执行
    if (NULL == iTimer)
        {
        //创建定时器
        iTimer = CPeriodic::NewL(CActive::EPriorityStandard);
        iTimer->Start(1000000, 1000000, TCallBack(Loop, this));
        }

    InitBmp();

    SetRect(aRect);
    ActivateL();
    }

// destructor
CCameraContainer::~CCameraContainer()
    {
#ifndef __WINS__
    MEM_FREE(iSnapBitmap);
    MEM_FREE(iCamera);
#endif
    MEM_FREE(iTimer);
    }

void CCameraContainer::SizeChanged()
    {
    }

// ---------------------------------------------------------
// CCameraContainer::CountComponentControls() const
// return the number of controls
// ---------------------------------------------------------

//
TInt CCameraContainer::CountComponentControls() const
    {
    // return number of controls inside this container
    return 0;
    }

// ---------------------------------------------------------
// CCameraContainer::ComponentControl(TInt aIndex) const
// return the pointer to specified control.
// ---------------------------------------------------------

//
CCoeControl* CCameraContainer::ComponentControl(TInt /*aIndex*/) const
    {
    return NULL;
    }

// ---------------------------------------------------------
// CCameraContainer::Draw(const TRect& aRect) const
// handle the message when client region must be redrawn.
// ---------------------------------------------------------

//
void CCameraContainer::Draw(const TRect& aRect) const
    {
    //只刷新一次页面
    if (iDrawState)
        {
        CWindowGc& gc = SystemGc();
        gc.Clear();
        NHelper::DrawMaxIcon(gc, aRect, iBmpBack);
        /*
         gc.SetPenStyle(CGraphicsContext::ENullPen);
         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
         gc.SetBrushColor(KRgbWhite);
         gc.DrawRect(Rect());
         */
        }
    }

// ---------------------------------------------------------
// CAknExGridContainer::OfferKeyEventL(
//           const TKeyEvent& aKeyEvent, TEventCode aType )
// Handles the key events.
// ---------------------------------------------------------

//
TKeyResponse CCameraContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
        TEventCode aType)
    {
    TKeyResponse ret(EKeyWasNotConsumed);
    TInt nZoom = 2;
    //不响应任何操作
    if (!iIsResponse)
        {
        return ret;
        }

    if (EKeyUpArrow == aKeyEvent.iCode)
        {
        iCurZoom += nZoom;
        if (iCurZoom >= iMaxZoom)
            iCurZoom = iMaxZoom;
        iCamera->SetDigitalZoomFactorL(iCurZoom);
        DrawDeferred();
        return ret;
        }
    else if (EKeyDownArrow == aKeyEvent.iCode)
        {
        iCurZoom -= nZoom;
        if (iCurZoom <= 0)
            iCurZoom = 0;
        iCamera->SetDigitalZoomFactorL(iCurZoom);
        DrawDeferred();
        return ret;
        }

#ifndef __WINS__
    //按下中间键
    if (EKeyDevice3 == aKeyEvent.iCode)
        {
        SnapL();
        return ret;
        }
#endif

    return ret;

    }

void CCameraContainer::ReserveComplete(TInt aError)
    {
    iState = ECameraReserved;
    iCamera->PowerOn();
    }

void CCameraContainer::PowerOnComplete(TInt aError)
    {
    CCamera::TFormat format = CCamera::EFormatExif;
    TInt imageSizeIndex = 1;
    TCameraInfo cainfo;
    iCamera->CameraInfo(cainfo);

    iMaxZoom = cainfo.iMaxDigitalZoom;
    iCurZoom = iCamera->DigitalZoomFactor();

    /*
     //--test--
     TBuf<32> bn;
     bn.AppendNum(iMaxZoom);
     bn.Append(_L(","));
     bn.AppendNum(iCurZoom);
     iEikonEnv->InfoWinL(bn, KNullDesC);
     //--test--
     */

    TInt nCount = cainfo.iNumImageSizesSupported;
    for (TInt i = 0; i < nCount; i++)
        {
        TSize size;
        iCamera->EnumerateCaptureSizes(size, i, format);

        /*
         //--test--
         TBuf<200> bSize;
         bSize.Append(_L("width:"));
         bSize.AppendNum(size.iWidth);
         bSize.Append(_L("---height:"));
         bSize.AppendNum(size.iHeight);
         DBGPRINTF(bSize);
         //--test--
         */

        if (1000 <= size.iWidth && 1100 >= size.iWidth)
            {
            imageSizeIndex = i;
            break;
            }

        /*
         if (640 == size.iWidth && 480 == size.iHeight)
         {
         imageSizeIndex = i;
         break;
         }
         */
        }

    iCamera->PrepareImageCaptureL(format, imageSizeIndex);

    iState = EEngineIdle;

    //TSize finderSize = Size();
    TSize finderSize = iRCamera.Size();
    TBuf<128> bNum;
    bNum.AppendNum(finderSize.iWidth);
    bNum.Append(_L("--"));
    bNum.AppendNum(finderSize.iHeight);
    //DBGPRINTF(bNum);
    TRAPD(ignored, iCamera->StartViewFinderBitmapsL(finderSize));

    TRect rect = Rect();
    //iFinderPosition = rect.iTl;
    iFinderPosition = rect.iTl;
    iFinderPosition.iX += (rect.Size().iWidth - finderSize.iWidth) / 2;
    iFinderPosition.iY += (rect.Size().iHeight - finderSize.iHeight) / 2;

    }

void CCameraContainer::ViewFinderFrameReady(CFbsBitmap& aFrame)
    {
    User::ResetInactivityTime();

    CWindowGc &gc = SystemGc();

    gc.Activate(Window());

    gc.BitBlt(iFinderPosition, &aFrame);

    gc.Deactivate();
    }

void CCameraContainer::ImageReady(CFbsBitmap* aBitmap, HBufC8* aData,
        TInt aError)
    {
    iState = EEngineIdle;
    if (aError == KErrNone)
        {
        if (aData)
            {
            //保存文件
            SaveJgp(aData);

            //保存成功后,需要跳转到缩略图页面
            JumpToMinPicView();
            }
        }
    }

void CCameraContainer::FrameBufferReady(MFrameBuffer* aFrameBuffer, TInt aError)
    {
    // TODO: Error handling

    // Capturing video is not demonstrated in this snippet
    }

void CCameraContainer::SnapL()
    {
    //当点击过拍照后,就跳转到缩略图页面
    iIsResponse = EFalse;

    // Snapping a picture is only possible if the engine is in idle state
    if (iState == EEngineIdle)
        {
        iState = ESnappingPicture;
        iCamera->CaptureImage();

        // On completion, MCameraObserver::ImageReady() will be called
        }
    else
        {
        User::Leave(KErrNotReady);
        }

    }

void CCameraContainer::SaveJgp(HBufC8* aData)
    {
    //获取有效的路径和文件名
    FindAvailablePath();

    //判断是否有效
    if (iCurPath.Length() == 0)
        return;

    //得到文件会话
    RFs& fs = CEikonEnv::Static()->FsSession();

    //文件指针
    RFile file;

    //打开文件,如文件未找到,则创建文件
    TInt err;
    err = file.Open(fs, iCurPath, EFileWrite);

    //如果文件没有找到,就创建文件
    if (err == KErrNotFound)
        User::LeaveIfError(file.Create(fs, iCurPath, EFileWrite));
    else
        User::LeaveIfError(err);

    //Write和Read函数只能使用8位的数据
    file.Write(*aData);

    //关闭RFile,释放资源
    file.Close();

    }

//查找出有效路径,并设置文件名
void CCameraContainer::FindAvailablePath()
    {
    iCurPath.Zero();
    iFileName.Zero();

    TFileName name1;
    TFileName name2;
    TFileName name3;

    //设置路径
#ifdef __WINS__
    iCurPath.Append(KWINSCamera);
#else
    //E://Images//,
    name2.Append(KPhoneCamera2);
    User::LeaveIfError(CompleteWithAppPath(name2));
    //C://Images//camera97//,
    name3.Append(KPhoneCamera3);
    User::LeaveIfError(CompleteWithAppPath(name3));
    //E://Images//camera97//,
    name1.Append(KPhoneCamera);
    User::LeaveIfError(CompleteWithAppPath(name1));
#endif

    TBool value =
            BaflUtils::FolderExists(CCoeEnv::Static()->FsSession(), name2);
    //如果E盘存在
    if (value)
        {
        iCurPath.Append(name1);
        }
    //E盘不存在
    else
        {
        iCurPath.Append(name3);
        }

    //判断文件是否存在,如果不存在,就创建
    value = BaflUtils::FolderExists(CCoeEnv::Static()->FsSession(), iCurPath);
    if (!value)
        {
        TInt err = CCoeEnv::Static()->FsSession().MkDirAll(iCurPath);
        if ((KErrNone != err) && (KErrAlreadyExists != err))
            {
            return;
            }
        }

    //保存上传的路径
    iUploadPath.Zero();
    iUploadPath.Append(iCurPath);

    //获取文件名
    GenerationFileName();

    //获取路径和文件名
    iCurPath.Append(iFileName);
    }

TInt CCameraContainer::Loop(TAny* aPtr)
    {
    //转换指针
    CCameraContainer* pThis = (CCameraContainer*) aPtr;
    pThis->iTimerCount++;
    if (1 == pThis->iTimerCount)
        {
        pThis->iDrawState = EFalse;
        pThis->DrawDeferred();
        }
    //如果大于2秒就设置可以响应菜单并销毁定时器
    else if (2 < pThis->iTimerCount)
        {
        MEM_FREE(pThis->iTimer);
        pThis->iTimer = NULL;
        pThis->iIsResponse = ETrue; //可以响应按键
        }

    }

//-----------------------------页面跳转-----------------------------
//当图片保存成功后,就需要跳转到缩略图页面,跳转前需要设置Control中一系列的配置
void CCameraContainer::JumpToMinPicView()
    {
    //
    //清空图片参数
    iControl->SetEmptyPic();
    //设置到控制类中
    iControl->iEntryMinPicType = 1; //从那个页面进入
    iControl->iUploadPath.Append(iUploadPath);
    iControl->iUploadPicPath.Append(iCurPath); //路径和名称
    iControl->iUpLoadPicName.Append(iFileName); //图片名称
    iControl->GotoOtherView(EMinPicView);

    }

void CCameraContainer::InitBmp()
    {
    if (iControl)
        {
        CMaxIconManager* pIconMgr = iControl->iIconManager;
        iBmpBack = pIconMgr->GetMaxIcon(CMaxIconManager::EBackBmp);
        }
    }

//在拍过照片后,就生成文件名,经度_纬度_时间.jpg
void CCameraContainer::GenerationFileName()
    {
    TBuf<3> bSeparator;
    bSeparator.Append(_L("_"));

    TBuf<3> bN;
    bN.Append(_L("n"));

    //取一个文件名,取年月日时分秒.jgp
    TTime tCur;
    tCur.HomeTime();

    TBuf<30> timeAsText;
    _LIT ( KTimeFormat, "%Y%M%D%1%2%3%H%T%S");
    tCur.FormatL(timeAsText, KTimeFormat);
    iFileName.Append(timeAsText);
    iFileName.Append(bSeparator);

    //如果经纬度不为空的话,就是
    if (iControl->iLon && iControl->iLat)
        {
        //有经纬度信息
        if (iControl->iLon->Length() > 0 && iControl->iLat->Length() > 0)
            {
            iFileName.Append(iControl->iLon->Des());
            iFileName.Append(bSeparator);
            iFileName.Append(iControl->iLat->Des());
            iFileName.Append(bSeparator);
            //添加偏移量
            if (iControl->iOffset)
                {
                if (iControl->iOffset->Length() > 0)
                    {
                    iFileName.Append(iControl->iOffset->Des());
                    }
                //没有偏移量
                else
                    {
                    iFileName.Append(bN);
                    }
                }
            //没有偏移量
            else
                {
                iFileName.Append(bN);
                }
            }
        //没有经纬度信息
        else
            {
            iFileName.Append(bN);
            iFileName.Append(bSeparator);
            iFileName.Append(bN);
            iFileName.Append(bSeparator);
            iFileName.Append(bN);
            }
        }
    //没有经纬度信息
    else
        {
        iFileName.Append(bN);
        iFileName.Append(bSeparator);
        iFileName.Append(bN);
        iFileName.Append(bSeparator);
        iFileName.Append(bN);
        }

    iFileName.Append(_L(".jpg"));

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值