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

📄 cameramodule.cpp

📁 python s60 1.4.5版本的源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/**
 * ====================================================================
 *  cameramodule.cpp
 *
 *
 *  Python API to camera, slightly modified from "miso" (HIIT), "CameraApp" 
 *  and video example application (available from Forum Nokia). 
 *
 *  Implements currently following Python types and functions:
 *
 *  Camera
 *   string take_photo([mode=value, size=valuer zoom=value, flash=value, 
 *                      exp=value, white=value, position=value])
 *      takes a photo, returns the image AND ownership in CFbsBitmap. 
 *      This operation fails with "SymbianError: KErrInUse" if some other 
 *      application is using the camera.
 *
 *      size
 *        image size (resolution)
 *      mode
 *        number of colors, defaults to EFormatFbsBitmapColor64K 
 *      zoom
 *        digital zoom factor, defaults to 0
 *      flash
 *        defaults to EFlashNone
 *      exp
 *        the exposure adjustment of the device, defaults to EExposureAuto
 *      white
 *        white balance, default EWBAuto
 *      position
 *        camera position, defaults to 0
 *
 *   image_modes()
 *   max_image_size()
 *   max_zoom()
 *   flash_modes()
 *   exp_modes()
 *   white_modes()
 *   cameras_available()
 *   start_finder(callable)
 *   stop_finder()
 *   release()
 *   handle()
 *
 *  Video
 *   start_record()
 *   stop_record()
 *    
 *  TODO 
 * 
 *  o The current implementation of finder/take photo does not take
 *    into consideration that camera might be reserved and powered
 *    already. The code needs refactoring when the Python interface
 *    is changed to utilize "Camera" type.
 *  o Add the support for other video sizes
 *
 * Copyright 2005 Helsinki Institute for Information Technology (HIIT)
 * and the authors.  All rights reserved.
 * Authors: Tero Hasu <tero.hasu@hut.fi>
 *
 * Portions Copyright (c) 2005-2007 Nokia Corporation 
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ====================================================================
 */

#include <e32std.h>

#include "cameramodule.h"

//////////////TYPE METHODS VIDEO CAMERA

/*
 * Deallocate video camera.
 */
extern "C" {
  static void vid_dealloc(VID_object *vido)
  {
    if (vido->video) {
      delete vido->video;
      vido->video = NULL;
    }
    if (vido->callBackSet) {
      Py_XDECREF(vido->myCallBack.iCb);
    }
    PyObject_Del(vido);
  }
}

/*
 * Allocate video camera.
 */
extern "C" PyObject *
new_vid_object(PyObject* /*self*/, PyObject *args)
{
  VID_object *vido = PyObject_New(VID_object, VID_type);
  if (vido == NULL)
    return PyErr_NoMemory();

  TRAPD(error, vido->video = CVideoCamera::NewL());
  if (error != KErrNone){
    PyObject_Del(vido);
    return SPyErr_SetFromSymbianOSErr(error);    
  }
  
  vido->callBackSet = EFalse;
  
  return (PyObject*) vido;
}

/*
 * Start video recording
 */
extern "C" PyObject *
vid_start_record(VID_object* self, PyObject *args)
{
  TInt handle;
  PyObject* filename = NULL;
  PyObject* c;
  TInt error = KErrNone;
  
  if (!PyArg_ParseTuple(args, "iUO", &handle, &filename, &c)) 
    return NULL;

  if ((filename && PyUnicode_GetSize(filename) > KMaxFileName)) {
    PyErr_SetString(PyExc_NameError, "filename too long");
    return NULL;
  }

  TPtrC videofile((TUint16*) PyUnicode_AsUnicode(filename), 
                    PyUnicode_GetSize(filename));

  if (c == Py_None)
    c = NULL;
  else if (!PyCallable_Check(c)) {
    PyErr_SetString(PyExc_TypeError, "callable expected");
    return NULL;
  }

  if (self->callBackSet) {
    Py_XDECREF(self->myCallBack.iCb);
  }

  Py_XINCREF(c);
  
  self->myCallBack.iCb = c;
  self->callBackSet = ETrue;

  self->video->SetCallBack(self->myCallBack);
  
  Py_BEGIN_ALLOW_THREADS
  TRAP(error, self->video->StartRecordL(handle, videofile));
  Py_END_ALLOW_THREADS
  
  if (error != KErrNone){
    return SPyErr_SetFromSymbianOSErr(error);    
  }
    
  Py_INCREF(Py_None);
  return Py_None;  
}

