Created
September 19, 2019 09:34
-
-
Save CaiJingLong/8fa83aff71a4820fb5092f36e255a6a3 to your computer and use it in GitHub Desktop.
处理jpg格式的元数据, 后续会继续更新
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
import 'dart:io'; | |
main(List<String> arguments) async { | |
// final path = "timg.jpg"; | |
// final path = "/Users/cai/Desktop/1CD83646CA9CF1C2B0F7364B341E8580.gif"; | |
final path = "/Users/cai/Desktop/sxwphone.png"; | |
final imgFile = File(path); | |
final result = await isJpg(imgFile); | |
print("${imgFile.path} is jpg : $result"); | |
} | |
Future<bool> isJpg(File file) async { | |
if (file == null || !file.existsSync()) { | |
return false; | |
} | |
final length = file.lengthSync(); | |
final completer = MyCompleter<bool>(); | |
void handleEnd() { | |
file.openRead(length - 2, length).listen((data) { | |
if (data[0] == 0xFF && data[1] == 0xD9) { | |
completer.reply(true); | |
} else { | |
completer.reply(false); | |
} | |
}); | |
} | |
void handleStart() async { | |
file.openRead(0, 2).listen((data) { | |
if (data[0] == 0xFF && data[1] == 0xD8) { | |
handleEnd(); | |
} else { | |
completer.reply(false); | |
} | |
}); | |
} | |
handleStart(); | |
return completer.future; | |
} | |
class MyCompleter<T> { | |
Completer<T> completer = Completer(); | |
MyCompleter(); | |
Future<T> get future => completer.future; | |
void reply(T result) { | |
if (!completer.isCompleted) { | |
completer.complete(result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment