基础库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

encode_util.dart 2.7 KiB

4 년 전
4 년 전
4 년 전
4 년 전
4 년 전
4 년 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'package:crypto/crypto.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'dart:convert';
  4. import 'dart:io';
  5. import 'package:flutter_native_image/flutter_native_image.dart';
  6. import 'package:zhiying_comm/util/dialog/loading/loading.dart';
  7. import 'package:zhiying_comm/zhiying_comm.dart';
  8. class EncodeUtil {
  9. /*
  10. * Md5加密
  11. * */
  12. static String generateMd5(String data) {
  13. var content = new Utf8Encoder().convert(data);
  14. var digest = md5.convert(content);
  15. return digest.toString();
  16. }
  17. /*
  18. * Base64加密
  19. */
  20. static String encodeBase64(String data) {
  21. var content = utf8.encode(data);
  22. var digest = base64Encode(content);
  23. return digest;
  24. }
  25. /*
  26. * Base64解密
  27. */
  28. static String decodeBase64(String data) {
  29. // return String.fromCharCodes(base64Decode(data));
  30. return utf8.decode(base64Decode(data));
  31. }
  32. // 图片转成base64
  33. static Future<String> image2Base64(File file) async {
  34. // File file = new File(path);
  35. List<int> imageBytes = await file.readAsBytes();
  36. return base64Encode(imageBytes);
  37. }
  38. // 压缩图片至指定大小
  39. static Future<File> compressImage(BuildContext context, {File file, List<int> images, int size}) async {
  40. List<int> originBytes;
  41. if (images != null) {
  42. originBytes = images;
  43. Directory dir = await getTemporaryDirectory();
  44. File tempFile = File(dir.path + "/temp.png");
  45. tempFile = await tempFile.writeAsBytes(originBytes);
  46. file = tempFile;
  47. } else if (file != null) {
  48. originBytes = await file.readAsBytes();
  49. } else {
  50. return null;
  51. }
  52. int originSize = (originBytes.length / 1024).ceil();
  53. print('原图大小:' + originSize.toString() + 'kb');
  54. if (originSize <= size) {
  55. return file;
  56. }
  57. int minQuality = 0;
  58. int maxQuality = 100;
  59. List<int> imageBytes = originBytes;
  60. do {
  61. int midQuality = ((minQuality + maxQuality) / 2).floor();
  62. File compressedFile = await FlutterNativeImage.compressImage(file.path,
  63. quality: midQuality);
  64. imageBytes = await compressedFile.readAsBytes();
  65. int compressSize = (imageBytes.length / 1024).ceil();
  66. if (compressSize == size) {
  67. break;
  68. } else if (compressSize > size) {
  69. maxQuality = midQuality;
  70. } else {
  71. minQuality = midQuality;
  72. }
  73. } while (maxQuality - minQuality > 1);
  74. File compressedFile =
  75. await FlutterNativeImage.compressImage(file.path, quality: minQuality);
  76. imageBytes = await compressedFile.readAsBytes();
  77. int compressSize = (imageBytes.length / 1024).ceil();
  78. print('quality:' +
  79. minQuality.toString() +
  80. ' 压缩后大小:' +
  81. compressSize.toString() +
  82. 'kb');
  83. return compressedFile;
  84. }
  85. }