Android之Camera拍照

原创 2013年12月05日 18:59:12

一、看看调用时序图

1.拍照命令时序图


2.拍照数据回调时序图


二、看看源码分析

hardware/amlogic/camera/CameraHal.cpp

status_t CameraHal::takePicture( ){
  ret = mCameraAdapter->sendCommand(CameraAdapter::CAMERA_START_IMAGE_CAPTURE,  (int) &mStartCapture);
}

调用父类方法:

hardware/amlogic/camera/BaseCameraAdapter.cpp

status_t BaseCameraAdapter::sendCommand(CameraCommands operation, int value1, int value2, int value3){
  switch ( operation ) {
    case CameraAdapter::CAMERA_START_IMAGE_CAPTURE:
      ret = takePicture();
  }
}

调用子类方法:

hardware/amlogic/camera/V4LCameraAdapter/V4LCameraAdapter.cpp

status_t V4LCameraAdapter::takePicture(){
  if (createThread(beginPictureThread, this) == false)
    return -1;
}
/*static*/ int V4LCameraAdapter::beginPictureThread(void *cookie){
  V4LCameraAdapter *c = (V4LCameraAdapter *)cookie;
  return c->pictureThread();
}
int V4LCameraAdapter::pictureThread(){
  ret = sendFrameToSubscribers(&frame);
}

调用父类方法:

hardware/amlogic/camera/BaseCameraAdapter.cpp

status_t BaseCameraAdapter::sendFrameToSubscribers(CameraFrame *frame){
  ret = __sendFrameToSubscribers(frame, &mImageSubscribers, CameraFrame::IMAGE_FRAME);
/*
//如上callback的设置
hardware/amlogic/camera/CameraHal.cpp
status_t CameraHal::initialize(CameraProperties::Properties* properties){
  mAppCallbackNotifier->setEventProvider(eventMask, mCameraAdapter);
  mAppCallbackNotifier->setFrameProvider(mCameraAdapter);
}

hardware/amlogic/camera/AppCallbackNotifier.cpp 
void AppCallbackNotifier::setFrameProvider(FrameNotifier *frameNotifier){
  mFrameProvider = new FrameProvider(frameNotifier, this, frameCallbackRelay);
  mFrameProvider->enableFrameNotification(CameraFrame::IMAGE_FRAME);
  mFrameProvider->enableFrameNotification(CameraFrame::RAW_FRAME);
}

hardware/amlogic/camera/inc/CamerHal.h 
class FrameProvider{
  public:
    FrameProvider(FrameNotifier *fn, void* cookie, frame_callback frameCallback)
                  :mFrameNotifier(fn), mCookie(cookie),mFrameCallback(frameCallback) { }
}
hardware/amlogic/camera/CameraHalUtilClasses.cpp
int FrameProvider::enableFrameNotification(int32_t frameTypes){
  mFrameNotifier->enableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION
                                , mFrameCallback
                                , NULL
                                , mCookie);
}
hardware/amlogic/camera/BaseCameraAdapter.cpp
void BaseCameraAdapter::enableMsgType(int32_t msgs, frame_callback callback, event_callback eventCb, void* cookie){
  if ( CameraFrame::PREVIEW_FRAME_SYNC == msgs ){
    mFrameSubscribers.add((int) cookie, callback);
  }
  else if ( CameraFrame::IMAGE_FRAME == msgs){
    mImageSubscribers.add((int) cookie, callback);
  }
  else if ( CameraFrame::RAW_FRAME == msgs){
    mRawSubscribers.add((int) cookie, callback);
  }
}
//以下callback就是frameCallbackRelay
//callbak-》subscribers-》mImageSubscribers-》mImageSubscribers.add((int) cookie, callback)-》mFrameCallback-》frameCallbackRelay
*/
}
status_t BaseCameraAdapter::__sendFrameToSubscribers(CameraFrame* frame,
     KeyedVector<int, frame_callback> *subscribers,
     CameraFrame::FrameType frameType){
  callback = (frame_callback) subscribers->valueAt(k);
  callback(frame);
}

该回调函数即是:

hardware/amlogic/camera/AppCallbackNotifier.cpp

