【发布时间】:2019-12-25 12:47:48
【问题描述】:
我正在尝试使用 Flutter 中的相机使用手电筒闪光灯拍照。拍照时如何添加使用手电筒闪光灯的选项?
我使用以下相机包:https://pub.dev/packages/camera#-installing-tab- 用于相机。我已经尝试了 https://pub.dev/packages/lamp 和 https://pub.dev/packages/torch 包来打开手电筒并使其闪烁。在我看来,相机包中没有内置的手电筒功能,所以我尝试使用单独的手电筒包。
TakePictureScreen 小部件:
class TakePictureScreenState extends State<TakePictureScreen> {
CameraController _controller;
Future<void> _initializeControllerFuture;
double displayWidth = 1;
double displayHeight = 1;
bool showFlashButton;
bool isFlashActivated;
@override
void initState() {
super.initState();
showFlashButton = false;
isFlashActivated = false;
switchShowLampButton();
_controller = CameraController(widget.camera, ResolutionPreset.high);
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
displayWidth = MediaQuery.of(context).size.width;
displayHeight = MediaQuery.of(context).size.height;
return WillPopScope(
child: Scaffold(
appBar: AppBar(backgroundColor: Color(0xFF276272)),
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator
return Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Flexible(
child: buildCameraButton(),
),
Flexible(
child: buildFlashButton(),
)
],
),
),
onWillPop: onPop,
);
}
Future<bool> onPop() async {
Navigator.pop(this.context, null);
return false;
}
Future cameraButtonPressed() async {
try {
await _initializeControllerFuture;
// Construct the path where the image should be saved using the path
// package.
final path = join(
// In this example, store the picture in the temp directory. Find
// the temp directory using the `path_provider` plugin.
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
// Attempt to take a picture and log where it's been saved
await _controller.takePicture(path);
// If the picture was taken, display it on a new screen
Future<bool> savePhotoOnPop = Navigator.push(
this.context,
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imagePath: path),
),
);
savePhotoOnPop.then((savePhoto) {
if (savePhoto) {
var file = File(path);
var fileBytesFuture = file.readAsBytes();
fileBytesFuture.then((fileBytes) {
Navigator.pop(this.context, fileBytes);
});
}
});
} catch (e) {
print(e);
}
}
void flashButtonPressed() async {
await Lamp.turnOn(intensity: 1.0);
setState(() {
isFlashActivated = !isFlashActivated;
});
}
Widget buildCameraButton() {
return Container(
margin: EdgeInsets.only(bottom: displayHeight * 0.01),
width: displayWidth * 0.175,
height: displayWidth * 0.175,
child: RawMaterialButton(
shape: CircleBorder(),
fillColor: Color(0xff10846D),
onPressed: cameraButtonPressed,
child: Icon(
Icons.camera_alt,
color: Colors.white,
size: displayWidth * 0.1,
),
),
);
}
Widget buildFlashButton() {
if (showFlashButton)
return Container(
width: displayWidth * 0.175,
height: displayWidth * 0.175,
child: RawMaterialButton(
shape: CircleBorder(),
fillColor: Color(0xff10846D),
onPressed: flashButtonPressed,
child: Icon(
isFlashActivated ? Icons.flash_on : Icons.flash_off,
color: isFlashActivated ? Colors.white : Color(0xFF999999),
size: displayWidth * 0.1,
),
),
);
else
return SizedBox(height: 0);
}
void switchShowLampButton() {
Lamp.hasLamp.then((hasLamp) {
setState(() {
showFlashButton = hasLamp;
});
});
}
}
当flashButtonPressed()被调用时,我希望手机上的手电筒会打开。
根据我使用的是 Lamp- 还是 Torch 包,有两种不同的结果:
灯包:无异常或错误,但手电筒从未打开,也没有任何反应。
Torch 包:我收到以下错误“
Torch for camera "0" is not available due to an existing camera user”。
所以似乎同时使用手电筒和相机之间存在冲突。但是由于我在拍照时没有看到相机包中的任何内置选项使用手电筒,我不知道如何解决这个问题。
谢谢!
【问题讨论】:
-
pub.dev/packages/lamp 试试这个。
-
@AmitPrajapati 我已经尝试过那个包但是没有用。
-
你有没有找到任何解决方案?
-
闪烁开/关不工作并显示错误 - 在调试模式下在 Redmi Note 4 上启动 lib\main.dart... lib\main.dart 注意:D:\john\flutter\.pub -cache\hosted\pub.dartlang.org\lamp-0.0.6\android\src\main\java\plugins\flutter\lamp\lamp\LampPlugin.java 使用或覆盖已弃用的 API。注意:使用 -Xlint:deprecation 重新编译以获取详细信息。 √ 内置 build\app\outputs\flutter-apk\app-debug.apk。在 ws://127.0.0.1:58600/IamL1H5pEQs=/ws I/flutter (27398) 连接到 VM 服务:设备有闪存?真正的 V/BoostFramework(27398): BoostFramework() : mPerf = com.qualcomm.qti.Performance@cc3969
-
lamp 和 torch_compat 两个包都没用,不能在任何安卓设备上运行
标签: flutter