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

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

  /// フロントカメラからフル解像度の PNG バイト列をキャプチャする(ネイティブ変換).
  Future<Uint8List> captureFullResolutionPng() async {
    final result = await _channel
        .invokeMethod<Uint8List>('captureFullResolutionPng')
        .timeout(
          const Duration(seconds: 30),
          onTimeout: () => throw Exception('カメラキャプチャがタイムアウトしました'),
        );
    if (result == null) {
      throw Exception('PNG キャプチャに失敗しました');
    }
    return result;
  }

  /// YUV_420_888 を JPEG に変換する(Android ネイティブの高速変換).
  Future<Uint8List> convertYuvToJpeg({
    required int width,
    required int height,
    required Uint8List yPlane,
    required Uint8List uPlane,
    required Uint8List vPlane,
    required int yRowStride,
    required int uvRowStride,
    required int uvPixelStride,
    int rotation = 0,
    bool mirror = false,
    int quality = 85,
  }) async {
    final result = await _channel.invokeMethod<Uint8List>('convertYuvToJpeg', {
      'width': width,
      'height': height,
      'yPlane': yPlane,
      'uPlane': uPlane,
      'vPlane': vPlane,
      'yRowStride': yRowStride,
      'uvRowStride': uvRowStride,
      'uvPixelStride': uvPixelStride,
      'rotation': rotation,
      'mirror': mirror,
      'quality': quality,
    });
    if (result == null) {
      throw Exception('YUV to JPEG 変換に失敗しました');
    }
    return result;
  }

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