【问题标题】:How to turn asset image into bitmap in Flutter/Dart?如何在 Flutter/Dart 中将资产图像转换为位图?
【发布时间】:2020-01-09 18:14:47
【问题描述】:

我需要比较两个图像,因此我需要将这两个图像(资产)解码为位图。 我试过这个:

import 'package:flutter/services.dart';
import 'package:image/image.dart';

var imageData = await rootBundle.load("assets/images/test.png");
Image firstImage = Image.fromBytes(100, 100, imageData.buffer.asUint8List());
int pixel = firstImage.getPixel(5, 0);
print('$pixel');

但它不起作用,因为在我将getPixel(x, y) 的第二个参数设置为除 0 之外的任何值后,它会引发异常:

E/flutter (11752): [ERROR:flutter/shell/common/shell.cc(199)] Dart Error: Unhandled exception:
E/flutter (11752): RangeError (index): Index out of range: index should be less than 86: 101
E/flutter (11752): #0      _Uint32ArrayView.[] (dart:typed_data-patch/typed_data_patch.dart:3999:7)
E/flutter (11752): #1      Image.getPixel (package:image/src/image.dart:409:37)

我也试过这种方式:

Image firstImage = decodeImage(new File('assets/images/test.png').readAsBytesSync());
int pixel = firstImage.getPixel(1, 1);
print('$pixel');

但有下一个例外:

E/flutter (11752): FileSystemException: Cannot open file, path = 'assets/images/test.png' (OS Error: No such file or directory, errno = 2)

所以我正在寻找一种可以帮助我将资源转换为位图的灵魂,这样我就可以获取每个图像的每个像素的颜色。

【问题讨论】:

    标签: image flutter dart bitmap image-comparison


    【解决方案1】:

    试试这个:

    import 'package:image/image.dart' as img;
    
    ...
    
    ByteData imageBytes = await rootBundle.load('assets/images/test.png');
    List<int> values = imageBytes.buffer.asUint8List();
    img.Image photo;
    photo = img.decodeImage(values);
    int pixel = photo.getPixel(5, 0);
    

    【讨论】:

      猜你喜欢
      • 2020-05-12
      • 1970-01-01
      • 2019-11-05
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多