Android Camer Framework

Camera框架

Android Camer Framework_第1张图片
图片.png

Camera拍照流程

拍照采用ImageReader来作为中介保存图片例子参考android-Camera2Basic
首先是创建ImageReader

static void ImageReader_init(JNIEnv* env, jobject thiz, jobject weakThiz,
                             jint width, jint height, jint format, jint maxImages)
{
    status_t res;
    int nativeFormat;
    android_dataspace nativeDataspace;

    sp ctx(new JNIImageReaderContext(env, weakThiz, clazz, maxImages));

    sp gbProducer;
    sp gbConsumer;
    BufferQueue::createBufferQueue(&gbProducer, &gbConsumer);  // 创建BufferQueue作为相机与 ImageReader的buffer
    sp bufferConsumer;
    String8 consumerName = String8::format("ImageReader-%dx%df%xm%d-%d-%d",
            width, height, format, maxImages, getpid(),
            createProcessUniqueId());
    uint32_t consumerUsage = GRALLOC_USAGE_SW_READ_OFTEN;

    bufferConsumer = new BufferItemConsumer(gbConsumer, consumerUsage, maxImages,
            /*controlledByApp*/true);

    ctx->setBufferConsumer(bufferConsumer);
    bufferConsumer->setName(consumerName);

    ctx->setProducer(gbProducer);
    bufferConsumer->setFrameAvailableListener(ctx);   // 消费者是ImageReader,有新图像到来时会通知onFrameAvailable,再--> postEventFromNative --> App注册的Listener
    ImageReader_setNativeContext(env, thiz, ctx);
    ctx->setBufferFormat(nativeFormat);
    ctx->setBufferDataspace(nativeDataspace);
    ctx->setBufferWidth(width);
    ctx->setBufferHeight(height);

    // Set the width/height/format/dataspace to the bufferConsumer.
    res = bufferConsumer->setDefaultBufferSize(width, height);
    res = bufferConsumer->setDefaultBufferFormat(nativeFormat);
    res = bufferConsumer->setDefaultBufferDataSpace(nativeDataspace);
}
Android Camer Framework_第2张图片
图片.png
  • 从上图可以看出Surface仅仅是个接口壳,多有的操作都是针对BufferQueue的
  • BufferQueueCore中有个成员是mSlots,这个就是用来存放garlloc分配的buffer的handle,类型为

参考

  • Android Camera API
  • Developer Android
  • Source Android

你可能感兴趣的:(Android Camer Framework)