Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

img.Image resizedImage = img.copyResize(oriImage, 224, 224); #35

Closed
emailsubjekt opened this issue Jun 6, 2019 · 3 comments
Closed

Comments

@emailsubjekt
Copy link

Hi, the copyResize method in Dart packages has been changed by the Flutter team and no longer takes in 3 arguments, but one. How do you solve this? This is the new class, if you make a new project in Flutter, you will see an error on the line in the subject, and you can inspect the same class, here it is

import 'dart:typed_data';

import '../color.dart';
import '../image.dart';
import '../image_exception.dart';
import '../util/interpolation.dart';
import 'bake_orientation.dart';

/// Returns a resized copy of the [src] image.
/// If [height] isn't specified, then it will be determined by the aspect
/// ratio of [src] and [width].
/// If [width] isn't specified, then it will be determined by the aspect ratio
/// of [src] and [height].
Image copyResize(Image src, {int width, int height,
Interpolation interpolation = Interpolation.nearest}) {
if (width == null && height == null) {
throw ImageException('Invalid size');
}

src = bakeOrientation(src);

if (height == null || height <= 0) {
height = (width * (src.height / src.width)).toInt();
}

if (width == null || width <= 0) {
width = (height * (src.width / src.height)).toInt();
}

if (width == src.width && height == src.height) {
return src.clone();
}

Image dst = Image(width, height, channels: src.channels, exif: src.exif,
iccp: src.iccProfile);

double dy = src.height / height;
double dx = src.width / width;

if (interpolation == Interpolation.average) {
Uint8List sData = src.getBytes();
int sw4 = src.width * 4;

for (int y = 0; y < height; ++y) {
  int y1 = (y * dy).toInt();
  int y2 = ((y + 1) * dy).toInt();
  if (y2 == y1) {
    y2++;
  }

  for (int x = 0; x < width; ++x) {
    int x1 = (x * dx).toInt();
    int x2 = ((x + 1) * dx).toInt();
    if (x2 == x1) {
      x2++;
    }

    int r = 0;
    int g = 0;
    int b = 0;
    int a = 0;
    int np = 0;
    for (int sy = y1; sy < y2; ++sy) {
      int si = sy * sw4 + x1 * 4;
      for (int sx = x1; sx < x2; ++sx, ++np) {
        r += sData[si++];
        g += sData[si++];
        b += sData[si++];
        a += sData[si++];
      }
    }
    dst.setPixel(x, y, getColor(r ~/ np, g ~/ np, b ~/ np, a ~/ np));
  }
}

} else if (interpolation == Interpolation.nearest) {
final scaleX = Int32List(width);
for (int x = 0; x < width; ++x) {
scaleX[x] = (x * dx).toInt();
}
for (int y = 0; y < height; ++y) {
int y2 = (y * dy).toInt();
for (int x = 0; x < width; ++x) {
dst.setPixel(x, y, src.getPixel(scaleX[x], y2));
}
}
} else {
// Copy the pixels from this image to the new image.
for (int y = 0; y < height; ++y) {
double y2 = (y * dy);
for (int x = 0; x < width; ++x) {
double x2 = (x * dx);
dst.setPixel(x, y, src.getPixelInterpolate(x2, y2, interpolation));
}
}
}

return dst;
}

For now, I have just done this

img.Image resizedImage = img.copyResize(oriImage);

and it works, but I don't know if this will change the results a lot, is there a better solution?

@emailsubjekt emailsubjekt changed the title img.Image resizedImage = img.copyResize(oriImage, 224, 224); //OG img.Image resizedImage = img.copyResize(oriImage, 224, 224); Jun 6, 2019
@emailsubjekt
Copy link
Author

emailsubjekt commented Jun 6, 2019

Nevermind, I saw someone already opened this 11 days ago and you helped them with this

img.Image resizedImage = img.copyResize(oriImage, width: 224, height: 224);

Thanks!

@shaqian
Copy link
Owner

shaqian commented Jun 16, 2019

Yes. It's a breaking change in flutter image library. I fixed it to the old version in latest commit.

Thanks,
Qian

@shaqian
Copy link
Owner

shaqian commented Jun 23, 2019

Archiving the thread as it's resolved.

@shaqian shaqian closed this as completed Jun 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants