jazzbpn
jazzbpn

Reputation: 7318

Merge list of Images files to single-image-file

Unable to merge image to vertical alignment and sharing that file. In this case the last image file will only added to the end of the mergeImage. Please see this code:

enter image description here

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');

Upvotes: 0

Views: 1385

Answers (2)

Saurabh Kumar
Saurabh Kumar

Reputation: 2763

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;
  }

Upvotes: 0

jazzbpn
jazzbpn

Reputation: 7318

Issue with fullMergeImage declaration.

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');
}

Upvotes: 0

Related Questions