Marcin
Marcin

Reputation: 55

Image processing in Dart

I create app which recognize elements from picture. Now I have image in File class and I want to rotate image about few degrees and again have image in File class. Is there any solution how to resolve this?

P.S I don't want display this image. I must pass image as File object to some method.

Upvotes: 1

Views: 2214

Answers (1)

cmd_prompter
cmd_prompter

Reputation: 1680

To rotate the actual file in memory use the image library and the copyRotate function.

Image copyRotate(Image src, num angle, {
   Interpolation interpolation = Interpolation.nearest
  }
);
//Returns a copy of the [src] image, rotated by [angle] degrees.

Example:

import 'dart:io';
import 'package:image/image.dart';
void main() {
  // Read an image from file (webp in this case).
  // decodeImage will identify the format of the image and use the appropriate
  // decoder.
  Image image = decodeImage(File('test.webp').readAsBytesSync());

  Image thumbnail = copyRotate(image, 120);

  // Save the thumbnail as a PNG.
  File('thumbnail.png').writeAsBytesSync(encodePng(thumbnail));
}

source

To display a rotated version of the image stored, you can use a RotationTransition with an AlwaysStoppedAnimation like-

new RotatedBox(
  quarterTurns: 1,
  child: new Text("Lorem ipsum")
)

or the Transform.rotate() widget like-

  Transform.rotate(angle: - math.pi / 4, child: Text("Text"),);

for your use case-

  Transform.rotate(angle: degrees*3.14/180, child: Image.asset("file_path"),);

Upvotes: 2

Related Questions