wang-bin / fvp

Flutter video player plugin for all desktop+mobile platforms. download prebuilt examples from github actions. https://pub.dev/packages/fvp
BSD 3-Clause "New" or "Revised" License
197 stars 31 forks source link

截图产生的图片数组损坏,无法生成有效的图片 #135

Closed yhsj0919 closed 3 months ago

yhsj0919 commented 3 months ago

这个是我的测试代码

import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:fvp/mdk.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Video Player',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: VideoScreen(),
    );
  }
}

class VideoScreen extends StatefulWidget {
  @override
  _VideoScreenState createState() => _VideoScreenState();
}

class _VideoScreenState extends State<VideoScreen> {
  late final player = Player();
  Uint8List? imageData;

  @override
  void dispose() {
    player.dispose();
    super.dispose();
  }

  Future<void> play() async {
    player.media = 'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4';
    player.loop = -1;
    player.state = PlaybackState.playing;
    player.updateTexture();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: Row(
        mainAxisAlignment: MainAxisAlignment.end,
        mainAxisSize: MainAxisSize.min,
        children: [
          IconButton(
              onPressed: () async {
                player.snapshot(width: 54, height: 54).then((v) {
                  File img = File("D:/test.jpg");
                  if (v != null) {
                    img.writeAsBytesSync(v);
                    setState(() {
                      imageData = v;
                      print(imageData?.map((e) => e.toRadixString(16).padLeft(2, '0').toUpperCase()).toList());
                    });
                  }
                });
              },
              icon: const Icon(Icons.image)),
          IconButton(
              onPressed: () {
                play();
              },
              icon: const Icon(Icons.play_arrow)),
        ],
      ),
      body: Stack(
        children: [
          AspectRatio(
            aspectRatio: 16 / 9,
            child: ValueListenableBuilder<int?>(
              valueListenable: player.textureId,
              builder: (context, id, _) => id == null ? const SizedBox.shrink() : Texture(textureId: id),
            ),
          ),
          Center(
            child: imageData != null ? Image.memory(imageData!) : const CircularProgressIndicator(),
          )
        ],
      ),
    );
  }
}

生成图片会报一个图片无效的错误

======== Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
wang-bin commented 3 months ago

生成的是 rgba数据,你得用dart编码成jpeg再写文件

yhsj0919 commented 3 months ago

通过image包解决了

File imgFile = File("D:/test.jpg");
if (v != null) {
     //创建图像,指定编码
    img.Image image = img.Image.fromBytes(width: 1280, height: 720, bytes: v.buffer, order: img.ChannelOrder.rgba);
    //重新编码
    List<int> jpg = img.encodeJpg(image);
    //保存图片
    imgFile.writeAsBytesSync(jpg);
}