/*
 * Stop video recording
 */
extern "C" PyObject *
vid_stop_record(VID_object* self)
{
  self->video->StopRecord();
  
  Py_INCREF(Py_None);
  return Py_None;  
}

//////////////TYPE METHODS CAMERA

/*
 * Deallocate cam.
 */
extern "C" {
  static void cam_dealloc(CAM_object *camo)
  {
    if (camo->camera) {
      delete camo->camera;
      camo->camera = NULL;
    }
    if (camo->callBackSet) {
      Py_XDECREF(camo->myCallBack.iCb);
    }
    PyObject_Del(camo);
  }
}

/*
 * Allocate cam.
 */
extern "C" PyObject *
new_cam_object(PyObject* /*self*/, PyObject *args)
{
  TInt position;
  if (!PyArg_ParseTuple(args, "i", &position))
    return NULL;
  CAM_object *camo = PyObject_New(CAM_object, CAM_type);
  if (camo == NULL)
    return PyErr_NoMemory();

  TRAPD(error, camo->camera = CMisoPhotoTaker::NewL(position));
  if (error != KErrNone){
    PyObject_Del(camo);
    return SPyErr_SetFromSymbianOSErr(error);    
  }
  
  camo->callBackSet = EFalse;
  camo->cameraUsed = ETrue;
  
  return (PyObject*) camo;
}

/*
 * Take photo.
 */
extern "C" PyObject *
cam_take_photo(CAM_object* self, PyObject *args, PyObject *keywds)
{
  // defaults:
  TInt mode = CCamera::EFormatFbsBitmapColor64K;
  TInt size = 0; 
  TInt zoom = 0;
  TInt flash = CCamera::EFlashNone;
  TInt exp = CCamera::EExposureAuto;
  TInt white = CCamera::EWBAuto;
  TInt position = 0; 
  
  TInt error = KErrNone;  

  PyObject *ret = NULL;
  
  static const char *const kwlist[] = 
    {"mode", "size", "zoom", "flash", "exp", "white", "position", NULL};
 
  if (!PyArg_ParseTupleAndKeywords(args, keywds, "|iiiiiii", (char**)kwlist,
                                   &mode, 
                                   &size,
                                   &zoom,
                                   &flash,
                                   &exp, 
                                   &white,
                                   &position)){ 
    return NULL;
  }

  // TODO was this only for the "position"?
  // TODO work on this when there is a real "Camera" type in wrapper also
  
  // re-initialization of camera if needed
  // this is in-efficient but works
  if(self->cameraUsed) 
  {
   	delete self->camera;
   	self->camera = NULL;
    TRAPD(error, self->camera = CMisoPhotoTaker::NewL(position));
    if (error != KErrNone){
      return SPyErr_SetFromSymbianOSErr(error);    
    }   
  }
  self->cameraUsed = ETrue;

  Py_BEGIN_ALLOW_THREADS
  TRAP(error, self->camera->TakePhotoL(mode, size, zoom, flash, exp, white));
  Py_END_ALLOW_THREADS
  
  if (error != KErrNone)
    return SPyErr_SetFromSymbianOSErr(error);

  if (mode == CCamera::EFormatExif || mode == CCamera::EFormatJpeg) {
    HBufC8* data = NULL;
    data = self->camera->GetJpg();
    
    ret = Py_BuildValue("s#", data->Ptr(), data->Length());
    
    delete data; // the data was copied in above - need to delete
      
  } else {
    CFbsBitmap* bmp = NULL;
    bmp = self->camera->GetBitmap();

    ret = PyCObject_FromVoidPtr(bmp, NULL);
    
    // no deletion here as we pass the ownership
  }
  
  if (!ret)
    // should have set an exception
    return NULL;
  return ret;
}

/*
 * Start view finder.
 */
extern "C" PyObject *

⌨️ 快捷键说明

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