Camera服务之--App

1.回调接口.

Camera Client端需要提供一些回调方法,给Camera Service用。而Camera Client提供的这些接口,真正的实现是在Camera的应用部分。


在packages/apps/Camera/src/com/android/camera/Camera.java类中,定义了几个回调接口:

private final ShutterCallback mShutterCallback = new ShutterCallback();
    private final PostViewPictureCallback mPostViewPictureCallback =
            new PostViewPictureCallback();
    private final RawPictureCallback mRawPictureCallback =
            new RawPictureCallback();
    private final AutoFocusCallback mAutoFocusCallback =
            new AutoFocusCallback();
    private final ZoomListener mZoomListener = new ZoomListener();
    // Use the ErrorCallback to capture the crash count
    // on the mediaserver
    private final ErrorCallback mErrorCallback = new ErrorCallback();

这些回调方法通过:mCameraDevice.takePicture(mShutterCallback, mRawPictureCallback,
                    mPostViewPictureCallback, new JpegPictureCallback(loc));

将这些回调方法设置到android.hardware.Camera中。


那么这些回调接口在什么时候会被调用呢?我们来看一下android.hardware.Camera的代码(/frameworks/base/core/java/android/hardware/Camera.java)。

在这个android.hardware.Camera中,先看takePicture的实现:

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

可以看到,在这里把app传进来的一些回调接口都保存在了成员变量中,这个类中还有一个EventHandler的类,扩展自Handler,一看类名和继承关系就可以猜到了,这个EventHandler肯定是对一些回调事件的处理,看实现:

private class EventHandler extends Handler
    {
        private Camera mCamera;

        public EventHandler(Camera c, Looper looper) {
            super(looper);
            mCamera = c;
        }

        @Override
        public void handleMessage(Message msg) {
            switch(msg.what) {
            case CAMERA_MSG_SHUTTER:
                if (mShutterCallback != null) {
                    mShutterCallback.onShutter();
                }
                return;

            case CAMERA_MSG_RAW_IMAGE:
                if (mRawImageCallback != null) {
                    mRawImageCallback.onPictureTaken((byte[])msg.obj, mCamera);
                }
                return;

            case CAMERA_MSG_COMPRESSED_IMAGE:
                if (mJpegCallback != null) {
                    mJpegCallback.onPictureTaken((byte[])msg.obj, mCamera);
                }
                return;

            case CAMERA_MSG_PREVIEW_FRAME:
                if (mPreviewCallback != null) {
                    PreviewCallback cb = mPreviewCallback;
                    if (mOneShot) {
                        // Clear the callback variable before the callback
                        // in case the app calls setPreviewCallback from
                        // the callback function
                        mPreviewCallback = null;
                    } else if (!mWithBuffer) {
                        // We're faking the camera preview mode to prevent
                        // the app from being flooded with preview frames.
                        // Set to oneshot mode again.
                        setHasPreviewCallback(true, false);
                    }
                    cb.onPreviewFrame((byte[])msg.obj, mCamera);
                }
                return;

            case CAMERA_MSG_POSTVIEW_FRAME:
                if (mPostviewCallback != null) {
                    mPostviewCallback.onPictureTaken((byte[])msg.obj, mCamera);
                }
                return;

            case CAMERA_MSG_FOCUS:
                if (mAutoFocusCallback != null) {
                    mAutoFocusCallback.onAutoFocus(msg.arg1 == 0 ? false : true, mCamera);
                }
                return;

            case CAMERA_MSG_ZOOM:
                if (mZoomListener != null) {
                    mZoomListener.onZoomChange(msg.arg1, msg.arg2 != 0, mCamera);
                }
                return;

            case CAMERA_MSG_ERROR :
                Log.e(TAG, "Error " + msg.arg1);
                if (mErrorCallback != null) {
                    mErrorCallback.onError(msg.arg1, mCamera);
                }
                return;

            default:
                Log.e(TAG, "Unknown message type " + msg.what);
                return;
            }
        }
    }

果然,就是对之前保存的那些回调接口的成员变量的调用。

还有一个静态方法:

private static void postEventFromNative(Object camera_ref,
                                            int what, int arg1, int arg2, Object obj)
    {
        Camera c = (Camera)((WeakReference)camera_ref).get();
        if (c == null)
            return;

        if (c.mEventHandler != null) {
            Message m = c.mEventHandler.obtainMessage(what, arg1, arg2, obj);
            c.mEventHandler.sendMessage(m);
        }
    }

这个方法中,就是回调event的中转站,将Camera Service传回来的event,转发给handler来处理。

至于这个postEventFromNative是怎么被调用的,请看后续关于Camera JNI部分的文章。


2.关于阅读代码中遇到的一些问题。

(1)应用部分的UI:

如果你已经读过android2.3的camera应用代码,你就会知道,整个应用,机会没有用到android提供的任何widget,除了surfaceview和framelayout,甚至连TextView都没用。所有的UI都是通过OpenGL画上去的。而且它自己实现了一套View的框架,和android framework中关于view的框架是一致的,比如onMeasure和onLayout,比如用到了同View一样的复合设计模式等。如果熟悉android framework和view的绘制过程,对它自己的UI框架应该不难理解。但是具体的实现就需要对OpenGL有了解了,我自己是不懂,所以就没怎么看具体的实现。

(2)camera的应用部分的架构。

老实说,应用部分的结构写的不是很好,代码很乱,虽然有些地方用一些小技巧或者设计模式很巧妙。但是bug实在是很多。如果你需要维护原生的Camera应用,那么你惨了。(我就是……很悲催,anr和fc太平常了)。如果你只是想阅读代码了解一下,那么你知道怎么使用android.hardware.Camera来进行各种操作就可以了。如果想做Camera应用,就别在原生的上面改了,自己重写一个吧。比如MIUI就是个例子,把原生camera就扔了,自己写的。

你可能感兴趣的:(Camera服务之--App)