A Flutter plugin based on AndroidUSBCamera to enable Flutter apps to use external cameras.
Add the flutter_uvc_camera
plugin dependency to your Flutter project's pubspec.yaml
file:
dependencies:
flutter_uvc_camera: last_version
Before using this plugin, some configurations are needed for the Android project.
Add the following permissions to the AndroidManifest.xml file of your Android project:
<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.usb.host" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<!-- Add other necessary permissions here -->
maven { url "https://jitpack.io" }
in android/build.gradle:allprojects {
repositories {
/// other repositories
maven { url "https://jitpack.io" }
}
}
Android documents USB host overview
Add an action for USB device connection in the intent-filter of mainActivity, and reference the corresponding XML file in meta-data:
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" />
</resources>
import 'package:flutter/material.dart';
import 'package:flutter_uvc_camera/uvc_camera_controller.dart';
import 'package:flutter_uvc_camera/uvc_camera_view.dart';
class CameraTest extends StatefulWidget {
const CameraTest({super.key});
@override
State<CameraTest> createState() => _CameraTestState();
}
class _CameraTestState extends State<CameraTest> {
UVCCameraController? cameraController;
@override
void initState() {
super.initState();
cameraController = UVCCameraController();
cameraController?.msgCallback = (state) {
showCustomToast(state);
};
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('UVC Camera Example'),
),
body: Center(
child: UVCCameraView(
cameraController: cameraController!,
width: 300,
height: 300),
),
),
);
}
}
flutter run release encounters a NoSuchMethodError
buildTypes {
release {
<!-- ...-->
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
minifyEnabled true
}
}
-keep class com.jiangdg.uvc.UVCCamera {
native <methods>;
long mNativePtr;
}
-keep class com.jiangdg.uvc.IStatusCallback {
*;
}
-keep interface com.jiangdg.uvc.IButtonCallback {
*;
}
If you encounter any problems or have any suggestions during usage, please report them on GitHub Issues .