简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM