Chaythanya Nair
Chaythanya Nair

Reputation: 5064

Convert jpg image to png image in Flutter iOS

How can I convert a jpg image selected from photo gallery to png image in flutter?

Upvotes: 5

Views: 13548

Answers (5)

martiwg
martiwg

Reputation: 95

If you're working directly with bytes, you can do the following:

  import 'dart:typed_data';
  import 'package:image/image.dart';

  Uint8List jpgToPng(Uint8List bytes) {
    final jpgImage = decodeImage(bytes);
    final pngImage = copyResize(
      jpgImage!,
      width: jpgImage.width,
      height: jpgImage.height,
    );

    return Uint8List.fromList(encodePng(jpgImage));
  }

This will return the PNG bytes without you needing to keep track of the image dimensions.

Upvotes: 1

JasonP
JasonP

Reputation: 11

Many of the suggestions listed are good, I just wanted to add something that may confuse some people. If you're getting black images, see if you have alpha channels in the image. I use the Image package for my purposes, so I just add one during the decode: img.decodeImage(imageFile.readAsBytesSync())..channels = img.Channels.rgba

I also use the Image/Paint method to get a Dart UI Image as .png:

img = Image package, thumbnail is an img Image.

import 'dart:ui' as ui;
import 'package:image/image.dart' as img;

    ui.Image imageN;
        try {
          final paint = await PaintingBinding.instance
              .instantiateImageCodec(img.encodePng(thumbnail, level: 0));
          final nextFrame = await paint.getNextFrame();
          imageN = nextFrame.image;
        } catch (e, s) {
          // handle the exception
        }
        return imageN;

Upvotes: 1

goops17
goops17

Reputation: 1160

First thing you need to do is import IMAGE library. Then using similar custom function like below you can convert JPG to 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));     
}

Upvotes: 3

Raouf Rahiche
Raouf Rahiche

Reputation: 31316

using image library you can do this

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

Upvotes: 1

alex kucksdorf
alex kucksdorf

Reputation: 2633

Take a look at the image package. The following is a snippet available in the examples section, which converts JPEG to 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));
}

Upvotes: 10

Related Questions