Skip to content

Commit

Permalink
chore: update example
Browse files Browse the repository at this point in the history
Signed-off-by: Caijinglong <cjl_spy@163.com>
  • Loading branch information
CaiJingLong committed Jan 6, 2025
1 parent 5af0ee5 commit 70b8242
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion packages/flutter_image_compress/example/lib/main/main_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,28 @@ class _MyAppState extends State<MyApp> {
});
}

/// The example for compressing heic format.
///
/// Convert jpeg to heic format, and then convert heic to jpg format.
///
/// Show the file path and size in the console.
void _compressHeicExample() async {
print('start compress');
final logger = TimeLogger();
logger.startRecorder();
final tmpDir = (await getTemporaryDirectory()).path;
// final tmpDir = (await getTemporaryDirectory()).path;
final tmpDir = (await path_provider.getExternalStorageDirectories())
?.first
.absolute
.path;

if (tmpDir == null) {
print('tmpDir is null');
print(
'You need check your permission for the external storage on Android.');
return;
}

final target = '$tmpDir/${DateTime.now().millisecondsSinceEpoch}.heic';
final srcPath = await getExampleFilePath();
final result = await FlutterImageCompress.compressAndGetFile(
Expand All @@ -272,6 +289,30 @@ class _MyAppState extends State<MyApp> {
'Compress heic result path: ${result.path}, '
'size: ${await result.length()}',
);

// Convert heic to jpg
final jpgPath =
'$tmpDir/heic-to-jpg-${DateTime.now().millisecondsSinceEpoch}.jpg';
try {
final jpgResult = await FlutterImageCompress.compressAndGetFile(
result.path,
jpgPath,
format: CompressFormat.jpeg,
quality: 90,
);
if (jpgResult == null) {
print('Convert heic to jpg failed.');
} else {
print(
'Convert heic to jpg success. '
'Jpg path: ${jpgResult.path}, '
'size: ${await jpgResult.length()}',
);
}
} catch (e) {
print('Error: $e');
print('Convert heic to jpg failed.');
}
}

void _compressAndroidWebpExample() async {
Expand Down

0 comments on commit 70b8242

Please sign in to comment.