59 lines
2.5 KiB
Python
59 lines
2.5 KiB
Python
# -*- coding: utf-8 -*-
|
|
import base64
|
|
|
|
from odoo import models, fields, api, exceptions, _, tools
|
|
|
|
|
|
class HrEmployee(models.Model):
|
|
_inherit = "hr.employee"
|
|
_description = "考勤机和用户信息的绑定"
|
|
|
|
attendance_iot_ids = fields.One2many('attendance.iot.user', 'employee_id', string="考勤机信息")
|
|
# 图片需要压缩
|
|
# 1、下发的人脸图片压缩成 jpg 格式,大小不超过 300 kb ( 尺寸不超过 1280 * 720 ) ,我们正常是 50 kb 左右
|
|
# 2、把压缩后的图片转换成完整的 Base64 格式(必须加上前缀 “data:image/jpeg;base64,”)
|
|
# 3、把完整的 Base64 图片使用 UrlEncode 方法进行 UTF-8 格式的编码。
|
|
|
|
avatar_image = fields.Image(string='头像')
|
|
|
|
compress_image = fields.Image(string='压缩后的图片', compute='_compute_compress_image')
|
|
|
|
@api.depends('avatar_image')
|
|
def _compute_compress_image(self):
|
|
for record in self:
|
|
if record.avatar_image:
|
|
|
|
origin_300 = 400 * 1024
|
|
|
|
if len(record.avatar_image) < origin_300:
|
|
record.compress_image = record.avatar_image
|
|
else:
|
|
|
|
image = base64.b64decode(record.avatar_image)
|
|
|
|
compress_image = tools.image_process(image, size=(1280, 720),
|
|
output_format='JPEG', verify_resolution=True)
|
|
if len(compress_image) <= origin_300:
|
|
record.compress_image = base64.b64encode(compress_image)
|
|
else:
|
|
quality = 95
|
|
image_value = tools.image_process(image, size=(1280, 720), output_format='JPEG',
|
|
quality=quality, verify_resolution=True)
|
|
|
|
while quality > 10 and len(image_value) > origin_300:
|
|
image_value = tools.image_process(base64.b64decode(image), size=(1280, 720), output_format='JPEG',
|
|
quality=quality, verify_resolution=True)
|
|
quality -= 5
|
|
record.compress_image = base64.b64encode(image_value)
|
|
else:
|
|
record.compress_image = False
|
|
|
|
def write(self, values):
|
|
res = super(HrEmployee,self).write(values)
|
|
# 更新数据
|
|
if 'name' in values or 'avatar_image' in values:
|
|
# 同步信息到设备上
|
|
self.attendance_iot_ids.action_update_iot_info()
|
|
|
|
return res
|