'Invoking functions from camera_web
according to the Flutter documentation, it is mentioned that camera_web plugin is similar to the camera plugin, but designed for web application, so I want to remake the code for camera_web where I would be able to access the webcam of my laptop using chrome on mac, but I found the functions of camera_web are not same as that of camera! For example, there's no controller (CameraController)
can anyone help me in reformatting the code for camera_web, I did found I have to invoke CameraPlugin but how I am not sure!
the code with "camera" plugin:
import 'dart:ui';
import 'package:camera/camera.dart';
import 'package:camera_web/camera_web.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
class CameraService {
CameraPlugin? cameraPlugin;
CameraController? _cameraController;
CameraController? get cameraController => this._cameraController;
InputImageRotation? _cameraRotation;
InputImageRotation? get cameraRotation => this._cameraRotation;
String? _imagePath;
String? get imagePath => this._imagePath;
Future<void> initialize() async {
if (_cameraController != null) return;
CameraDescription description = await _getCameraDescription();
await _setupCameraController(description: description);
this._cameraRotation = rotationIntToImageRotation(
description.sensorOrientation,
);
}
Future<CameraDescription> _getCameraDescription() async {
List<CameraDescription> cameras = await availableCameras();
return cameras.firstWhere((CameraDescription camera) =>
camera.lensDirection == CameraLensDirection.front);
}
Future _setupCameraController({
required CameraDescription description,
}) async {
this._cameraController = CameraController(
description,
ResolutionPreset.high,
enableAudio: false,
);
await _cameraController?.initialize();
}
InputImageRotation rotationIntToImageRotation(int rotation) {
switch (rotation) {
case 90:
return InputImageRotation.rotation90deg;
case 180:
return InputImageRotation.rotation180deg;
case 270:
return InputImageRotation.rotation270deg;
default:
return InputImageRotation.rotation0deg;
}
}
Future<XFile?> takePicture() async {
assert(_cameraController != null, 'Camera controller not initialized');
await _cameraController?.stopImageStream();
XFile? file = await _cameraController?.takePicture();
_imagePath = file?.path;
return file;
}
Size getImageSize() {
assert(_cameraController != null, 'Camera controller not initialized');
assert(
_cameraController!.value.previewSize != null, 'Preview size is null');
return Size(
_cameraController!.value.previewSize!.height,
_cameraController!.value.previewSize!.width,
);
}
dispose() async {
await this._cameraController?.dispose();
this._cameraController = null;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
