How does flutter get the main color captured in the camera? - flutter

I'm building an app that uses a camera to capture pictures, and I need to prevent the lens from being blocked and display all black.
I'm using camera library.
Future<void> initController() async {
try {
List cameras = await availableCameras();
controller = CameraController(cameras.first, ResolutionPreset.low);
await controller!.initialize();
Future.delayed(const Duration(milliseconds: 100)).then((onValue) {
controller!.setFlashMode(FlashMode.torch);
});
controller!.startImageStream((CameraImage img) {
image = img;
});
} on Exception {
print(Exception);
}
}
I hope to use the CameraImage type that comes with the library to obtain the color code in some way in startImageStream to determine whether the user blocks the camera.

Related

ImagePicker return invalid image source

Im using ImagePicker to pick multiple image. On android just work fine. But iOS returning.
PlatformException (PlatformException(invalid_source, Invalid image source.
ImagePicker _picker = ImagePicker();
pickMultiImage() async {
Size size = MediaQuery.of(context).size;
AppLocalizations localization = AppLocalizations.of(context)!;
try {
final List<XFile>? images = await _picker.pickMultiImage();
if (images!.isNotEmpty) {
imageFileList!.addAll(images);
}
setState(() {});
} catch (e) {
showDialogOpenPermission(
context, localization, localization.gallery, size);
}
}
update : Checking on iOS real device just works fine. Long ago, I think just works fine on emulator :(.
** I'm prefer to test my apps by via emulator. So, If anyone here know the solution, please tell me ;)

how to edit the taken picture with flutter

I use Image_picker and provider and some other things to access to camera and gallery and get the photo and save it with Sqlite . there is no problem here but I want to ask is there any solution for editing photo while we take the photo using camera? for example highlight a part of the photo
Future<void> _takePicture() async {
final picker = ImagePicker();
final imageFile =
await picker.pickImage(source: ImageSource.camera, maxHeight: 600);
if (imageFile == null) {
return;
}
setState(() {
_imageFile = File(imageFile.path);
});
this is my method
should I use Image_painter or Image_editor_plus?
you can use Image package in flutter, you can change the brightness using this method from image package
Image image = decodeImage(file.readAsBytesSync());
Image brightenedImage = adjustColor(image, brightness: 0.2);
you can read details of this package here.

how to handle the time lag after capturing the image ImagePicker Flutter

im learning flutter and now tried to capture the photo with ImagePicker package from the following method, but after I successfully capture the photo, there is always 1 sec until app get the data and jump to next page:
Future pickImage(ImageSource source) async {
try {
var image = await ImagePicker().pickImage(source: source);
if (image == null) return;
final imagePermanent = await saveImagePermanently(image.path);
selectedImage = File(imagePermanent.path);
isUploaded.value = !isUploaded.value;
update();
} on PlatformException catch (e) {
print('Failed to pick image: $e');
}
}
Future<File> saveImagePermanently(String imagePath) async {
final directory = await getApplicationDocumentsDirectory();
final name = basename(imagePath);
final image = File('${directory.path}/$name');
return File(imagePath).copy(image.path);
}
now my solution is adding a listener onInit when image is created with GetX:
#override
void onInit() {
super.onInit();
ever(isUploaded, (value) {
Get.to(
() => AddPersonProfileAddDetails(),
);
});
}
So is there a way to detect the status of capturing the image like finished/failed/progressing, thanks for any clue or let me know a better way to jump to next page after capturing the image, thanks a lot!

pass var to another screen in flutter cameras = await availableCameras();

i am implementing camera this lib this library collects camera in the main file like this
List<CameraDescription> cameras = [];
Future<void> main() async {
// Fetch the available cameras before initializing the app.
try {
WidgetsFlutterBinding.ensureInitialized();
cameras = await availableCameras();
} on CameraException catch (e) {
logError(e.code, e.description);
}
runApp(CameraApp());
}
The problem is that i want to use this cameras list in another class which is not connected in main() function i dont know how to use these cameras in 3rd screen.
For eg if i have 4 screen main()=>1st screen=> 2nd screen=> 3rd screen=> 4th screen. Now the cameras are initialising the main but i want them in 4th screen.
use state management for pass data to another screen.
https://flutter.dev/docs/development/data-and-backend/state-mgmt/intro

How can I limit video capturing via ImagePicker to 1 minutes maximum duration in Flutter?

I'm using ImagePicker to upload videos either from gallery or via capturing them from camera.
Problem is that I don't want the video to exceed 1 minute duration, when in gallery picking mode, I check the duration of selected video and show a message if video is longer than 1 minute.
How can I do something like retrica, open camera but with limit on video duration ?
use maxDuration provided by image_picker
final PickedFile videoFile = await picker.getVideo(
source: ImageSource.camera,
maxDuration: const Duration(seconds: 60),
);
I think you cant do this by ImagePicker because of this plugin capture video by phone default camera app and you haven't access to check and manage duration while capturing until the user stops capturing and return to your application
but if you use camera plugin you can do this because of this plugin capture video by your application and you have access to check video duration while user capture video
https://pub.dev/packages/camera
you can't controller it if you want to get this feature use Camera plugin
https://pub.dev/packages/camera
and use timer to stop recording
//Timer
timer = Timer.periodic(Duration(seconds: 60), (Timer t) {
_onStopButtonPressed();
timer.cancel();
});
});
//stop recording when click on the button
void _onStopButtonPressed() {
setState(() {
buttonColor = Colors.white;
});
_stopVideoRecording().then((_) {
if (mounted) setState(() {});
});
timer.cancel(); //when user close it manually
}
// stop funcation
Future<void> _stopVideoRecording() async {
if (!controller.value.isRecordingVideo) {
return null;
}
try {
await controller.stopVideoRecording();
} on CameraException catch (e) {
_showCameraException(e);
return null;
}
}
also you can use video_player plugin to replay the video after recording
https://pub.dev/packages/video_player#-installing-tab-