12 Commits

21 changed files with 253 additions and 84 deletions

View File

@ -38,4 +38,5 @@ public interface GenConfigService extends IService<GenConfig> {
* @return 表配置
*/
GenConfig update(String tableName, GenConfig genConfig);
}

View File

@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.fuyuanshen.base.BaseEntity;
@ -20,6 +21,7 @@ import java.io.Serializable;
**/
@Data
@TableName("device")
@JsonInclude(JsonInclude.Include.ALWAYS) // 关键注解
public class Device extends BaseEntity implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
@ -106,6 +108,9 @@ public class Device extends BaseEntity implements Serializable {
@ApiModelProperty(value = "绑定状态")
private Integer bindingStatus;
@ApiModelProperty(value = "通讯方式", example = "0:4G;1:蓝牙")
private Integer communicationMode;
public void copy(Device source) {
BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true));

View File

@ -9,6 +9,7 @@ import com.baomidou.mybatisplus.annotation.TableName;
import com.fuyuanshen.base.BaseEntity;
import com.fuyuanshen.modules.system.domain.Device;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serializable;
@ -100,6 +101,8 @@ public class APPDevice extends BaseEntity implements Serializable {
@ApiModelProperty(value = "绑定类型")
private Integer bindingType;
@Schema(name = "通讯方式", example = "0:4G;1:蓝牙")
private String communicationMode;
public void copy(Device source) {
BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true));

View File

@ -17,6 +17,9 @@ public class DeviceForm {
@ApiModelProperty(value = "ID", hidden = true)
private Long id;
@ApiModelProperty(value = "设备记录ID")
private Long assignId;
@ApiModelProperty(value = "设备类型")
private Long deviceType;

View File

@ -4,7 +4,6 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

View File

@ -13,10 +13,13 @@ import javax.validation.constraints.NotNull;
@Data
public class APPUnbindDTO {
@NotBlank(message = "设备MAC不能为空")
// @NotBlank(message = "设备MAC不能为空")
@ApiModelProperty(value = "设备MAC", required = true)
private String deviceMac;
@ApiModelProperty(value = "设备IMEI")
private String deviceImei;
@NotNull(message = "客户号不能为空")
@ApiModelProperty(value = "客户号")
private Long customerId;

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fuyuanshen.modules.system.domain.app.APPDeviceType;
import com.fuyuanshen.modules.system.domain.dto.DeviceQueryCriteria;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -23,6 +24,6 @@ public interface AppDeviceTypeMapper extends BaseMapper<APPDeviceType> {
* @param criteria 查询条件
* @return 设备类型列表
*/
List<APPDeviceType> appTypeList(DeviceQueryCriteria criteria);
List<APPDeviceType> appTypeList(@Param("criteria")DeviceQueryCriteria criteria);
}

View File

@ -146,6 +146,20 @@ public class DeviceController {
}
@Log("撤回设备")
@ApiOperation("撤回设备")
@PostMapping(value = "/withdraw")
public ResponseVO<Object> withdrawDevice(@Validated @ModelAttribute DeviceForm deviceForm) {
try {
deviceService.withdrawDevice(deviceForm);
} catch (Exception e) {
log.error("updateDevice error: " + e.getMessage());
return ResponseVO.fail("出错了");
}
return ResponseVO.success(null);
}
@ApiOperation("设备详情")
@GetMapping(value = "/detail/{id}")
public ResponseVO<Object> getDevice(@PathVariable Long id) {

View File

@ -1,56 +1,20 @@
package com.fuyuanshen.modules.system.rest.app;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyuanshen.annotation.Log;
import com.fuyuanshen.exception.BadRequestException;
import com.fuyuanshen.modules.system.constant.UserConstants;
import com.fuyuanshen.modules.system.domain.Device;
import com.fuyuanshen.modules.system.domain.User;
import com.fuyuanshen.modules.system.domain.app.APPDevice;
import com.fuyuanshen.modules.system.domain.app.APPDeviceType;
import com.fuyuanshen.modules.system.domain.dto.CustomerVo;
import com.fuyuanshen.modules.system.domain.dto.DeviceExcelImportDTO;
import com.fuyuanshen.modules.system.domain.dto.DeviceForm;
import com.fuyuanshen.modules.system.domain.dto.DeviceQueryCriteria;
import com.fuyuanshen.modules.system.domain.dto.app.APPUnbindDTO;
import com.fuyuanshen.modules.system.listener.excel.DeviceImportParams;
import com.fuyuanshen.modules.system.listener.excel.UploadDeviceDataListener;
import com.fuyuanshen.modules.system.mapper.DeviceMapper;
import com.fuyuanshen.modules.system.mapper.DeviceTypeMapper;
import com.fuyuanshen.modules.system.mapper.UserMapper;
import com.fuyuanshen.modules.system.service.DeviceService;
import com.fuyuanshen.modules.system.service.UserService;
import com.fuyuanshen.modules.system.service.app.APPDeviceService;
import com.fuyuanshen.modules.system.service.impl.DeviceExportService;
import com.fuyuanshen.modules.utils.ResponseVO;
import com.fuyuanshen.modules.utils.excel.ImportResult;
import com.fuyuanshen.utils.FileUtil;
import com.fuyuanshen.utils.PageResult;
import com.fuyuanshen.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.List;
/**
@ -86,7 +50,7 @@ public class APPDeviceController {
@PostMapping(value = "/typeList")
@ApiOperation("APP用户设备类型列表")
public ResponseVO<List<APPDeviceType>> appTypeList(@RequestBody DeviceQueryCriteria criteria) {
List<APPDeviceType> typeList = appDeviceService.appTypeList(criteria);
List<APPDeviceType> typeList = appDeviceService.appTypeList(criteria);
return ResponseVO.success(typeList);
}

View File

@ -134,4 +134,5 @@ public class APPUserController {
// appUserService.sendSms(phoneNumber);
return ResponseVO.success("success");
}
}

View File

@ -73,6 +73,8 @@ public interface DeviceService extends IService<Device> {
*/
void assignCustomer(CustomerVo customerVo);
void withdrawDevice(DeviceForm deviceForm);
/**
* 多选删除
*
@ -110,5 +112,4 @@ public interface DeviceService extends IService<Device> {
*/
void unbindDevice(DeviceForm deviceForm);
}

View File

@ -22,6 +22,7 @@ import com.fuyuanshen.modules.system.mapper.app.APPDeviceMapper;
import com.fuyuanshen.modules.system.mapper.app.AppDeviceTypeMapper;
import com.fuyuanshen.utils.PageResult;
import com.fuyuanshen.utils.SecurityUtils;
import com.fuyuanshen.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -29,7 +30,6 @@ import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@ -56,10 +56,12 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
*/
@Override
public PageResult<APPDevice> appDeviceList(Page<APPDevice> page, DeviceQueryCriteria criteria) {
criteria.setCustomerId(SecurityUtils.getCurrentUserId());
IPage<APPDevice> devices = appDeviceMapper.appDeviceList(page, criteria);
return new PageResult<>(devices.getRecords(), devices.getTotal());
}
/**
* APP用户设备类型列表
*
@ -68,7 +70,8 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
*/
@Override
public List<APPDeviceType> appTypeList(DeviceQueryCriteria criteria) {
return appDeviceTypeMapper.appTypeList(criteria);
criteria.setCustomerId(SecurityUtils.getCurrentUserId());
return appDeviceTypeMapper.appTypeList(criteria);
}
@ -83,6 +86,7 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
List<Device> devices = new ArrayList<>();
if (criteria.getCommunicationMode().equals(CommunicationModeEnum.BLUETOOTH.getValue())) {
devices = deviceMapper.selectList(new QueryWrapper<Device>().eq("device_mac", criteria.getDeviceMac()));
if (CollectionUtil.isEmpty(devices)) {
@ -106,8 +110,12 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
throw new BadRequestException("该设备已绑定!!!");
}
}
Device device = devices.get(0);
device.setCommunicationMode(criteria.getCommunicationMode());
device.setBindingStatus(BindingStatusEnum.BOUND.getCode());
deviceMapper.updateById(device);
APPDevice appDevice = new APPDevice();
BeanUtil.copyProperties(device, appDevice);
appDevice.setBindingType(UserType.APP.getValue());
@ -124,6 +132,7 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
DeviceType deviceType = deviceTypeMapper.selectById(device.getDeviceType());
APPDeviceType type = new APPDeviceType();
BeanUtil.copyProperties(deviceType, type);
type.setCustomerId(currentUserId);
appDeviceTypeMapper.insert(type);
}
@ -152,18 +161,34 @@ public class APPDeviceServiceImpl extends ServiceImpl<APPDeviceMapper, APPDevice
@Override
@Transactional
public void unbindAPPDevice(APPUnbindDTO deviceForm) {
QueryWrapper<APPDevice> queryWrapper = new QueryWrapper<>();
QueryWrapper<Device> qw = new QueryWrapper<>();
if (StringUtils.isNotEmpty(deviceForm.getDeviceMac())) {
queryWrapper.eq("device_mac", deviceForm.getDeviceMac());
qw.eq("device_mac", deviceForm.getDeviceMac());
}
if (StringUtils.isNotEmpty(deviceForm.getDeviceImei())) {
queryWrapper.eq("device_imei", deviceForm.getDeviceImei());
qw.eq("device_imei", deviceForm.getDeviceImei());
}
queryWrapper.eq("binding_type", UserType.APP.getValue());
APPDevice appDevice = appDeviceMapper.selectOne(queryWrapper);
if (appDevice == null) {
throw new BadRequestException("设备不存在!!!");
}
appDeviceMapper.delete(queryWrapper);
appDeviceMapper.delete(new QueryWrapper<APPDevice>().eq("device_mac", deviceForm.getDeviceMac()).eq("binding_type", UserType.APP.getValue()));
List<Device> devices = deviceMapper.selectList(new QueryWrapper<Device>().eq("device_mac", deviceForm.getDeviceMac()));
List<Device> devices = deviceMapper.selectList(qw);
List<Long> ids = devices.stream()
.map(Device::getId)
.collect(Collectors.toList());
appDeviceTypeMapper.deleteBatchIds(ids);
if (CollectionUtil.isNotEmpty(ids)) {
appDeviceTypeMapper.deleteBatchIds(ids);
}
Device device = new Device();
device.setId(deviceForm.getCustomerId());
device.setId(appDevice.getId());
device.setBindingStatus(BindingStatusEnum.UNBOUND.getCode());
deviceMapper.updateById(device);
}
}

View File

@ -456,6 +456,30 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
}
/**
* 撤回设备
*
* @param deviceForm
*/
@Override
public void withdrawDevice(DeviceForm deviceForm) {
DeviceAssignments assignment = deviceAssignmentsMapper.selectById(deviceForm.getAssignId());
// 接收者
assignment.setAssigneeName("");
deviceAssignmentsMapper.updateById(assignment);
LambdaQueryWrapper<DeviceAssignments> q1 = new LambdaQueryWrapper<>();
q1.eq(DeviceAssignments::getAssignerId, assignment.getAssigneeId())
.like(DeviceAssignments::getLever, assignment.getLever())
.ne(DeviceAssignments::getId, assignment.getId());
DeviceAssignments d1 = new DeviceAssignments();
d1.setActive(DeviceActiveStatusEnum.INACTIVE.getCode());
deviceAssignmentsMapper.update(d1, q1);
}
/**
* 创建并保存设备类型授权记录
*
@ -551,6 +575,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
* @param ids
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAssign(List<Long> ids) {
// Step 1: 查询所有传入的设备(根据 ID
List<DeviceAssignments> deviceAssignments = deviceAssignmentsMapper.selectBatchIds(ids);
@ -562,7 +587,8 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
throw new BadRequestException("已分配的设备不允许删除!!!");
}
deviceAssignmentsMapper.deleteBatchIds(ids);
deviceAssignmentsMapper.deleteBatchIds(nonNullCustomerIds);
deviceTypeGrantsMapper.delete(new QueryWrapper<DeviceTypeGrants>().in("assignment_id", nonNullCustomerIds));
}
@ -625,7 +651,15 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
@Override
@Transactional
public void unbindDevice(DeviceForm deviceForm) {
appDeviceMapper.delete(new QueryWrapper<APPDevice>().eq("device_mac", deviceForm.getDeviceMac()));
QueryWrapper<APPDevice> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotEmpty(deviceForm.getDeviceMac())) {
queryWrapper.eq("device_mac", deviceForm.getDeviceMac());
}
if (StringUtils.isNotEmpty(deviceForm.getDeviceImei())) {
queryWrapper.eq("device_imei", deviceForm.getDeviceImei());
}
appDeviceMapper.delete(queryWrapper);
Device device = new Device();
device.setId(deviceForm.getId());
device.setBindingStatus(BindingStatusEnum.UNBOUND.getCode());

View File

@ -1,9 +1,13 @@
package com.fuyuanshen.modules.system.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyuanshen.exception.BadRequestException;
import com.fuyuanshen.modules.system.domain.Device;
import com.fuyuanshen.modules.system.domain.DeviceTypeGrants;
import com.fuyuanshen.modules.system.mapper.DeviceMapper;
import com.fuyuanshen.modules.system.mapper.DeviceTypeGrantsMapper;
import com.fuyuanshen.modules.utils.NanoId;
import com.fuyuanshen.utils.enums.NanoIdLengthEnum;
@ -41,6 +45,7 @@ import java.util.stream.Collectors;
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements DeviceTypeService {
private final DeviceTypeMapper deviceTypeMapper;
private final DeviceMapper deviceMapper;
private final UserMapper userMapper;
private final UserCacheManager userCacheManager;
private final DeviceTypeGrantsMapper deviceTypeGrantsMapper;
@ -142,9 +147,20 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
}
/**
* 修改设备类型
*
* @param resources /
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void update(DeviceType resources) {
List<Device> deviceList = deviceMapper.selectList(new QueryWrapper<Device>().eq("device_type", resources.getId()));
if (CollectionUtil.isNotEmpty(deviceList)) {
throw new BadRequestException("该设备类型下已有设备,请先解绑设备!!!");
}
DeviceType deviceType = getById(resources.getId());
deviceType.copy(resources);
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
@ -153,10 +169,32 @@ public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceT
}
/**
* 删除设备类型
*
* @param ids /
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAll(List<Long> ids) {
deviceTypeMapper.deleteBatchIds(ids);
// 查询所有与 device 关联的 deviceType IDs
List<Device> deviceList = deviceMapper.selectList(new QueryWrapper<Device>().in("device_type", ids));
// 提取与 device 关联的 deviceType IDs
List<Long> filteredIds = deviceList.stream()
.map(Device::getDeviceType)
.distinct()
.collect(Collectors.toList());
// 从原始 ids 中移除已关联 device 的 id即过滤掉能查到结果的 id
List<Long> idsToBeDeleted = ids.stream()
.filter(id -> !filteredIds.contains(id))
.collect(Collectors.toList());
if (idsToBeDeleted.isEmpty()) {
throw new BadRequestException("选中设备类型已绑定设备,请先解绑设备!!!");
}
// 删除过滤后的 id 列表
deviceTypeMapper.deleteBatchIds(idsToBeDeleted);
deviceTypeGrantsMapper.delete(new QueryWrapper<DeviceTypeGrants>().in("device_type_id", idsToBeDeleted));
}

View File

@ -149,11 +149,12 @@ file:
logging:
level:
com.fuyuanshen: debug
# MQTT配置
mqtt:
username: admin
password: fys123456
url: tcp://127.0.0.1:1883
url: tcp://47.107.152.87:1883
subClientId: wuLang_subClient_01
subTopic: worker/alert/#,worker/location/#
pubTopic: worker/location

View File

@ -155,3 +155,14 @@ file:
pic: /home/eladmin/app_avatar/ #设备图片存储路径
#ip: http://fuyuanshen.com:81/ #服务器地址
ip: https://fuyuanshen.com/ #服务器地址
# MQTT配置
mqtt:
username: admin
password: fys123456
url: tcp://47.107.152.87:1883
subClientId: wuLang_subClient_01
subTopic: worker/alert/#,worker/location/#
pubTopic: worker/location
pubClientId: wuLang_pubClient_01

View File

@ -34,7 +34,7 @@ spring:
check-template-location: false
profiles:
# 激活的环境,如果需要 quartz 分布式支持,需要修改 active: dev,quartz
active: dev
active: prod
data:
redis:
repositories:
@ -44,7 +44,7 @@ spring:
max-file-size: 5MB # 设置单个上传文件的最大大小为10MB
max-request-size: 5MB
jackson:
default-property-inclusion: non_null
default-property-inclusion: always
# pid:
# file: /自行指定位置/eladmin.pid

View File

@ -21,7 +21,7 @@
<!-- APP用户设备列表 -->
<select id="appDeviceList" resultType="com.fuyuanshen.modules.system.domain.app.APPDevice">
select d.* from app_device as d
select d.* ,d.app_device_id AS id from app_device as d
<where>
<!-- 时间范围等其他条件保持原样 -->
<if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
@ -87,5 +87,4 @@
order by d.create_time desc
</select>
</mapper>

View File

@ -19,7 +19,8 @@
</resultMap>
<sql id="Base_Column_List">
id,type_name,is_support_ble,locate_mode,network_way,create_by,
id
,type_name,is_support_ble,locate_mode,network_way,create_by,
update_by,create_time,update_time,customer_id,communication_mode
</sql>
@ -27,31 +28,6 @@
<select id="appTypeList" resultType="com.fuyuanshen.modules.system.domain.app.APPDeviceType">
select d.* from app_device_type as d
<where>
<!-- 时间范围等其他条件保持原样 -->
<if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
</if>
<if test="criteria.deviceMac != null and criteria.deviceMac.trim() != ''">
and d.device_mac = #{criteria.deviceMac}
</if>
<if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
and d.device_imei = #{criteria.deviceImei}
</if>
<if test="criteria.deviceSn != null">
and d.device_sn = #{criteria.deviceSn}
</if>
<if test="criteria.deviceType != null">
and d.device_type = #{criteria.deviceType}
</if>
<if test="criteria.deviceStatus != null">
and d.device_status = #{criteria.deviceStatus}
</if>
<if test="criteria.createTime != null and criteria.createTime.size() != 0">
and d.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
</if>
<if test="criteria.tenantId != null">
AND tenant_id = #{criteria.tenantId}
</if>
and d.customer_id = #{criteria.customerId}
</where>
order by d.create_time desc

View File

@ -61,6 +61,6 @@
and dt.create_by = #{criteria.createBy}
</if>
</where>
order by create_time desc
ORDER BY create_time DESC
</select>
</mapper>

View File

@ -0,0 +1,90 @@
package com.fuyuanshen.utils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageToCArrayConverter {
public static void main(String[] args) {
try {
convertImageToCArray("C:\\work\\6170_强光_160_80_2.jpg", "output.c", 160, 80);
System.out.println("转换成功!");
} catch (IOException e) {
System.err.println("转换失败: " + e.getMessage());
}
}
public static void convertImageToCArray(String inputPath, String outputPath,
int width, int height) throws IOException {
// 读取原始图片
BufferedImage originalImage = ImageIO.read(new File(inputPath));
// 调整图片尺寸
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
resizedImage.getGraphics().drawImage(
originalImage, 0, 0, width, height, null);
// 转换像素数据为RGB565格式高位在前
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = resizedImage.getRGB(x, y);
// 提取RGB分量
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = rgb & 0xFF;
// 转换为RGB5655位红6位绿5位蓝
int r5 = (r >> 3) & 0x1F;
int g6 = (g >> 2) & 0x3F;
int b5 = (b >> 3) & 0x1F;
// 组合为16位值
int rgb565 = (r5 << 11) | (g6 << 5) | b5;
// 高位在前(大端序)写入字节
byteStream.write((rgb565 >> 8) & 0xFF); // 高字节
byteStream.write(rgb565 & 0xFF); // 低字节
}
}
byte[] imageData = byteStream.toByteArray();
// 生成C语言数组文件
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
// 写入注释行(包含尺寸信息)
String header = String.format("/* 0X10,0X10,0X00,0X%02X,0X00,0X%02X,0X01,0X1B, */\n",
width, height);
fos.write(header.getBytes());
// 写入数组声明
fos.write("const unsigned char gImage_data[] = {\n".getBytes());
// 写入数据每行16个字节
for (int i = 0; i < imageData.length; i++) {
// 写入0X前缀
fos.write(("0X" + String.format("%02X", imageData[i] & 0xFF)).getBytes());
// 添加逗号(最后一个除外)
if (i < imageData.length - 1) {
fos.write(',');
}
// 换行和缩进
if ((i + 1) % 16 == 0) {
fos.write('\n');
} else {
fos.write(' ');
}
}
// 写入数组结尾
fos.write("\n};\n".getBytes());
}
}
}