30 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			30 lines
		
	
	
		
			1010 B
		
	
	
	
		
			Python
		
	
	
	
	
	
| import io
 | |
| import base64
 | |
| from PIL import Image
 | |
| from picamera2 import Picamera2
 | |
| import sys
 | |
| 
 | |
| # 原代码6. 摄像头模块完整逻辑
 | |
| class CameraModule:
 | |
|     def __init__(self):
 | |
|         try:
 | |
|             self.camera = Picamera2()
 | |
|             cam_config = self.camera.create_still_configuration(main={"size": (320, 240)})
 | |
|             self.camera.configure(cam_config)
 | |
|             self.camera.start()
 | |
|             print("📷 摄像头模块初始化成功")
 | |
|         except Exception as e:
 | |
|             print(f"❌ 摄像头失败:{str(e)}")
 | |
|             self.camera = None
 | |
| 
 | |
|     def capture_base64(self):
 | |
|         if not self.camera:
 | |
|             return None
 | |
|         try:
 | |
|             img_array = self.camera.capture_array()
 | |
|             img_byte = io.BytesIO()
 | |
|             Image.fromarray(img_array).save(img_byte, format="JPEG", quality=80)
 | |
|             return base64.b64encode(img_byte.getvalue()).decode("utf-8")
 | |
|         except Exception as e:
 | |
|             print(f"❌ 拍摄失败:{str(e)}")
 | |
|             return None |