Camera框架
Camera拍照流程
拍照采用ImageReader來作為中介保存圖片例子參考android-Camera2Basic
首先是創(chuàng)建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<JNIImageReaderContext> ctx(new JNIImageReaderContext(env, weakThiz, clazz, maxImages));
sp<IGraphicBufferProducer> gbProducer;
sp<IGraphicBufferConsumer> gbConsumer;
BufferQueue::createBufferQueue(&gbProducer, &gbConsumer); // 創(chuàng)建BufferQueue作為相機與 ImageReader的buffer
sp<BufferItemConsumer> 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);
}
- 從上圖可以看出Surface僅僅是個接口殼而咆,多有的操作都是針對BufferQueue的
- BufferQueueCore中有個成員是mSlots,這個就是用來存放garlloc分配的buffer的handle擂红,類型為