brew中文本的自动换行

要实现文本的自动换行,在brew平台下,一般使用IDisplay_MeasureTextEx()接口。对于文本比较短的时候,可以使用该接口,但是,当文本比较长的时候,该接口的效率会比较低。

解决的办法一般有两个:

1、以空间换时间,复制当前屏幕要显示的内容到一个临时buffer中,然后显示。由于当前屏幕要显示的内容一般不会超过300字,所以完全可以满足需要。

2、实现计算好。计算的时候,可以使用IDisplay_MeasureText()接口,以提高效率,但是由于IDisplay_MeasureText()接口没有指定文本长度,所以也需要把数据复制到一个临时的buffer中,然后再进行计算。

帮我写进我的代码# -*- coding: utf-8 -*- import sys import io from flask import Flask, request, jsonify from flask_cors import CORS from rapidocr_onnxruntime import RapidOCR import tempfile import os # 强制系统使用UTF-8编码 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8') sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8') app = Flask(__name__) CORS(app) # 关键配置:禁用JSON的ASCII转码 app.config['JSON_AS_ASCII'] = False # Flask<2.3版本 app.json.ensure_ascii = False # Flask>=2.3版本 engine = RapidOCR() ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'bmp'} def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/ocr', methods=['POST']) def ocr_processing(): if 'file' not in request.files: return jsonify({'error': '未上传文件'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': '未选择文件'}), 400 if not allowed_file(file.filename): return jsonify({'error': '不支持的文件格式'}), 400 try: with tempfile.NamedTemporaryFile(delete=False) as temp_file: file.save(temp_file.name) result, elapse = engine(temp_file.name) os.unlink(temp_file.name) # 确保中文文本正确格式化 formatted_result = [{ 'text': item[1], # RapidOCR默认返回UTF-8 'confidence': float(item[2]), 'coordinates': item[0] } for item in result] # 创建响应并显式设置UTF-8头部 response = jsonify({ 'result': formatted_result, 'processing_time': elapse }) response.headers['Content-Type'] = 'application/json; charset=utf-8' return response except Exception as e: error_msg = str(e).encode('utf-8').decode('utf-8') return jsonify({'error': error_msg}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值