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);
}
/// 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});
}
}