mirror of
https://github.com/haorendashu/nowser.git
synced 2025-12-17 01:44:19 +01:00
sometime shortcut image handle error
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||||
@@ -132,6 +133,11 @@ class _BookmarkRouter extends CustState<BookmarkRouter>
|
|||||||
final _flutterPinnedShortcutPlugin = FlutterPinnedShortcut();
|
final _flutterPinnedShortcutPlugin = FlutterPinnedShortcut();
|
||||||
|
|
||||||
Future<void> doAddPinnedShortcut(Bookmark bookmark) async {
|
Future<void> doAddPinnedShortcut(Bookmark bookmark) async {
|
||||||
|
if (StringUtil.isBlank(bookmark.title) ||
|
||||||
|
StringUtil.isBlank(bookmark.url)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
File? file;
|
File? file;
|
||||||
if (StringUtil.isNotBlank(bookmark.favicon)) {
|
if (StringUtil.isNotBlank(bookmark.favicon)) {
|
||||||
// use favicon as icon
|
// use favicon as icon
|
||||||
@@ -143,40 +149,44 @@ class _BookmarkRouter extends CustState<BookmarkRouter>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final originalBytes = await file.readAsBytes();
|
var filepath = file.path;
|
||||||
final originalImage = img.decodeImage(originalBytes);
|
|
||||||
if (originalImage == null) {
|
|
||||||
print("Failed to decode image");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final targetSize = 256;
|
try {
|
||||||
final newImage = img.Image(width: targetSize, height: targetSize);
|
final originalBytes = await file.readAsBytes();
|
||||||
img.fill(newImage, color: img.ColorRgb8(240, 240, 240));
|
final originalImage = img.decodeImage(originalBytes);
|
||||||
|
if (originalImage == null) {
|
||||||
|
print("Failed to decode image");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
final scale = (targetSize * 0.6).toDouble() /
|
final targetSize = 512;
|
||||||
(originalImage.width > originalImage.height
|
final newImage = img.Image(width: targetSize, height: targetSize);
|
||||||
? originalImage.width
|
img.fill(newImage, color: img.ColorRgb8(240, 240, 240));
|
||||||
: originalImage.height);
|
|
||||||
final int newWidth = (originalImage.width * scale).round();
|
|
||||||
final int newHeight = (originalImage.height * scale).round();
|
|
||||||
|
|
||||||
final resizedImage =
|
final scale = (targetSize * 0.6).toDouble() /
|
||||||
img.copyResize(originalImage, width: newWidth, height: newHeight);
|
(originalImage.width > originalImage.height
|
||||||
|
? originalImage.width
|
||||||
|
: originalImage.height);
|
||||||
|
final int newWidth = max((originalImage.width * scale).round(), 1);
|
||||||
|
final int newHeight = max((originalImage.height * scale).round(), 1);
|
||||||
|
|
||||||
final int x = (targetSize - newWidth) ~/ 2;
|
final resizedImage =
|
||||||
final int y = (targetSize - newHeight) ~/ 2;
|
img.copyResize(originalImage, width: newWidth, height: newHeight);
|
||||||
|
|
||||||
img.compositeImage(newImage, resizedImage, dstX: x, dstY: y);
|
final int x = (targetSize - newWidth) ~/ 2;
|
||||||
|
final int y = (targetSize - newHeight) ~/ 2;
|
||||||
|
|
||||||
final tempDir = await getTemporaryDirectory();
|
img.compositeImage(newImage, resizedImage, dstX: x, dstY: y);
|
||||||
final resizedFile = File(
|
|
||||||
'${tempDir.path}/icon_${DateTime.now().millisecondsSinceEpoch}.png');
|
|
||||||
await resizedFile.writeAsBytes(img.encodePng(newImage));
|
|
||||||
|
|
||||||
if (StringUtil.isBlank(bookmark.title) ||
|
final tempDir = await getTemporaryDirectory();
|
||||||
StringUtil.isBlank(bookmark.url)) {
|
final resizedFile = File(
|
||||||
return;
|
'${tempDir.path}/icon_${DateTime.now().millisecondsSinceEpoch}.png');
|
||||||
|
await resizedFile.writeAsBytes(img.encodePng(newImage));
|
||||||
|
|
||||||
|
filepath = resizedFile.path;
|
||||||
|
} catch (e) {
|
||||||
|
print("create pinned shortcut failed!");
|
||||||
|
print(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
_flutterPinnedShortcutPlugin.createPinnedShortcut(
|
_flutterPinnedShortcutPlugin.createPinnedShortcut(
|
||||||
@@ -184,7 +194,7 @@ class _BookmarkRouter extends CustState<BookmarkRouter>
|
|||||||
label: bookmark.title!,
|
label: bookmark.title!,
|
||||||
action: bookmark.url!,
|
action: bookmark.url!,
|
||||||
iconAssetName: "assets/logo_android.png",
|
iconAssetName: "assets/logo_android.png",
|
||||||
iconUri: Uri.file(resizedFile.path).toString());
|
iconUri: Uri.file(filepath).toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> doEdit(Bookmark bookmark) async {
|
Future<void> doEdit(Bookmark bookmark) async {
|
||||||
|
|||||||
Reference in New Issue
Block a user