void AppCallbackNotifier::frameCallbackRelay(CameraFrame* caFrame){
  appcbn->frameCallback(caFrame);
}
void AppCallbackNotifier::frameCallback(CameraFrame* caFrame){
  msg.command = AppCallbackNotifier::NOTIFIER_CMD_PROCESS_FRAME;
  msg.arg1 = frame;
  mFrameQ.put(&msg);
}
void AppCallbackNotifier::notifyFrame(){
  mFrameQ.get(&msg);
  sp<Encoder_libjpeg> encoder = new Encoder_libjpeg(main_jpeg, tn_jpeg,
    AppCallbackNotifierEncoderCallback,
    (CameraFrame::FrameType)frame->mFrameType,
    this,raw_picture,exif_data);
}

补充——拍照是声音的回调如下:

frameworks/av/services/camera/libcameraservice/CameraClient.cpp

void CameraClient::handleShutter(void) {
  mCameraService->playSound(CameraService::SOUND_SHUTTER);
}
frameworks/av/services/camera/libcameraservice/CameraService.cpp
//mSoundPlayer[SOUND_SHUTTER] = newMediaPlayer("/system/media/audio/ui/camera_click.ogg");
void CameraService::playSound(sound_kind kind) {


}
源码中位置:

frameworks/base/data/sounds/OriginalAudio.mk

$(LOCAL_PATH)/effects/camera_click.ogg:system/media/audio/ui/camera_click.ogg

frameworks/base/data/sounds/effects/camera_click.ogg

三、项目问题

拍照后,应用程序不调用startPreview导致预览界面不再刷新:

1.看看预览部分CameraHAL的处理

hardware/amlogic/camera/V4LCameraAdapter/V4LCameraAdapter.cpp

