这是我的 initializecamera 代码:
initializeCamera() async {
try {
final cameras = await availableCameras();
final firstCamera = cameras.first;
ContextManager.camera = firstCamera;
setState(() {
cameraAuthorized = true;
ContextManager.cameraAuthorized = cameraAuthorized;
});
} catch(e) {
ContextManager.camera = null;
setState(() {
cameraAuthorized = false;
ContextManager.cameraAuthorized = cameraAuthorized;
});
print(e);
}
}
的启动代码
@override
void initState() {
super.initState();
initializeCamera().then((value) {
if (ContextManager.camera != null) {
setState(() {
_controller = CameraController(
ContextManager.camera,
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize();
});
}
});
}
这是我 浏览器我会从我的 initizecamera 步骤中获得此弹出式:
当我单击“允许”“允许” the the()中调用的其余代码
尤其是 _controller.Initialize()在设备上初始化我的相机,并且相机开始工作。当我单击“拒绝”我自定义的代码的另一部分时,将运行以在屏幕上显示消息时,换句话说,一切正常。
当我在Android仿真器上运行时,从我的 initizececamera()函数到 _controller.initialize()
“ https://i.sstatic.net/5ax2i.png” rel =“ nofollow noreferrer”> 
无论我选择“使用应用程序时”还是“仅此一次”,相机开始工作,一切都可以。但是,当我单击“拒绝”时,该应用会引发 cameraxception 。
我试图包装这一行(这引起了问题):
_initializeControllerFuture = _controller.initialize()
在try/catch中,还指定了异常的名称,但没有任何效果。
我不到达这里的是,在模拟器中的弹出窗口显示之前,请调用 _controller.initialize()函数,因此在我们可以选择拒绝/允许之前,在设备上初始化了相机它。
在计算机上,您实际上可以选择是否拒绝/允许相机在执行 _controller.initialize()函数之前。
它在模拟器上的工作方式对我来说没有任何意义,如果有人知识渊博的人可以启发我,谢谢。
Here is my initializeCamera code :
initializeCamera() async {
try {
final cameras = await availableCameras();
final firstCamera = cameras.first;
ContextManager.camera = firstCamera;
setState(() {
cameraAuthorized = true;
ContextManager.cameraAuthorized = cameraAuthorized;
});
} catch(e) {
ContextManager.camera = null;
setState(() {
cameraAuthorized = false;
ContextManager.cameraAuthorized = cameraAuthorized;
});
print(e);
}
}
Here is my initState code where I call the initializeCamera function and after that initialize the CameraController :
@override
void initState() {
super.initState();
initializeCamera().then((value) {
if (ContextManager.camera != null) {
setState(() {
_controller = CameraController(
ContextManager.camera,
ResolutionPreset.medium,
);
_initializeControllerFuture = _controller.initialize();
});
}
});
}
When I am running the code with flutter web from my computer browser I get this popup from my initializeCamera step :

When I click "allow" the rest of the code which is called in the then()
and especially _controller.initialize(), which initialize my camera on the device, is executed and the camera starts working. When I click "deny" another part of the code that I customed is run to display a message on the screen, in other words everything works fine.
When I'm running on the Android Emulator everything from my initializeCamera() function to _controller.initialize() is run without interruption until I get this popup :

Whether I choose "while using the app" or "only this time" the camera starts working and everything is ok. But when I click "deny" the app throws a CameraException.
I tried to wrap this line (which is causing the issue) :
_initializeControllerFuture = _controller.initialize()
in try/catch, and also specifying the name of the exception, but nothing worked.
What I don't get here, is that _controller.initialize() function is called before the popup show in the emulator, so the camera is initialized on the device before we can actually choose to deny/allow it.
While on the computer you actually get to choose whether you deny/allow the camera before the _controller.initialize() function is executed, which make perfect sense to me.
The ways it works on the emulator doesn't make any sense for me, if someone more knowledgeable could enlighten me, thanks.
发布评论