How to convert image dpi (Dots per inch) in dart flutter?

Viewed 272

The question is related to (print) DPI of various images ( for eg. of jpeg or png ) format.

This question does NOT relate to SCREEN DPI or sizes of various Android devices. It is also NOT related to showing the Bitmap on the device in screen size.

1 Answers

This will convert the image resolution dots per inch to 300.

Uint8List resultBytes = provider.bytes;
  print(resultBytes.toString());
  
  var dpi = 300;
  resultBytes[13] = 1;
  resultBytes[14] = (dpi >> 8);
  resultBytes[15] = (dpi & 0xff);
  resultBytes[16] = (dpi >> 8);
  resultBytes[17] = (dpi & 0xff);

  print(resultBytes.toString());

  String tempPath = (await getTemporaryDirectory()).path;
  String imgName = "IMG" + DateTime.now().microsecondsSinceEpoch.toString()+".jpg";
  File file = File('$tempPath/$imgName');
  await file.writeAsBytes(resultBytes);

  /**You will not be able to see the image in android local storage, so rewriting the file, using the code below will show you image in Pictures Directory of android storage. Note: ImageGallerySaver is plugin, just copy and paste the dependency and have a go.*/

  final result1 = ImageGallerySaver.saveFile(file.path);
  print(result1);
Related