Newer
Older
MiniTias / lib / services / raw_capture_service.dart
import 'package:flutter/services.dart';

/// Camera2 API を使用してフル解像度の YUV 画像をキャプチャするサービス.
class RawCaptureService {
  static const _channel = MethodChannel('com.example.mini_tias/raw_capture');

  /// フロントカメラからフル解像度の YUV データをキャプチャする.
  Future<Map<String, dynamic>> captureFullResolutionYuv() async {
    final result = await _channel
        .invokeMethod('captureFullResolutionYuv')
        .timeout(
          const Duration(seconds: 30),
          onTimeout: () => throw Exception('カメラキャプチャがタイムアウトしました'),
        );
    return Map<String, dynamic>.from(result as Map);
  }

  /// PixelCopy でカメラプレビュー領域のスナップショットを JPEG として取得する.
  Future<Uint8List> capturePreviewFrame({
    required int x,
    required int y,
    required int width,
    required int height,
  }) async {
    final result = await _channel.invokeMethod<Uint8List>(
      'capturePreviewFrame',
      {'x': x, 'y': y, 'width': width, 'height': height},
    );
    if (result == null) {
      throw Exception('プレビューフレームの取得に失敗しました');
    }
    return result;
  }

  /// MediaStore にファイルを登録し,PC から MTP で見えるようにする.
  Future<void> scanFile(String path) async {
    await _channel.invokeMethod('scanFile', {'path': path});
  }
}