【Android Camera】之 Preview
void JNICameraContext::copyAndPost(JNIEnv* env, const sp<IMemory>& dataPtr, int msgType)
{
jbyteArray obj = NULL;
// allocate Java byte array and copy data
if (dataPtr != NULL) {
ssize_t offset;
size_t size;
sp<IMemoryHeap> heap = dataPtr->getMemory(&offset, &size);
LOGV("postData: off=%d, size=%d", offset, size);
uint8_t *heapBase = (uint8_t*)heap->base();
if (heapBase != NULL) {
const jbyte* data = reinterpret_cast<const jbyte*>(heapBase + offset);
if (!mManualBufferMode) {
LOGV("Allocating callback buffer");
obj = env->NewByteArray(size);
} else {
// Vector access should be protected by lock in postData()
if(!mCallbackBuffers.isEmpty()) {
LOGV("Using callback buffer from queue of length %d", mCallbackBuffers.size());
jbyteArray globalBuffer = mCallbackBuffers.itemAt(0);
mCallbackBuffers.removeAt(0);
obj = (jbyteArray)env->NewLocalRef(globalBuffer);
env->DeleteGlobalRef(globalBuffer);
if (obj != NULL) {
jsize bufferLength = env->GetArrayLength(obj);
if ((int)bufferLength < (int)size) {
LOGE("Manually set buffer was too small! Expected %d bytes, but got %d!",
size, bufferLength);
env->DeleteLocalRef(obj);
return;
}
}
}
if(mCallbackBuffers.isEmpty()) {
LOGV("Out of buffers, clearing callback!");
mCamera->setPreviewCallbackFlags(FRAME_CALLBACK_FLAG_NOOP);
mManualCameraCallbackSet = false;
if (obj == NULL) {
return;
}
}
}
if (obj == NULL) {
LOGE("Couldn't allocate byte array for JPEG data");
env->ExceptionClear();
} else {
env->SetByteArrayRegion(obj, 0, size, data);
}
} else {
LOGE("image heap is NULL");
}
}
// post image data to Java
env->CallStaticVoidMethod(mCameraJClass, fields.post_event,
mCameraJObjectWeak, msgType, 0, 0, obj);
if (obj) {
env->DeleteLocalRef(obj);
}
}
内容版权声明:除非注明,否则皆为本站原创文章。