Compress Video Flutter and Upload to Firebase
Compresses image as native plugin (Obj-C/Kotlin)
This library can works on Android and iOS.
- flutter_image_compress
- Why don't yous use dart to practice information technology
- 1.0.0
- Usage
- About common params
- minWidth and minHeight
- rotate
- autoCorrectionAngle
- quality
- format
- Webp
- HEIF(Heic)
- Heif for iOS
- Heif for Android
- inSampleSize
- keepExif
- Effect
- Virtually
List<int>andUint8List
- Virtually
- Runtime Error
- Android
- iOS
- Troubleshooting or common error
- Compressing returns
aught - Android build mistake
- Compressing returns
- Well-nigh EXIF information
- LICENSE
- PNG/JPEG encoder
- Webp encoder
- HEIF encoder
- About Exif handle code
Q:Sprint already has prototype compression libraries. Why use native?
A:For unknown reasons, image pinch in Dart language is not efficient, even in release version. Using isolate does not solve the trouble.
The one.0.0 is null-safety version.
Please read document for goose egg-safety information in dart or palpitate.
dependencies: flutter_image_compress: ^1.0.0-nullsafety import 'bundle:flutter_image_compress/flutter_image_compress.dart'; Use as:
See total case
There are several means to employ the library api.
// 1. compress file and get Uint8List Futurity<Uint8List> testCompressFile(File file) async { var result = await FlutterImageCompress.compressWithFile( file.absolute.path, minWidth: 2300, minHeight: 1500, quality: 94, rotate: xc, ); print(file.lengthSync()); print(result.length); return result; } // 2. compress file and get file. Futurity<File> testCompressAndGetFile(File file, String targetPath) async { var result = expect FlutterImageCompress.compressAndGetFile( file.absolute.path, targetPath, quality: 88, rotate: 180, ); impress(file.lengthSync()); impress(result.lengthSync()); render consequence; } // iii. compress nugget and get Uint8List. Time to come<Uint8List> testCompressAsset(String assetName) async { var listing = await FlutterImageCompress.compressAssetImage( assetName, minHeight: 1920, minWidth: 1080, quality: 96, rotate: 180, ); return listing; } // 4. compress Uint8List and get another Uint8List. Futurity<Uint8List> testComporessList(Uint8List listing) async { var issue = await FlutterImageCompress.compressWithList( list, minHeight: 1920, minWidth: 1080, quality: 96, rotate: 135, ); print(list.length); impress(issue.length); return result; } minWidth and minHeight #
minWidth and minHeight are constraints on image scaling.
For example, a 4000*2000 image, minWidth ready to 1920, minHeight set to 1080, the calculation is equally follows:
// Using dart as an instance, the actual implementation is Kotlin or OC. import 'dart:math' as math; void chief() { var scale = calcScale( srcWidth: 4000, srcHeight: 2000, minWidth: 1920, minHeight: 1080, ); print("scale = $scale"); // scale = 1.8518518518518519 print("target width = ${4000 / scale}, height = ${2000 / scale}"); // target width = 2160.0, height = 1080.0 } double calcScale({ double srcWidth, double srcHeight, double minWidth, double minHeight, }) { var scaleW = srcWidth / minWidth; var scaleH = srcHeight / minHeight; var calibration = math.max(1.0, math.min(scaleW, scaleH)); render scale; } If your image width is smaller than minWidth or peak samller than minHeight, calibration will be 1, that is, the size will non change.
If you lot need to rotate the pic, use this parameter.
This property just exists in the version later 0.5.0.
And for historical reasons, there may be conflicts with rotate attributes, which need to be self-corrected.
Change rotate to 0 or autoCorrectionAngle to simulated.
Quality of target prototype.
If format is png, the param will be ignored in iOS.
Supports jpeg or png, default is jpeg.
The format class sign enum CompressFormat.
Heif and webp Partially supported.
Webp
Support android by the organization api (speed very dainty).
And support iOS, merely However, no organization implementation, using third-party libraries used, it is not recommended due to encoding speed. In the time to come, libwebp by google (c / c ++) may exist used to practice coding work, bypassing other three-party libraries, simply there is no guarantee of implementation fourth dimension.
HEIF(Heic)
Heif for iOS
Only support iOS eleven+.
Heif for Android
Use HeifWriter to implemation.
Merely support API 28+.
And may require hardware encoder support, does non guarantee that all devices above API28 are available
The param is only support android.
For a description of this parameter, see the Android official website.
If this parameter is true, EXIF information is saved in the compressed outcome.
Attending should exist paid to the post-obit points:
- Default value is false.
- Fifty-fifty if set to true, the direction attribute is not included.
- Simply back up jpg format, PNG format does not back up.
The consequence of returning a Listing drove will not have null, just volition always be an empty array.
The returned file may be nil. In addition, please decide for yourself whether the file exists.
About List<int> and Uint8List #
You may need to convert Listing<int> to Uint8List to display images.
To use Uint8List, y'all need import packet to your code like this:
final paradigm = Uint8List.fromList(imageList) ImageProvider provider = MemoryImage(Uint8List.fromList(imageList)); Usage in Image Widget:
List<int> prototype = look testCompressFile(file); ImageProvider provider = MemoryImage(Uint8List.fromList(image)); Image( image: provider ?? AssetImage("img/img.jpg"), ), Write to file usage:
void writeToFile(Listing<int> image, String filePath) { terminal file = File(filePath); file.writeAsBytes(image, flush: true, manner: FileMode.write); } Because of some support issues, all APIs will be compatible with format and system compatibility, and an exception (UnsupportError) may be thrown, so if you insist on using webp and heic formats, please catch the exception yourself and use it on unsupported devices jpeg pinch.
Example:
Future<Uint8List> compressAndTryCatch(String path) async { Uint8List issue; try { result = await FlutterImageCompress.compressWithFile(path, format: CompressFormat.heic); } on UnsupportedError catch (e) { print(e); consequence = wait FlutterImageCompress.compressWithFile(path, format: CompressFormat.jpeg); } render result; } Android #
You lot may need to update Kotlin to version one.3.72 or college.
No problems currently found.
Sometimes, compressing will return null. You lot should check if you can read/write the file, and the parent folder of the target file must exist.
For example, employ the path_provider plugin to access some awarding folders, and use a permission plugin to request permission to admission SD cards on Android/iOS.
Android build error #
Acquired past: org.gradle.internal.issue.ListenerNotificationException: Failed to notify project evaluation listener. at org.gradle.internal.outcome.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:86) ... Caused by: java.lang.AbstractMethodError at org.jetbrains.kotlin.gradle.plugin.KotlinPluginKt.resolveSubpluginArtifacts(KotlinPlugin.kt:776) ... See palpitate/palpitate/issues#21473
You lot need to upgrade your Kotlin version to 1.ii.71+(recommended 1.3.72).
If Palpitate supports more platforms (Windows, Mac, Linux, etc) in the future and you use this library, suggest an effect or PR!
Using this library, EXIF information will be removed by default.
EXIF information can exist retained past setting keepExif to true, but not direction information.
The code under MIT style.
Each using organisation API.
Employ SDWebImageWebPCoder to encode the UIImage in iOS. (Under MIT)
Android code use the Android system api.
Use iOS organisation api in iOS.
Use HeifWriter(androidx component by Google) to encode in androidP or higher.
About Exif handle code #
The iOS code was copied from dvkch/SYPictureMetadata, LICENSE
The android code was copied from flutter/plugin/image_picker and edit some. (BSD 3 mode)
Source: https://pub.dev/packages/flutter_image_compress
0 Response to "Compress Video Flutter and Upload to Firebase"
Post a Comment