int V4LCameraAdapter::previewThread(){
  frame.mFrameMask |= CameraFrame::PREVIEW_FRAME_SYNC;
  ret = sendFrameToSubscribers(&frame);
}
hardware/amlogic/camera/BaseCameraAdapter.cpp
status_t BaseCameraAdapter::sendFrameToSubscribers(CameraFrame *frame){
  ret = __sendFrameToSubscribers(frame, &mFrameSubscribers, CameraFrame::PREVIEW_FRAME_SYNC);
}
status_t BaseCameraAdapter::__sendFrameToSubscribers(CameraFrame* frame,
                                                      KeyedVector<int, frame_callback> *subscribers,
                                                      CameraFrame::FrameType frameType){
  while(k<subscribers->size()){
    callback = (frame_callback) subscribers->valueAt(k);
    for(uint32_t i = 0; i<subscribers_ref.size();i++){
      if((frame->mCookie == ( void * ) subscribers_ref.keyAt(i))&&(subscribers_ref.valueAt(i) == 0)){
      subscribers_ref.replaceValueFor((uint32_t)frame->mCookie,1);
      //CAMHAL_LOGDB("Frame callbback is available, cookie:0x%x, callback:0x%x",(uint32_t)frame->mCookie,(uint32_t)callback);
      callback(frame);
      k = 0;
      is_find = true;
      break;
    }
  }
}
/*
//如上callback的设置
hardware/amlogic/camera/CameraHal.cpp
//设置APP回调函数
status_t CameraHal::initialize(CameraProperties::Properties* properties){
  mAppCallbackNotifier->setEventProvider(eventMask, mCameraAdapter);
  mAppCallbackNotifier->setFrameProvider(mCameraAdapter);
}
//设置刷屏回调函数
status_t CameraHal::setPreviewWindow(struct preview_stream_ops *window){
  mDisplayAdapter->setFrameProvider(mCameraAdapter);
  mDisplayAdapter->setErrorHandler(mAppCallbackNotifier.get());
  ret  = mDisplayAdapter->setPreviewWindow(window);
}
status_t CameraHal::startPreview(){
  ret = mDisplayAdapter->enableDisplay(width, height, NULL, isS3d ? &s3dParams : NULL);
}

//设置APP回调函数
hardware/amlogic/camera/AppCallbackNotifier.cpp 
void AppCallbackNotifier::setFrameProvider(FrameNotifier *frameNotifier){
  mFrameProvider = new FrameProvider(frameNotifier, this, frameCallbackRelay);
  mFrameProvider->enableFrameNotification(CameraFrame::IMAGE_FRAME);
  mFrameProvider->enableFrameNotification(CameraFrame::RAW_FRAME);
}

//设置刷屏回调函数
hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp
int ANativeWindowDisplayAdapter::setFrameProvider(FrameNotifier *frameProvider){
  mFrameProvider = new FrameProvider(frameProvider, this, frameCallbackRelay);
}
int ANativeWindowDisplayAdapter::enableDisplay(int width, int height, struct timeval *refTime, S3DParameters *s3dParams){
  mFrameProvider->enableFrameNotification(CameraFrame::PREVIEW_FRAME_SYNC);
}

//最终设置地方
hardware/amlogic/camera/inc/CamerHal.h 
class FrameProvider{
  public:
    FrameProvider(FrameNotifier *fn, void* cookie, frame_callback frameCallback)
                  :mFrameNotifier(fn), mCookie(cookie),mFrameCallback(frameCallback) { }
}
hardware/amlogic/camera/CameraHalUtilClasses.cpp
int FrameProvider::enableFrameNotification(int32_t frameTypes){
  mFrameNotifier->enableMsgType(frameTypes<<MessageNotifier::FRAME_BIT_FIELD_POSITION
                                , mFrameCallback
                                , NULL
                                , mCookie);
}
hardware/amlogic/camera/BaseCameraAdapter.cpp
void BaseCameraAdapter::enableMsgType(int32_t msgs, frame_callback callback, event_callback eventCb, void* cookie){
  if ( CameraFrame::PREVIEW_FRAME_SYNC == msgs ){
    mFrameSubscribers.add((int) cookie, callback);
  }
  else if ( CameraFrame::IMAGE_FRAME == msgs){
    mImageSubscribers.add((int) cookie, callback);
  }
  else if ( CameraFrame::RAW_FRAME == msgs){
    mRawSubscribers.add((int) cookie, callback);
}
}
*/

如上callback即是ANativeWindowDisplayAdapter.cpp/AppCallbackNotifier.cpp中的frameCallbackRelay,我们分析预览画面只需要关心ANativeWindowDisplayAdapter.cpp(AppCallbackNotifier.cpp是例如拍照和录像,需要将数据送给APP):

hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp
void ANativeWindowDisplayAdapter::frameCallbackRelay(CameraFrame* caFrame){
  da->frameCallback(caFrame);
}
void ANativeWindowDisplayAdapter::frameCallback(CameraFrame* caFrame){
  PostFrame(df);
}
status_t ANativeWindowDisplayAdapter::PostFrame(ANativeWindowDisplayAdapter::DisplayFrame &dispFrame){
  LOGD("TK----->>>>mPaused is %d\n",mPaused);//add by tankai
  if ( mDisplayState == ANativeWindowDisplayAdapter::DISPLAY_STARTED &&
                (!mPaused ||  CameraFrame::CameraFrame::SNAPSHOT_FRAME == dispFrame.mType) &&
                !mSuspend){
    ret = mANativeWindow->enqueue_buffer(mANativeWindow, mBufferHandleMap[i]);
    mFramesWithCameraAdapterMap.removeItem((int) dispFrame.mBuffer);
  }
  else{
    ret = mANativeWindow->cancel_buffer(mANativeWindow, mBufferHandleMap[i]);
    mFramesWithCameraAdapterMap.removeItem((int) dispFrame.mBuffer);
  }
}
2.问题分析

从如上可以看出:mPaused为true表示暂停刷屏,mPaused为false表示开始刷屏;我们的问题就出在这里。

hardware/camera/CameraHal.cpp
status_t CameraHal::startPreview(){
  if( (mDisplayAdapter.get() != NULL) && ( !mPreviewEnabled ) && ( mDisplayPaused ) ){
    CAMHAL_LOGDA("Preview is in paused state");
    mDisplayPaused = false;
    mPreviewEnabled = true;
    if ( NO_ERROR == ret ){
      ret = mDisplayAdapter->pauseDisplay(mDisplayPaused); //重新开始预览
/*
hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp
status_t ANativeWindowDisplayAdapter::pauseDisplay(bool pause){
  mPaused = pause;
}
*/
      if ( NO_ERROR != ret ){
        CAMHAL_LOGEB("Display adapter resume failed %x", ret);
      }
    }
    //restart preview callbacks
    if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME){
      mAppCallbackNotifier->enableMsgType (CAMERA_MSG_PREVIEW_FRAME);
    }
    return ret;
  }
}
//拍照时将mPaused置为false,暂停刷屏:
status_t CameraHal::takePicture( ){
  if (NO_ERROR == ret &&
    NULL != mDisplayAdapter.get() && burst < 1) {
    if (mCameraAdapter->getState() != CameraAdapter::VIDEO_STATE) {
      mDisplayPaused = true;
      mPreviewEnabled = false;
      ret = mDisplayAdapter->pauseDisplay(mDisplayPaused); //暂停预览
/*
hardware/amlogic/camera/ANativeWindowDisplayAdapter.cpp
status_t ANativeWindowDisplayAdapter::pauseDisplay(bool pause){
  mPaused = pause;
}
*/
      // since preview is paused we should stop sending preview frames too
      if(mMsgEnabled & CAMERA_MSG_PREVIEW_FRAME) {
        mAppCallbackNotifier->disableMsgType (mMsgEnabled & CAMERA_MSG_POSTVIEW_FRAME);
        CAMHAL_LOGDA("disable MSG_PREVIEW_FRAME");
      }
    }

#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
     mDisplayAdapter->setSnapshotTimeRef(&mStartCapture);
#endif
  }

}
3.问题解决

