I need to extract some random images from gallery without the picker option. Please be clear that I don't want to open picker for this, just extract images if user tapped the button.
You could use dart:io to get the files in a directory and display one of the image files in the directory.
import 'dart:io' as io;
var files = io.Directory("/storage/emulated/0/DCIM/Camera").listSync();
final _random = new math.Random();
var idx= _random.nextInt(files.length) ;
var file = files[idx];
//TODO display the file.
Related
Right now I'm able to chose an image as XFile using MultiImage picker as follows:
final List<XFile>? images = await _picker.pickMultiImage(maxWidth: _maxWidth, maxHeight:_maxHeight);
which then gives me a list of images which I am able to send into my upload service for uploading to my server
for (var image in images)
{
await myUploadService(image); //image is an XFile
}
Now what I need to do is convert the images, no matter what format they are in, into a JPG image before the upload
So I have tried to convert the images, using the image Flutter package (https://github.com/brendan-duncan/image) as follows:
import 'package:image/image.dart' as ImageConvert;
for (var image in images)
{
//Convert all images to JPG
final newImage = ImageConvert.decodeImage(File(image.path).readAsBytesSync())!;
var newImg = ImageConvert.encodeJpg(newImage);
var newImg2 = Image.memory(Uint8List.fromList(newImg)) as XFile; // <--- the main problem
await myUploadService(newImg2);
}
But this isn't working, mainly at the point where I try to put the image back into an XFile format for upload with my upload service
So the main question is here is how can I convert the newly encoded JPG image back to XFile format?
Open to totally different strategies of dealing with this issue as well
Is it possible to split an audio file into individual samples in Flutter? Does anyone know a suitable way?
Yes, you can use this simple plugin to import, cut and then export new audio files
https://pub.dev/packages/audiocutter
and then u can use this code:
import 'package:audiocutter/audiocutter.dart';
var start = 15.0;
var end = 25.5;
// Set The Path to You Your File To Import
var path = 'path/to/audio/file.mp3';
// Get path to cut file and do whatever you want with it.
var outputFilePath = await AudioCutter.cutAudio(path, start, end);
I am using Hive to store the data locally, but the boxes are created dynamically throughout the apps and don't know how many boxes are there in total.
I want to delete all the boxes, whether open or closed, when the user presses the reset button.
So far, I could delete all open boxes or the particular box but not all.
Is there is a way to do that? Or is there any way to open all the boxes at once?
If you want to close all open boxes
Hive.close();
If you want to delete all currently open boxes from disk
Hive.deleteFromDisk();
I created this extension:
import 'dart:async';
import 'dart:io';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
extension on HiveInterface {
/// Get a name list of existing boxes
FutureOr<List<String>> getNamesOfBoxes() async {
final appDir = await getApplicationDocumentsDirectory();
var files = appDir.listSync();
var _list = <String>[];
files.forEach((file) {
if (file.statSync().type == FileSystemEntityType.file
&& p.extension(file.path).toLowerCase() == '.hive') {
_list.add(p.basenameWithoutExtension(file.path));
}
});
print('Current boxes: $_list');
return _list;
}
/* ---------------------------------------------------------------------------- */
/// Delete existing boxes from disk
void deleteBoxes() async {
final _boxes = await this.getNamesOfBoxes();
if (_boxes.isNotEmpty) _boxes.forEach((name) => this.deleteBoxFromDisk(name));
}
}
As of now, I have to keep track of the boxes by again creating the box to store all the box information, and delete the box by reading the number of boxes stored and then resetting the box info also.
How can I convert a PDF file into an image with Flutter?
I want to print the image to an ESC/POS printer using esc_pos_printer. This package won't accept PDFImage, it needs to be a Flutter Image.
I see plenty of PHP plugins that do this but nothing for Flutter.
edit: There is an answer to another question here which shows some code to decode an image from "pdf64" but I can't figure out exactly what "pdf64" is.
I created a PDF from html using flutter_html_to_pdflike this:
Directory appDocDir = await getApplicationDocumentsDirectory();
var targetPath = appDocDir.path;
var generatedPdfFile = await FlutterHtmlToPdf.convertFromHtmlContent(
htmlContent, targetPath, targetFileName);
generatedPdfFilePath = generatedPdfFile.path;
Now I need to know how to create a Flutter Image from that PDF or the bytecode to send raw to the printer.
You can use https://pub.dev/packages/printing:
await for (var page in Printing.raster(document)) {
final image = page.asImage();
...
}
This plugin can also convert your Html to Pdf with this:
final pdf = await Printing.convertHtml(
format: PdfPageFormat.a4,
html: '<html><body><p>Hello!</p></body></html>',
));
How can i create a simple vala Gtk code of an application to open images of a folder and display one at a time ?
I have to create a Vala application to open a folder of images and display a single image at a time.
I have a Gtk.Stack to show only one Image received for a Gtk.FileChooserDialog, but i can't do the Gtk.Filechooser.Dialog to receive more elements and display them.
Thanks
There are 2 solutions:
You want to select multiple files form the same folder: Then just do chooser.select_multiple = true; and you'll get a SList of file URIs via chooser.get_uris()
You need to just select the folder: Then create your FileChooserDialog with the proper action (Gtk.FileChooserAction.SELECT_FOLDER):
var chooser = new Gtk.FileChooserDialog (
"Pick the folder to load the images", this,
Gtk.FileChooserAction.SELECT_FOLDER,
"_Cancel",
Gtk.ResponseType.CANCEL,
"_Open",
Gtk.ResponseType.ACCEPT);
And when you get the proper Folder:
if (chooser.run () == Gtk.ResponseType.ACCEPT) {
var folder = File.new_from_uri (chooser.get_uri ());
if (folder.query_exists() && folder.query_file_type (0) == FileType.DIRECTORY) {
// It's a directory and exists, so enumerate the children and do you stuff
}
}
chooser.close ();