dart 将图像文件列表合并到单个图像文件

vlju58qv  于 2023-04-27  发布在  其他
关注(0)|答案(2)|浏览(172)

无法将图像合并到垂直对齐并共享该文件。在这种情况下,最后一个图像文件将仅添加到mergeImage的末尾。请参阅以下代码:

import 'package:image/image.dart' as im;

final path = (await getTemporaryDirectory()).path;
var i = 0;
List<String> paths = [];

await for (final page in Printing.raster(pdf, dpi: 72)) {
  final png = await page.toPng();
  final file = File('$path/page-${i.toString().padLeft(3, '0')}.png');
  await file.writeAsBytes(png);
  print('Saved to ${file.absolute.path}');
  paths.add(file.absolute.path);

  i++;
  print('object');
}

int height = 0;
int width = im.decodeImage(File(paths.first).readAsBytesSync()).width;
for (var item in paths) {
  final image = im.decodeImage(File(item).readAsBytesSync());
  height += image.height + 10;
}

im.Image fullMergeImage;
int pos = 0;
for (var item in paths) {
  print('object');
  fullMergeImage = im.Image(width, height);
  final image = im.decodeImage(File(item).readAsBytesSync());
  im.copyInto(fullMergeImage, image, dstY: pos, blend: false);
  pos += image.height + 10;
}

final documentDirectory = await getTemporaryDirectory();
final file = new File(documentDirectory.path + '.png');
file.writeAsBytesSync(im.encodeJpg(fullMergeImage));

await Share.shareFiles([file.absolute.path], text: 'Great picture');
njthzxwz

njthzxwz1#

fullMergeImage声明有问题。

im.Image fullMergeImage;
int pos = 0;
fullMergeImage = im.Image(width, height);
for (var item in paths) {
  final image = im.decodeImage(File(item).readAsBytesSync());
  im.copyInto(fullMergeImage, image, dstY: pos, blend: false);
  pos += image.height + 10;
  print('object');
}
yk9xbfzb

yk9xbfzb2#

Image stackImagesVertically(List<Image> images) {
    int singleWidth = images[0].width;
    int singleHeight = images[0].height;

    Image stackedImage = Image(singleWidth, singleHeight * images.length);

    for (int i = 0; i < images.length; i++) {
      copyInto(stackedImage, images[i], dstY: i * singleHeight);
    }

    return stackedImage;
  }

相关问题