应用层在take_picture之后主动调用startpreview将mPaused改为false;

如果不需要应用主动调用,则需要在HAL将startpreview设为false。

4.如何在应用层取消掉拍照的回调

camera.takePicture(shutterCallback, rawCallback, jpegCallback);
改为
camera.takePicture(null, null, jpegCallback);

原理如下:

frameworks/base/core/java/android/hardware/Camera.java

public final void takePicture(ShutterCallback shutter, PictureCallback raw,
            PictureCallback postview, PictureCallback jpeg) {
        mShutterCallback = shutter;
        mRawImageCallback = raw;
        mPostviewCallback = postview;
        mJpegCallback = jpeg;

        // If callback is not set, do not send me callbacks.
        int msgType = 0;
        if (mShutterCallback != null) {
            msgType |= CAMERA_MSG_SHUTTER;
        }
        if (mRawImageCallback != null) {
            msgType |= CAMERA_MSG_RAW_IMAGE;
        }
        if (mPostviewCallback != null) {
            msgType |= CAMERA_MSG_POSTVIEW_FRAME;
        }
        if (mJpegCallback != null) {
            msgType |= CAMERA_MSG_COMPRESSED_IMAGE;
        }

        native_takePicture(msgType);
        mFaceDetectionRunning = false;
    }


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Android 学习之Camera拍照流程

最近在看Android 4.2.2上的Camera的源码,仔细看了下拍照的流程,从点击拍照按钮到成像保存为图片,整个流程分析如下: 1、  点击拍照按钮图标会执行ShutterButton类中的d...

初识Camera,调用系统拍照录像程序

Camera 官方指南,英语不太好,翻译有可能不准确,见谅 o(^▽^)o 关于拍照录像,有两种处理方式,一种是构建Intent调用系统或其他的APP,另一种是自定义自己的拍照或录像界面,下面就讲解一...
  • krubo1
  • krubo1
  • 2016年01月07日 16:07
  • 1070

android 实现拍照的2种方法

android系统的照相功能,已实现2种方法,可供大家参考:1.调用系统摄像头来拍照首先,找到AndroidManifest.xml文件里加入用户权限   ...

Android开发技巧——Camera拍照功能

本篇是我对开发项目的拍照功能过程中,对Camera拍照使用的总结。由于camera2是在api level 21(5.0.1)才引入的,而Camera到6.0仍可使用,所以暂未考虑camera2。文档...

android调用系统相机拍照

YougaKing 发表于 3周前 阅读 211 收藏 1 点赞 0 评论 0 摘要: 超级简单的利用现有的摄像头获取拍摄的照片 获取缩略图 直接调取相机拍照,无需任何权限,但是只能获取...

Android拍照或从图库选择图片并裁剪

一、 拍照选择图片 1、使用隐式Intent启动相机 //构建隐式Intent Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTU...

Android开发之调用摄像头拍照

现在很多应用中都会要求用户上传一张图片来作为头像,首先我在这接收使用相机拍照和在相册中选择图片。接下来先上效果图: 接下来看代码: 1,布局文件: ...

android调用照相机拍照获取照片并…

1.调用系统的照相机程序 Intent intent = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);  startActivityForResult(i...

安卓利用SurfaceView实现拍照功能

安卓开发中,除了使用自带的相机外,还可以自己实现自定义相机,通常是使用SurfaceView来实现预览拍照功能。 直接贴代码: 主界面Activity: public class MainActi...
  • true100
  • true100
  • 2016年12月07日 11:42
  • 1643

android中 camera 拍照应用详解

在安卓中使用拍照功能有两种方式,一是调用已有的拍照应用;二是使用android的Camera对象直接操作相机,自己写代码来实现拍照功能。 如果是采用Camera的方式,相当于自己写了个拍照程序。...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Android之Camera拍照
举报原因:
原因补充:

(最多只允许输入30个字)