# 海康工业相机HTTP API服务 通过FastAPI提供HTTP接口控制海康工业相机拍照 ## 功能特性 - ✅ 支持多相机管理 - ✅ 通过IP地址、序列号、索引选择相机 - ✅ RESTful API接口 - ✅ 自动图像格式转换(BMP/JPG) - ✅ 图片下载接口 - ✅ 完整的错误处理 ## 安装依赖 ```bash pip install -r requirements.txt ``` ## 启动服务 ```bash python camera_api.py ``` 服务将在 `http://localhost:8000` 启动 ## API文档 启动服务后访问: - Swagger UI: http://localhost:8000/docs - ReDoc: http://localhost:8000/redoc ## API接口 ### 1. 列出所有相机 **GET** `/cameras` 响应示例: ```json { "success": true, "count": 2, "cameras": [ { "index": 0, "type": "GigE", "model": "MV-CS200-10GC", "serial": "00J12345678", "ip": "192.168.1.100", "user_defined_name": "Camera1" } ] } ``` ### 2. 拍照(通过IP地址) **GET** `/capture?ip=192.168.1.100&filename=test_image` **POST** `/capture` ```json { "ip": "192.168.1.100", "filename": "test_image", "save_bmp": true, "save_jpg": true, "timeout": 3000 } ``` 响应示例: ```json { "success": true, "message": "拍照成功", "files": ["test_image.bmp", "test_image.jpg"], "camera_info": { "type": "GigE", "model": "MV-CS200-10GC", "serial": "00J12345678", "ip": "192.168.1.100" } } ``` ### 3. 拍照(通过序列号) **GET** `/capture_by_serial?serial=00J12345678&filename=test_image` ### 4. 拍照(通过索引) **GET** `/capture_by_index?index=0&filename=test_image` ### 5. 下载图片 **GET** `/image/test_image.jpg` ### 6. 健康检查 **GET** `/health` ## 使用示例 ### Python客户端 ```python import requests # 列出所有相机 response = requests.get("http://localhost:8000/cameras") cameras = response.json()["cameras"] # 通过IP地址拍照 response = requests.get( "http://localhost:8000/capture", params={ "ip": "192.168.1.100", "filename": "my_image" } ) result = response.json() print(f"保存的文件: {result['files']}") # 下载图片 response = requests.get("http://localhost:8000/image/my_image.jpg") with open("downloaded.jpg", "wb") as f: f.write(response.content) ``` ### cURL ```bash # 列出相机 curl http://localhost:8000/cameras # 拍照(GET) curl "http://localhost:8000/capture?ip=192.168.1.100&filename=test_image" # 拍照(POST) curl -X POST http://localhost:8000/capture \ -H "Content-Type: application/json" \ -d '{"ip":"192.168.1.100","filename":"test_image"}' # 下载图片 curl -O http://localhost:8000/image/test_image.jpg ``` ### JavaScript/Fetch ```javascript // 列出相机 fetch('http://localhost:8000/cameras') .then(res => res.json()) .then(data => console.log(data)); // 拍照 fetch('http://localhost:8000/capture?ip=192.168.1.100&filename=test_image') .then(res => res.json()) .then(data => console.log(data)); // 下载图片 fetch('http://localhost:8000/image/test_image.jpg') .then(res => res.blob()) .then(blob => { const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = 'test_image.jpg'; a.click(); }); ``` ## 测试 运行测试客户端: ```bash python test_api.py ``` ## 文件说明 - `camera_api.py` - FastAPI服务主文件 - `camera_manager.py` - 相机管理类 - `test_api.py` - API测试客户端 - `test.py` - 基础测试脚本 - `example_multi_camera.py` - 多相机使用示例 - `requirements.txt` - Python依赖 ## 注意事项 1. 确保相机已正确连接并配置好网络 2. 关闭海康MVS客户端软件,避免设备占用 3. 图片默认保存在当前目录 4. 建议在生产环境中添加认证和权限控制 5. 可以通过环境变量配置服务端口和保存路径 ## 故障排除 ### 错误:设备无访问权限 (0x80000203) - 关闭海康MVS客户端 - 确保没有其他程序占用相机 ### 错误:获取图像失败 (0x80000007) - 检查相机镜头盖是否打开 - 确保有足够光线 - 增加timeout参数值 ### 错误:无法连接到API服务 - 确保服务已启动:`python camera_api.py` - 检查端口8000是否被占用