Levi-Lesches / opencv_ffi

An FFI-based implementation of OpenCV in Dart
https://pub.dev/packages/opencv_ffi
Other
16 stars 1 forks source link

How to on Android #8

Closed bipinkrish closed 4 months ago

bipinkrish commented 9 months ago

This works in Windows and Linux but from this page it should also work in Android but when i tried. I got exception platform not supported

Levi-Lesches commented 9 months ago

Theoretically Android is supported, I just haven't gotten around to integration testing it yet. All that's left is to build OpenCV on Android, get the binaries to the Android platform, and modify _getPath to handle Platform.isAndroid:

https://github.com/Levi-Lesches/opencv_ffi/blob/45f4b9beb8c177641f63b2852f01747b03289305/lib/src/ffi.dart#L7-L18

That change can be made here, but the issue of building and linking to OpenCV will be handled within package:opencv_camera, which is a proper Flutter plugin, so that Flutter can build OpenCV in its normal build process. I see you already opened an issue for it at https://github.com/Levi-Lesches/opencv_camera/issues/1, so I'll leave this open until both sides are dealt with.

This package is still in active development with a current focus on bringing more features to desktop, so any help on the Android side is welcome!

bipinkrish commented 9 months ago

i was able to build for android but i have one question, what value should i use for CMAKE_BUILD_TYPE

1. Release: high optimization level, no debug info, code or asserts.
2. Debug: No optimization, asserts enabled, [custom debug (output) code enabled],
   debug info included in executable (so you can step through the code with a
   debugger and have address to source-file:line-number translation).
3. RelWithDebInfo: optimized, *with* debug info, but no debug (output) code or asserts.
4. MinSizeRel: same as Release but optimizing for size rather than speed.
Levi-Lesches commented 9 months ago

That's more of a question for the Flutter side (package:opencv_camera), as that's affected by flutter run vs flutter build (or flutter run --release) -- see CMAKE_BUILD_TYPE for details there.

This underlying package is more open-ended -- pick whatever works best for you and your platform. My build scripts don't define CMAKE_BUILD_TYPE at all so you're free to change it yourself. I'd personally recommend using Release or MinSizeRel if you stay within the confines of this package as it's supposed to handle OpenCV errors internally. When testing or debugging through errors this package should be handling, Debug or RelWithDebInfo might be more useful.

bipinkrish commented 9 months ago

Update :

I was able to load binaries on Android by moving it to respective architecture directories

and modifying opencv_ffi/lib/src/ffi.dart

 else if (Platform.isAndroid) {
    return "libopencv_ffi.so";
  } 

I tried to run a simple application to check if it works

import 'package:flutter/material.dart';
import "package:opencv_ffi/opencv_ffi.dart";
import 'package:permission_handler/permission_handler.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Future<void> _requestCameraPermission() async {
    final status = await Permission.camera.status;
    if (status.isGranted) {
      final camera = Camera.fromName("/dev/video0");
      debugPrint(camera.isOpened.toString());
    } else {
      await Permission.camera.request();
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: TextButton(
          onPressed: () {
            _requestCameraPermission();
          },
          child: const Text("Click"),
        ),
      ),
    );
  }
}

even with these in Manifest

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />

i got

W/1.ui    (13172): type=1400 audit(0.0:34732): avc: denied { getattr } for path="/dev/video0" dev="tmpfs" ino=8601 scontext=u:r:untrusted_app:s0:c198,c257,c512,c768 tcontext=u:object_r:video_device:s0 tclass=chr_file permissive=0 app=com.example.app
W/1.ui    (13172): type=1400 audit(0.0:34733): avc: denied { getattr } for path="/dev/video1" dev="tmpfs" ino=11316 scontext=u:r:untrusted_app:s0:c198,c257,c512,c768 tcontext=u:object_r:video_device:s0 tclass=chr_file permissive=0 app=com.example.app
I/flutter (13172): false

when i checked ls -l /dev in adb shell

crw-rw----  1 media       system        81,   0 2023-09-24 04:30 video0
crw-rw----  1 media       system        81,   1 2023-09-24 04:30 video1
crw-rw----  1 camera      system        81,   2 2023-09-24 04:30 video2
crw-rw----  1 camera      system        81,   3 2023-09-24 04:30 video3

but it needs to be in group camera instead of system

if you know to solve this, guide me please

Levi-Lesches commented 9 months ago

I don't really know the answer here but here are some resources for you to try:

At this point my advice would be to use package:camera, as it seems OpenCV's VideoCapture probably isn't going to work for you. Would love to be proven wrong though.

Levi-Lesches commented 4 months ago

@bipinkrish, any update? I'll close this issue as opencv_ffi is intended to use OpenCV over FFI, as the name implies, and if C++ OpenCV isn't supported on Android, then supporting Android in this package isn't a goal for me.

If you can get it to work please feel free to re-open and we can add instructions to the README.

bipinkrish commented 4 months ago

If you can get it to work please feel free to re-open

Sure.