You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Flutter/Dart拍摄的图片传给Python做OCR并旋转90度

实现方案:Flutter图片传输至Python OCR + 图片旋转处理

一、Flutter端操作:拍照、旋转、上传

1. 安装依赖

在pubspec.yaml中添加所需包:

dependencies:
  flutter:
    sdk: flutter
  image_picker: ^1.0.4
  image: ^4.0.17
  http: ^1.1.0

执行flutter pub get完成依赖安装。

2. 拍照并旋转90度

以下代码实现相机拍照+顺时针90度旋转功能,返回处理后的图片文件:

import 'dart:io';
import 'package:image/image.dart' as img;
import 'package:image_picker/image_picker.dart';

Future<File?> captureAndRotateImage() async {
  // 调用系统相机拍照
  final picker = ImagePicker();
  final XFile? capturedPhoto = await picker.pickImage(source: ImageSource.camera);
  
  if (capturedPhoto == null) return null;

  // 读取图片并执行旋转
  final imageData = await File(capturedPhoto.path).readAsBytes();
  img.Image? originalImg = img.decodeImage(imageData);
  if (originalImg == null) return null;
  
  // 顺时针转90度,逆时针转传270即可
  img.Image rotatedImg = img.copyRotate(originalImg, 90);
  
  // 将旋转后的图片保存为临时文件
  final rotatedFile = File('${capturedPhoto.path}_rotated.jpg');
  await rotatedFile.writeAsBytes(img.encodeJpg(rotatedImg));
  
  return rotatedFile;
}

3. 上传图片到Python服务

用multipart/form-data格式发送图片,获取Python端返回的OCR结果:

import 'package:http/http.dart' as http;
import 'dart:io';

Future<String?> uploadImage(File imageFile) async {
  // 替换为你的Python服务实际地址
  var apiUrl = Uri.parse('http://192.168.1.100:8000/upload');
  var request = http.MultipartRequest('POST', apiUrl);
  
  // 添加图片文件到请求体
  request.files.add(await http.MultipartFile.fromPath(
    'image',
    imageFile.path,
    contentType: http.MediaType('image', 'jpeg'),
  ));
  
  // 发送请求并处理响应
  var response = await request.send();
  if (response.statusCode == 200) {
    return await response.stream.bytesToString();
  } else {
    return null;
  }
}

二、Python端操作:接收图片、OCR识别

1. 安装依赖

执行命令安装所需库:

pip install fastapi uvicorn pytesseract pillow

注意:需提前安装Tesseract OCR引擎——Windows去官网下载安装包,Linux执行apt install tesseract-ocr;需识别中文的话,额外安装中文语言包。

2. 编写Web服务处理上传与OCR

创建main.py,代码如下:

from fastapi import FastAPI, File, UploadFile
from PIL import Image
import pytesseract
import io

app = FastAPI()

# 若Tesseract未加入系统环境变量,手动指定路径
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'

@app.post("/upload")
async def handle_upload(image: UploadFile = File(...)):
    # 读取上传的图片数据
    img_bytes = await image.read()
    image_obj = Image.open(io.BytesIO(img_bytes))
    
    # 执行OCR,lang参数指定语言(chi_sim为中文,eng为英文)
    ocr_text = pytesseract.image_to_string(image_obj, lang='chi_sim')
    
    return {"ocr_result": ocr_text.strip()}

3. 启动服务

执行命令启动Web服务:

uvicorn main:app --host 0.0.0.0 --port 8000

三、关键注意事项

  • 确保Flutter设备与Python服务处于同一局域网,或Python服务已部署至公网,替换对应IP/域名。
  • 图片旋转角度可按需调整,copyRotate支持任意角度值。
  • OCR语言包需与指定的lang参数匹配,否则无法正常识别对应语言文字。

内容的提问来源于stack exchange,提问作者Mohamad Bdeir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 23:06:24