CAMERA学习(一)-------跟踪应用层到HAL

本文详细介绍了Android应用层打开相机的过程,从Camera.open()开始,逐步剖析了从Java层到Native层的相机初始化步骤,涉及Camera.java、CameraBase.cpp、CameraService.cpp等多个关键文件,揭示了如何通过CameraService连接到硬件接口。
摘要由CSDN通过智能技术生成

         publicstatic Camera mCameraDevice;

try {

         Log.d(TAG,"open camera 0");

         //writeFile(APK_FILE_PATH,1);

         Log.d("qyh","opencamera"+System.currentTimeMillis());

         mCameraDevice = Camera.open(0);/*打开camera,获取camera实例给mCameraDevice */

         Log.d("qyh","sendMessage"+System.currentTimeMillis());

         mHandler.sendEmptyMessageDelayed(DISMISS_ALL,500);

         }catch (Exception e) {

         Log.e(TAG,"open camera error");

         //writeFile(APK_FILE_PATH,0);

         e.printStackTrace();

         return;

         }

         try{

         mCameraDevice.setPreviewDisplay(holder);/*这个应该是数据的显示部分,待分析*/

         }catch (Exception e) {

         e.printStackTrace();

         }

 

         Camera.Parameters parameters = mCameraDevice.getParameters();/*获取驱动的相关参数*/

         parameters.setPreviewSize(800, 600);/*重新设置预览的分辨率*/

         mCameraDevice.setParameters(parameters);/*设置相关参数*/

         mCameraDevice.startPreview();/*开始进入预览模式*/

 

以上是应用APP中打开camera的简要步骤,下面我们就对mCameraDevice = Camera.open(0);进行具体的跟进下:

 

STEP 1:

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

   public static Camera open(int cameraId) {

       if (!isPermissionGranted()) {

           return null;

       }

       returnnewCamera(cameraId);

}

 

STEP 2:

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

 

    /** used by Camera#open, Camera#open(int)*/

    Camera(int cameraId) {

        int err = cameraInitNormal(cameraId);

        if (checkInitErrors(err)) {

            switch(err) {

                case EACCESS:

                    throw newRuntimeException("Fail to connect to camera service");

                case ENODEV:

                    throw newRuntimeException("Camera initialization failed");

                default:

                    // Should never hit this.

                    throw newRuntimeException("Unknown camera error");

            }

        }

    }

 

STEP 3:

    private int cameraInitNormal(int cameraId){

      return cameraInitVersion(cameraId,CAMERA_HAL_API_VERSION_NORMAL_CONNECT);

    }

/*

CAMERA_HAL_API_VERSION_NORMAL_CONNECT参数的定义

private static final int CAMERA_HAL_API_VERSION_NORMAL_CONNECT= -2;

*/

STEP 4:

    private int cameraInitVersion(int cameraId,int halVersion) {

        mShutterCallback = null;

        mRawImageCallback = null;

        mJpegCallback = null;

        mPreviewCallback = null;

        mPreviewRawDumpCallback = null;

        mPostviewCallback = null;

        mUsingPreviewAllocation = false;

        mZoomListener = null;

 

        Looper looper;

        if ((looper = Looper.myLooper()) !=null) {

            mEventHandler = new EventHandler(this,looper);

        } else if ((looper =Looper.getMainLooper()) != null) {

            mEventHandler = newEventHandler(this, looper);

        } else {

            mEventHandler = null;

        }

 

        return native_setup(newWeakReference<Camera>(this), cameraId, halVersion,

               ActivityThread.currentOpPackageName());

    }

/*

    private native final int native_setup(Object camera_this, int cameraId,int halVersion,

                                          String packageName);

 

native_setup是一个本地的方法,对应的JNI在下面步骤中。

*/

STEP 5:

\frameworks\base\core\jni\android_hardware_Camera.cpp

 

// connect tocamera service

static jint android_hardware_Camera_native_setup(JNIEnv *env,jobject thiz,

    jobject weak_this, jint cameraId, jinthalVersion, jstring clientPackageName)

{

    // Convert jstring to String16

    const char16_t *rawClientName =reinterpret_cast<const char16_t*>(

       env->GetStringChars(clientPackageName, NULL));

    jsize rawClientNameLen =env->GetStringLength(clientPackageName);

    String16 clientName(rawClientName,rawClientNameLen);

   env->ReleaseStringChars(clientPackageName,

                           reinterpret_cast<const jchar*>(rawClientName));</

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值