This commit is contained in:
x1ulan 2026-01-02 15:27:57 +08:00
parent f2cf016fc5
commit ec9558f51c
5 changed files with 42 additions and 24 deletions

View file

@ -1,6 +1,6 @@
from flask import Flask, request, jsonify from flask import Flask, request, jsonify
from flask_cors import CORS from flask_cors import CORS
from PIL import Image from PIL import Image
import requests import requests
import io import io
@ -27,21 +27,35 @@ def bitmap():
try: try:
image_stream = io.BytesIO(file.read()) image_stream = io.BytesIO(file.read())
img = Image.open(image_stream) img = Image.open(image_stream)
grayscale_img = img.convert('L') img.save('test.png')
bitmap_data = list(grayscale_img.getdata()) print(img.tobytes())
# 1. 確保尺寸符合你的硬體需求 (296x152)
payload = 'P5\n296 152\n255\n' # 如果你的圖片不是這個尺寸,建議先 resize
payload += ''.join([chr(i) for i in bitmap_data]) img = img.resize((296, 152)).convert('1').transpose(Image.Transpose.ROTATE_90).transpose(Image.Transpose.FLIP_LEFT_RIGHT)
print(payload)
req = requests.put('http://localhost:8080/api/tag', data=payload, headers={ # 2. 直接獲取二進制數據 (Bit-packed)
'Content-Type': 'image/x-portable-greymap' # Pillow '1' 模式的 tobytes() 會將 8 個像素縮成 1 個 byte
}) raw_bits = img.tobytes()
return jsonify({'payload': payload, 'code': req.status_code})
# return jsonify({'code': req.status_code}) # 3. 組合 PBM P4 標頭與數據
header = f'P4\n152 296\n'.encode('ascii')
payload = header + raw_bits
print(raw_bits)
# 測試寫入檔案 (必須用 'wb' 模式)
with open('test.pbm', mode='wb') as f:
f.write(payload)
# 4. 發送請求
req = requests.put('http://172.16.0.130/api/tag',
data=payload,
headers={'Content-Type': 'image/x-portable-bitmap'})
return jsonify({'message': 'success', 'bytes_sent': len(payload)})
except Exception as e: except Exception as e:
return jsonify({"error": e}), 500 return jsonify({"error": str(e)}), 500
if __name__ == '__main__': if __name__ == '__main__':
app.run('0.0.0.0', port=80) app.run('0.0.0.0', port=80, debug=1)

View file

@ -6,4 +6,6 @@ COPY . .
RUN pip3 install pillow flask flask-cors requests RUN pip3 install pillow flask flask-cors requests
# RUN apt update && apt install nano
CMD ["python3", "app.py"] CMD ["python3", "app.py"]

5
bitmap_service/run.sh Normal file
View file

@ -0,0 +1,5 @@
#remote="127.0.0.1:8080"
remote="172.16.0.131"
#curl -v -T./logo-rotate.pbm "http://${remote}/api/tag" -H "Content-Type: image/x-portable-bitmap"
curl -v -T./logo.pbm "http://${remote}/api/tag" -H "Content-Type: image/x-portable-bitmap"
# url -v -T./test.pgm "http://127.0.0.1:8080/api/tag" -H "Content-Type: image/x-portable-greymap"

View file

@ -9,14 +9,10 @@ services:
container_name: backend_service container_name: backend_service
ports: ports:
- "12001:80" - "12001:80"
# bitmap_service:
# build: ./bitmap_service
# container_name: bitmap_service
# ports:
# - "12002:80"
bitmap_service: bitmap_service:
build: ./trash build: ./bitmap_service
container_name: trash container_name: bitmap_service
ports: ports:
- "12002:80" - "12002:80"
extra_hosts: extra_hosts:

View file

@ -15,13 +15,14 @@ const Canva = forwardRef<HTMLCanvasElement, CanvaProps>((props, ref) => {
canvas.width = 296 canvas.width = 296
canvas.height = 152 canvas.height = 152
context.clearRect(0, 0, canvas.width, canvas.height) context.fillStyle = "#ffffff"
context.fillRect(0, 0, canvas.width, canvas.height)
context.font = "24px 'Inter', Arial" context.font = "24px 'Inter', Arial"
context.textAlign = "center" context.textAlign = "center"
context.fillStyle = "#374151" context.fillStyle = "#000000"
context.fillText(props.name, canvas.width/2, 50) context.fillText(props.name, canvas.width/2, 50)
context.font = "48px 'Inter', Arial"; context.font = "48px 'Inter', Arial";
context.fillStyle = "#111827"; context.fillStyle = "#000000";
context.fillText(`$${props.price}`, canvas.width / 2, 110); context.fillText(`$${props.price}`, canvas.width / 2, 110);
}, [props.name, props.price, ref]); }, [props.name, props.price, ref]);