From ec9558f51cf586e96fa11510552f9665a88449a1 Mon Sep 17 00:00:00 2001 From: x1ulan Date: Fri, 2 Jan 2026 15:27:57 +0800 Subject: [PATCH] v0.0.1 --- bitmap_service/app.py | 42 ++++++++++++------- bitmap_service/dockerfile | 2 + bitmap_service/run.sh | 5 +++ docker-compose.yml | 10 ++--- .../app/routes/components/canvas.tsx | 7 ++-- 5 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 bitmap_service/run.sh diff --git a/bitmap_service/app.py b/bitmap_service/app.py index 0730b62..2a9f2dd 100644 --- a/bitmap_service/app.py +++ b/bitmap_service/app.py @@ -1,6 +1,6 @@ from flask import Flask, request, jsonify from flask_cors import CORS -from PIL import Image +from PIL import Image import requests import io @@ -27,21 +27,35 @@ def bitmap(): try: image_stream = io.BytesIO(file.read()) + img = Image.open(image_stream) - grayscale_img = img.convert('L') - bitmap_data = list(grayscale_img.getdata()) - - payload = 'P5\n296 152\n255\n' - payload += ''.join([chr(i) for i in bitmap_data]) - print(payload) - req = requests.put('http://localhost:8080/api/tag', data=payload, headers={ - 'Content-Type': 'image/x-portable-greymap' - }) - return jsonify({'payload': payload, 'code': req.status_code}) - # return jsonify({'code': req.status_code}) + img.save('test.png') + print(img.tobytes()) + # 1. 確保尺寸符合你的硬體需求 (296x152) + # 如果你的圖片不是這個尺寸,建議先 resize + img = img.resize((296, 152)).convert('1').transpose(Image.Transpose.ROTATE_90).transpose(Image.Transpose.FLIP_LEFT_RIGHT) + + # 2. 直接獲取二進制數據 (Bit-packed) + # Pillow '1' 模式的 tobytes() 會將 8 個像素縮成 1 個 byte + raw_bits = img.tobytes() + + # 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: - return jsonify({"error": e}), 500 + return jsonify({"error": str(e)}), 500 if __name__ == '__main__': - app.run('0.0.0.0', port=80) + app.run('0.0.0.0', port=80, debug=1) diff --git a/bitmap_service/dockerfile b/bitmap_service/dockerfile index 41af328..93c5f8c 100644 --- a/bitmap_service/dockerfile +++ b/bitmap_service/dockerfile @@ -6,4 +6,6 @@ COPY . . RUN pip3 install pillow flask flask-cors requests +# RUN apt update && apt install nano + CMD ["python3", "app.py"] \ No newline at end of file diff --git a/bitmap_service/run.sh b/bitmap_service/run.sh new file mode 100644 index 0000000..911667e --- /dev/null +++ b/bitmap_service/run.sh @@ -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" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8ec569e..6f91ea9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,14 +9,10 @@ services: container_name: backend_service ports: - "12001:80" - # bitmap_service: - # build: ./bitmap_service - # container_name: bitmap_service - # ports: - # - "12002:80" + bitmap_service: - build: ./trash - container_name: trash + build: ./bitmap_service + container_name: bitmap_service ports: - "12002:80" extra_hosts: diff --git a/frontend_service/app/routes/components/canvas.tsx b/frontend_service/app/routes/components/canvas.tsx index 53b1597..75a0784 100644 --- a/frontend_service/app/routes/components/canvas.tsx +++ b/frontend_service/app/routes/components/canvas.tsx @@ -15,13 +15,14 @@ const Canva = forwardRef((props, ref) => { canvas.width = 296 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.textAlign = "center" - context.fillStyle = "#374151" + context.fillStyle = "#000000" context.fillText(props.name, canvas.width/2, 50) context.font = "48px 'Inter', Arial"; - context.fillStyle = "#111827"; + context.fillStyle = "#000000"; context.fillText(`$${props.price}`, canvas.width / 2, 110); }, [props.name, props.price, ref]);