Skip to content

Instantly share code, notes, and snippets.

@omarhuss
Created September 15, 2023 11:28
Show Gist options
  • Save omarhuss/37b1e08ac45fff95e8b376209c20118c to your computer and use it in GitHub Desktop.
Save omarhuss/37b1e08ac45fff95e8b376209c20118c to your computer and use it in GitHub Desktop.
A simple Flutter app that applies image filters before exporting it in PDF.
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:image/image.dart' as img;
import 'package:open_file/open_file.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:path_provider/path_provider.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Image to PDF App',
theme: ThemeData(primarySwatch: Colors.blue),
home: const ImageToPdfPage(),
);
}
}
class ImageToPdfPage extends StatefulWidget {
const ImageToPdfPage({super.key});
@override
State<ImageToPdfPage> createState() => _ImageToPdfPageState();
}
class _ImageToPdfPageState extends State<ImageToPdfPage> {
File? _image;
img.Image? _filteredImage;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Image to PDF App')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_filteredImage != null
? Image.memory(
Uint8List.fromList(
img.encodeJpg(_filteredImage!),
),
)
: const Text('No image selected.'),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('Select Image'),
onPressed: () async {
final pickedFile = await ImagePicker().pickImage(
source: ImageSource.camera,
);
if (pickedFile != null) {
setState(() {
_image = File(pickedFile.path);
_filteredImage = img.decodeImage(
_image!.readAsBytesSync(),
);
});
}
},
),
const SizedBox(height: 20),
DropdownButton<String>(
items: [
'Original',
'Enhanced',
'Grayscale',
'Sepia',
].map((String filter) {
return DropdownMenuItem<String>(
value: filter,
child: Text(filter),
);
}).toList(),
onChanged: (selectedFilter) {
setState(() {
if (_image != null && selectedFilter != null) {
_filteredImage = applyFilter(_image!, selectedFilter);
}
});
},
hint: const Text('Choose a filter'),
),
const SizedBox(height: 20),
ElevatedButton(
child: const Text('Convert to PDF'),
onPressed: () async {
if (_filteredImage != null) {
await convertToPdf(_filteredImage!).then(
(pdfFile) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('PDF saved at: ${pdfFile.path}'),
action: SnackBarAction(
label: 'Open',
onPressed: () {
OpenFile.open(pdfFile.path);
},
),
),
);
},
);
}
},
),
],
),
),
);
}
img.Image applyFilter(File imageFile, String filter) {
final image = img.decodeImage(imageFile.readAsBytesSync())!;
switch (filter) {
case 'Enhanced':
return enhanceImage(image);
case 'Grayscale':
return img.grayscale(image);
case 'Sepia':
return img.sepia(image);
default:
return image;
}
}
img.Image enhanceImage(img.Image image) {
img.adjustColor(image, contrast: 1.5, brightness: 10);
return image;
}
Future<File> convertToPdf(img.Image image) async {
final pdf = pw.Document();
final pdfImage = pw.MemoryImage(img.encodeJpg(image));
pdf.addPage(
pw.Page(
build: (pw.Context context) => pw.Center(
child: pw.Image(pdfImage),
),
),
);
final pdfBytes = pdf.save();
final Directory documentDirectory =
await getApplicationDocumentsDirectory();
final String documentPath = documentDirectory.path;
final File receiptFile = File('$documentPath/example.pdf');
receiptFile.writeAsBytesSync(List.from(await pdfBytes));
return receiptFile;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment