代码之家  ›  专栏  ›  技术社区  ›  Chaythanya Nair

Flutter iOS中jpg图像到png图像的转换

  •  0
  • Chaythanya Nair  · 技术社区  · 7 年前

    如何将从照片库中选择的jpg图像转换为颤振中的png图像?

    3 回复  |  直到 7 年前
        1
  •  7
  •   alex kucksdorf    7 年前

    看看 image package . 以下是 examples section JPEG PNG :

    import 'dart:io';
    import 'package:image/image.dart';
    void main() {
      // Read a jpeg image from file.
      Image image = decodeImage(new File('test.jpg').readAsBytesSync());
    
      // Resize the image to a 120x? thumbnail (maintaining the aspect ratio).
      Image thumbnail = copyResize(image, 120);
    
      // Save the thumbnail as a PNG.
      new File('out/thumbnail-test.png')
        ..writeAsBytesSync(encodePng(thumbnail));
    }
    
        2
  •  1
  •   Elia Weiss    6 年前

    你要做的第一件事就是导入 IMAGE 图书馆。然后使用下面类似的自定义函数可以将JPG转换为PNG

    import 'package:flutter/material.dart';
    import 'dart:io';
    import 'dart:convert';
    import 'package:image/image.dart' as Im;
    import 'dart:math' as Math;
    void jpgTOpng(path) async {
      File imagePath = File(path);
      //get temporary directory
      final tempDir = await getTemporaryDirectory();
      int rand = new Math.Random().nextInt(10000);
      //reading jpg image
      Im.Image image = Im.decodeImage(imagePath.readAsBytesSync());
      //decreasing the size of image- optional
      Im.Image smallerImage = Im.copyResize(image, width:800);
          //get converting and saving in file
      File compressedImage = new File('${tempDir.path}/img_$rand.png')..writeAsBytesSync(Im.encodePng(smallerImage, level:8));     
    }
    
        3
  •  0
  •   Raouf Rahiche AbdulMomen عبدالمؤمن    7 年前

    使用 image 图书馆你可以这样做

    jpegToPng(jpegimage){
    new File('output.png')
        ..writeAsBytesSync(encodePng(thumbnail));
    }