Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
L
laki_icu_app
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张宏
laki_icu_app
Commits
8983fa7f
Commit
8983fa7f
authored
Jun 17, 2026
by
张宏
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
888c9417
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
39 additions
and
370 deletions
+39
-370
.flutter-plugins-dependencies
.flutter-plugins-dependencies
+0
-0
build.gradle
android/app/build.gradle
+1
-1
MainActivity.kt
...pp/src/main/kotlin/com/qialg/laki_icu_app/MainActivity.kt
+36
-27
UsbCameraManager.kt
.../kotlin/com/qialg/laki_icu_app/camera/UsbCameraManager.kt
+0
-170
UsbCameraPlugin.kt
...n/kotlin/com/qialg/laki_icu_app/camera/UsbCameraPlugin.kt
+0
-88
UsbCameraView.kt
...ain/kotlin/com/qialg/laki_icu_app/camera/UsbCameraView.kt
+0
-62
UsbCameraViewFactory.kt
...lin/com/qialg/laki_icu_app/camera/UsbCameraViewFactory.kt
+0
-22
pubspec.yaml
pubspec.yaml
+2
-0
No files found.
.flutter-plugins-dependencies
View file @
8983fa7f
This diff is collapsed.
Click to expand it.
android/app/build.gradle
View file @
8983fa7f
...
@@ -99,5 +99,5 @@ flutter {
...
@@ -99,5 +99,5 @@ flutter {
}
}
dependencies
{
dependencies
{
implementation
'com.github.jiangdongguo.AndroidUSBCamera:libausbc:3.3.3'
//
implementation 'com.github.jiangdongguo.AndroidUSBCamera:libausbc:3.3.3'
}
}
android/app/src/main/kotlin/com/qialg/laki_icu_app/MainActivity.kt
View file @
8983fa7f
package
com.
example
.laki_icu_app
package
com.
qialg
.laki_icu_app
import
com.example.laki_icu_app.camera.UsbCameraPlugin
import
com.example.laki_icu_app.camera.UsbCameraViewFactory
import
io.flutter.embedding.android.FlutterActivity
import
io.flutter.embedding.android.FlutterActivity
import
io.flutter.embedding.engine.FlutterEngine
import
io.flutter.plugin.common.MethodChannel
class
MainActivity
:
FlutterActivity
()
{
class
MainActivity
:
FlutterActivity
()
{
}
companion
object
{
const
val
USB_CAMERA_CHANNEL
=
"usb_camera"
const
val
USB_CAMERA_VIEW_TYPE
=
"usb_camera_view"
}
private
var
usbCameraPlugin
:
UsbCameraPlugin
?
=
null
override
fun
configureFlutterEngine
(
flutterEngine
:
FlutterEngine
)
{
// package com.example.laki_icu_app
super
.
configureFlutterEngine
(
flutterEngine
)
// 注册 PlatformViewFactory —— 用于在 Flutter 中渲染摄像头预览画面
// import com.example.laki_icu_app.camera.UsbCameraPlugin
flutterEngine
// import com.example.laki_icu_app.camera.UsbCameraViewFactory
.
platformViewsController
// import io.flutter.embedding.android.FlutterActivity
.
registry
// import io.flutter.embedding.engine.FlutterEngine
.
registerViewFactory
(
USB_CAMERA_VIEW_TYPE
,
UsbCameraViewFactory
())
// import io.flutter.plugin.common.MethodChannel
// 注册 MethodChannel —— 用于 Flutter 与原生层通信(拍照、录像等控制指令)
// class MainActivity : FlutterActivity() {
val
channel
=
MethodChannel
(
flutterEngine
.
dartExecutor
.
binaryMessenger
,
USB_CAMERA_CHANNEL
)
usbCameraPlugin
=
UsbCameraPlugin
(
this
,
channel
)
}
override
fun
onDestroy
()
{
// companion object {
usbCameraPlugin
?.
dispose
()
// const val USB_CAMERA_CHANNEL = "usb_camera"
super
.
onDestroy
()
// const val USB_CAMERA_VIEW_TYPE = "usb_camera_view"
}
// }
}
// private var usbCameraPlugin: UsbCameraPlugin? = null
// override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
// super.configureFlutterEngine(flutterEngine)
// // 注册 PlatformViewFactory —— 用于在 Flutter 中渲染摄像头预览画面
// flutterEngine
// .platformViewsController
// .registry
// .registerViewFactory(USB_CAMERA_VIEW_TYPE, UsbCameraViewFactory())
// // 注册 MethodChannel —— 用于 Flutter 与原生层通信(拍照、录像等控制指令)
// val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, USB_CAMERA_CHANNEL)
// usbCameraPlugin = UsbCameraPlugin(this, channel)
// }
// override fun onDestroy() {
// usbCameraPlugin?.dispose()
// super.onDestroy()
// }
// }
android/app/src/main/kotlin/com/qialg/laki_icu_app/camera/UsbCameraManager.kt
deleted
100644 → 0
View file @
888c9417
package
com.example.laki_icu_app.camera
import
android.content.Context
import
android.hardware.usb.UsbDevice
import
android.view.TextureView
import
android.view.View
import
com.jiangdg.ausbc.MultiCameraClient
import
com.jiangdg.ausbc.callback.ICameraStateCallBack
import
com.jiangdg.ausbc.callback.IPreviewDataCallBack
import
com.jiangdg.ausbc.camera.bean.CameraRequest
import
com.jiangdg.ausbc.camera.bean.PreviewSize
import
io.flutter.plugin.common.MethodChannel
import
java.io.File
/**
* USB 摄像头管理器(单例)
*
* 职责:
* - 封装 AndroidUSBCamera (libausbc) 的调用
* - 被 UsbCameraPlugin(MethodChannel 控制)和 UsbCameraView(预览画面)共享
*
* 注意:MultiCameraClient 支持多摄像头管理,当前实现先支持第一个 USB 摄像头
*/
object
UsbCameraManager
{
private
var
cameraClient
:
MultiCameraClient
?
=
null
private
var
textureView
:
TextureView
?
=
null
private
var
isOpened
=
false
private
var
currentDevice
:
UsbDevice
?
=
null
/** 获取已连接的 USB 摄像头设备列表 */
fun
getDeviceList
(
context
:
Context
):
List
<
Map
<
String
,
Any
?>>
{
val
client
=
MultiCameraClient
(
context
)
val
list
=
client
.
getDeviceList
()
client
.
release
()
return
list
.
map
{
device
->
mapOf
<
String
,
Any
?>(
"deviceId"
to
device
.
deviceId
,
"deviceName"
to
device
.
deviceName
,
"vendorId"
to
device
.
vendorId
,
"productId"
to
device
.
productId
,
)
}
}
/** 打开指定 USB 摄像头 */
fun
openCamera
(
context
:
Context
,
textureView
:
TextureView
,
deviceId
:
Int
?
=
null
,
):
Boolean
{
if
(
isOpened
)
{
closeCamera
()
}
return
try
{
val
client
=
MultiCameraClient
(
context
).
apply
{
// 设置状态回调
setCameraStateCallBack
(
object
:
ICameraStateCallBack
{
override
fun
onCameraOpen
()
{
isOpened
=
true
}
override
fun
onCameraClose
()
{
isOpened
=
false
}
override
fun
onCameraError
(
message
:
String
?)
{
isOpened
=
false
}
override
fun
onCameraAttached
(
device
:
UsbDevice
?)
{
// USB 摄像头插入时触发
}
override
fun
onCameraDetached
(
device
:
UsbDevice
?)
{
// USB 摄像头拔出时触发
if
(
device
==
currentDevice
)
{
isOpened
=
false
}
}
})
}
// 打开摄像头
val
config
=
CameraRequest
.
Builder
()
.
setPreviewSize
(
PreviewSize
.
PREVIEW_SIZE_720P
)
.
create
()
client
.
initialize
(
textureView
,
config
)
client
.
startPreview
()
this
.
cameraClient
=
client
this
.
textureView
=
textureView
isOpened
=
true
true
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
false
}
}
/** 拍照 */
fun
takePhoto
(
savePath
:
String
?):
String
?
{
val
client
=
cameraClient
?:
return
null
return
try
{
val
path
=
savePath
?:
"/storage/emulated/0/Pictures/usb_camera_${System.currentTimeMillis()}.jpg"
// 确保目录存在
val
file
=
File
(
path
)
file
.
parentFile
?.
mkdirs
()
client
.
capturePicture
(
path
)
path
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
null
}
}
/** 开始录像 */
fun
startRecord
(
savePath
:
String
?):
Boolean
{
val
client
=
cameraClient
?:
return
false
return
try
{
val
path
=
savePath
?:
"/storage/emulated/0/Movies/usb_camera_${System.currentTimeMillis()}.mp4"
val
file
=
File
(
path
)
file
.
parentFile
?.
mkdirs
()
client
.
startRecording
(
path
)
true
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
false
}
}
/** 停止录像 */
fun
stopRecord
():
String
?
{
val
client
=
cameraClient
?:
return
null
return
try
{
client
.
stopRecording
()
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
null
}
}
/** 关闭摄像头 */
fun
closeCamera
()
{
try
{
cameraClient
?.
stopPreview
()
cameraClient
?.
release
()
}
catch
(
_
:
Exception
)
{
// 忽略释放时的异常
}
cameraClient
=
null
textureView
=
null
isOpened
=
false
currentDevice
=
null
}
/** 查询摄像头是否已打开 */
fun
isCameraOpened
():
Boolean
=
isOpened
/** 设置预览分辨率 */
fun
setPreviewSize
(
width
:
Int
,
height
:
Int
):
Boolean
{
// libausbc 的 preview size 在初始化时设置,运行时无法动态切换
// 此处保留接口供后续扩展
return
false
}
}
android/app/src/main/kotlin/com/qialg/laki_icu_app/camera/UsbCameraPlugin.kt
deleted
100644 → 0
View file @
888c9417
package
com.example.laki_icu_app.camera
import
android.app.Activity
import
io.flutter.plugin.common.MethodCall
import
io.flutter.plugin.common.MethodChannel
/**
* USB 摄像头 MethodChannel 处理器
*
* 职责:
* - 接收 Flutter 端通过 MethodChannel 发来的控制指令
* - 调用 UsbCameraManager 执行对应操作
* - 通过 result 将结果返回给 Flutter 端
*
* MethodChannel 名称: "usb_camera"
*/
class
UsbCameraPlugin
(
private
val
activity
:
Activity
,
private
val
channel
:
MethodChannel
,
)
:
MethodChannel
.
MethodCallHandler
{
init
{
channel
.
setMethodCallHandler
(
this
)
}
override
fun
onMethodCall
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
when
(
call
.
method
)
{
"getDeviceList"
->
{
val
list
=
UsbCameraManager
.
getDeviceList
(
activity
)
result
.
success
(
list
)
}
"openCamera"
->
{
// 此方法由 PlatformView 在创建时通过 Flutter 端调用
// 实际的 openCamera 由 UsbCameraView 在 getView() 时完成
// 这里返回 true 表示准备就绪
result
.
success
(
true
)
}
"closeCamera"
->
{
UsbCameraManager
.
closeCamera
()
result
.
success
(
true
)
}
"takePhoto"
->
{
val
path
=
call
.
argument
<
String
>(
"path"
)
val
photoPath
=
UsbCameraManager
.
takePhoto
(
path
)
if
(
photoPath
!=
null
)
{
result
.
success
(
photoPath
)
}
else
{
result
.
error
(
"TAKE_PHOTO_ERROR"
,
"拍照失败"
,
null
)
}
}
"startRecord"
->
{
val
path
=
call
.
argument
<
String
>(
"path"
)
val
success
=
UsbCameraManager
.
startRecord
(
path
)
result
.
success
(
success
)
}
"stopRecord"
->
{
val
videoPath
=
UsbCameraManager
.
stopRecord
()
if
(
videoPath
!=
null
)
{
result
.
success
(
videoPath
)
}
else
{
result
.
error
(
"STOP_RECORD_ERROR"
,
"停止录像失败"
,
null
)
}
}
"isCameraOpened"
->
{
result
.
success
(
UsbCameraManager
.
isCameraOpened
())
}
"setPreviewSize"
->
{
val
width
=
call
.
argument
<
Int
>(
"width"
)
?:
1280
val
height
=
call
.
argument
<
Int
>(
"height"
)
?:
720
val
success
=
UsbCameraManager
.
setPreviewSize
(
width
,
height
)
result
.
success
(
success
)
}
else
->
result
.
notImplemented
()
}
}
fun
dispose
()
{
UsbCameraManager
.
closeCamera
()
}
}
android/app/src/main/kotlin/com/qialg/laki_icu_app/camera/UsbCameraView.kt
deleted
100644 → 0
View file @
888c9417
package
com.example.laki_icu_app.camera
import
android.annotation.SuppressLint
import
android.content.Context
import
android.graphics.SurfaceTexture
import
android.view.TextureView
import
android.view.View
import
io.flutter.plugin.platform.PlatformView
/**
* USB 摄像头预览 PlatformView
*
* 职责:
* - 内嵌 AndroidUSBCamera 的 TextureView 渲染摄像头预览画面
* - 实现 PlatformView 接口以嵌入 Flutter Widget 树
*
* viewType 注册名称: "usb_camera_view"
*/
class
UsbCameraView
(
private
val
context
:
Context
,
viewId
:
Int
,
)
:
PlatformView
{
/** 摄像头预览 TextureView */
private
val
textureView
:
TextureView
=
TextureView
(
context
).
apply
{
// 当 TextureView 的 SurfaceTexture 就绪后,打开摄像头并开始预览
surfaceTextureListener
=
object
:
TextureView
.
SurfaceTextureListener
{
override
fun
onSurfaceTextureAvailable
(
surface
:
SurfaceTexture
,
width
:
Int
,
height
:
Int
,
)
{
// Surface 就绪后打开 USB 摄像头
UsbCameraManager
.
openCamera
(
context
,
this
@apply
)
}
override
fun
onSurfaceTextureSizeChanged
(
surface
:
SurfaceTexture
,
width
:
Int
,
height
:
Int
,
)
{
// 尺寸变化时无需额外处理
}
override
fun
onSurfaceTextureDestroyed
(
surface
:
SurfaceTexture
):
Boolean
{
// 当 Surface 销毁时不要关闭摄像头,因为可能是暂时性的
return
true
}
override
fun
onSurfaceTextureUpdated
(
surface
:
SurfaceTexture
)
{
// 每帧更新时回调
}
}
}
override
fun
getView
():
View
=
textureView
override
fun
dispose
()
{
// PlatformView 销毁时释放摄像头资源
UsbCameraManager
.
closeCamera
()
}
}
android/app/src/main/kotlin/com/qialg/laki_icu_app/camera/UsbCameraViewFactory.kt
deleted
100644 → 0
View file @
888c9417
package
com.example.laki_icu_app.camera
import
android.content.Context
import
io.flutter.plugin.common.StandardMessageCodec
import
io.flutter.plugin.platform.PlatformView
import
io.flutter.plugin.platform.PlatformViewFactory
/**
* USB 摄像头 PlatformView 工厂
*
* 职责:
* - 告诉 Flutter 如何创建 UsbCameraView 实例
* - 在 MainActivity.configureFlutterEngine() 中注册
*
* 注册名称: "usb_camera_view"
*/
class
UsbCameraViewFactory
:
PlatformViewFactory
(
StandardMessageCodec
.
INSTANCE
)
{
override
fun
create
(
context
:
Context
,
viewId
:
Int
,
args
:
Any
?):
PlatformView
{
return
UsbCameraView
(
context
,
viewId
)
}
}
pubspec.yaml
View file @
8983fa7f
...
@@ -50,6 +50,8 @@ dependencies:
...
@@ -50,6 +50,8 @@ dependencies:
logger
:
^2.7.0
logger
:
^2.7.0
fluttertoast
:
^9.0.0
fluttertoast
:
^9.0.0
permission_handler
:
^11.3.1
permission_handler
:
^11.3.1
flutter_usbcamera
:
^0.0.1
# flutter_usbcamera: ^0.0.1
# fl_chart: ^0.71.0
# fl_chart: ^0.71.0
# fl_chart: ^1.1.0
# fl_chart: ^1.1.0
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment