导入设备数据

This commit is contained in:
2025-11-18 15:34:46 +08:00
parent 6d58268874
commit 3798e52ee0
23 changed files with 1351 additions and 192 deletions

View File

@ -27,14 +27,14 @@ public class Bitmap80x12Generator {
BufferedImage image = convertByteArrayToImage(bytes, 12, 80);
ImageIO.write(image, "PNG", new File("D:\\bitmap_preview.png"));
System.out.println("成功生成预览图片: D:\\bitmap_preview.png");
// 打印十六进制数据
// System.out.println("生成的点阵数据2:");
// printHexData(bitmapData);
// int[] ints = convertHexToDecimal(bitmapData);
System.out.println("打印十进制无符号:"+Arrays.toString(ints));
System.out.println("打印十进制无符号:" + Arrays.toString(ints));
// printDecimalData(bitmapData);
// 生成C文件
generateCFile(bitmapData, "bitmap_data.c", "chinese_text");
}
@ -125,7 +125,7 @@ public class Bitmap80x12Generator {
System.out.println();
}
public static void buildArr(int[] data,List<Integer> intData){
public static void buildArr(int[] data, List<Integer> intData) {
for (int datum : data) {
intData.add(datum);
}
@ -133,8 +133,8 @@ public class Bitmap80x12Generator {
/**
* 生成固定长度的点阵数据
*
* @param text 要转换的文本
*
* @param text 要转换的文本
* @param fixedLength 固定长度(字节)
* @return 固定长度的点阵数据
*/
@ -157,10 +157,11 @@ public class Bitmap80x12Generator {
int copyLength = Math.min(rawData.length, fixedLength);
System.arraycopy(rawData, 0, result, 0, copyLength);
// 剩余部分自动初始化为0
return result;
}
/**
* 创建文本图像
*/
@ -185,14 +186,14 @@ public class Bitmap80x12Generator {
// 获取字体度量
FontMetrics metrics = g.getFontMetrics();
// 计算文本绘制位置(居中)
int textWidth = metrics.stringWidth(text);
// int x = Math.max(0, (width - textWidth) / 2); // 水平居中
// 左对齐
int x = 0;
int y = (height - metrics.getHeight()) / 2 + metrics.getAscent(); // 垂直居中
// 绘制文本
g.drawString(text, x, y);
@ -242,6 +243,7 @@ public class Bitmap80x12Generator {
return byteListToArray(byteList);
}
public static byte[] byteListToArray(List<Byte> byteList) {
byte[] result = new byte[byteList.size()];
for (int i = 0; i < byteList.size(); i++) {
@ -282,7 +284,7 @@ public class Bitmap80x12Generator {
}
}
bitIndex++;
// 如果已经处理完所有像素,则退出
if (bitIndex >= width * height) {
return image;
@ -313,6 +315,7 @@ public class Bitmap80x12Generator {
sb.append("\n};");
return sb.toString();
}
/**
* 打印十六进制数据
*/
@ -320,7 +323,7 @@ public class Bitmap80x12Generator {
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xFF;
System.out.printf("0x%02X", value);
if (i < data.length - 1) {
System.out.print(", ");
if ((i + 1) % 12 == 0) System.out.println();
@ -348,6 +351,7 @@ public class Bitmap80x12Generator {
}
}
private static void writeByteArray(FileWriter writer, byte[] data) throws IOException {
for (int i = 0; i < data.length; i++) {
int value = data[i] & 0xFF;
@ -359,4 +363,6 @@ public class Bitmap80x12Generator {
}
}
}
}

View File

@ -0,0 +1,91 @@
package com.fuyuanshen.common.core.utils.file;
import lombok.extern.slf4j.Slf4j;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* 图片压缩工具类
*/
@Slf4j
public class ImageCompressUtil {
/**
* 压缩图片到指定大小以下
*
* @param imageData 原始图片数据
* @param maxSize 最大大小(字节)
* @return 压缩后的图片数据
*/
public static byte[] compressImage(byte[] imageData, int maxSize) {
try {
// 如果图片本身小于等于最大大小,直接返回
if (imageData.length <= maxSize) {
return imageData;
}
// 读取原始图片
BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData));
if (originalImage == null) {
log.warn("无法读取图片数据");
return imageData;
}
// 计算压缩比例
double scale = Math.sqrt((double) maxSize / imageData.length);
// 确保至少压缩到一半大小,避免压缩效果不明显
scale = Math.max(scale, 0.5);
// 压缩图片
byte[] compressedData = compressImageByScale(originalImage, scale);
// 如果压缩后还是太大,继续压缩
int attempts = 0;
while (compressedData.length > maxSize && attempts < 5) {
scale *= 0.8; // 每次缩小20%
compressedData = compressImageByScale(originalImage, scale);
attempts++;
}
log.info("图片压缩完成,原始大小: {} bytes, 压缩后大小: {} bytes, 压缩比例: {}",
imageData.length, compressedData.length, String.format("%.2f", scale));
return compressedData;
} catch (Exception e) {
log.error("图片压缩失败: {}", e.getMessage(), e);
return imageData; // 压缩失败时返回原始数据
}
}
/**
* 按比例缩放图片
*
* @param originalImage 原始图片
* @param scale 缩放比例
* @return 缩放后的图片数据
* @throws IOException IO异常
*/
private static byte[] compressImageByScale(BufferedImage originalImage, double scale) throws IOException {
int width = (int) (originalImage.getWidth() * scale);
int height = (int) (originalImage.getHeight() * scale);
// 创建缩放后的图片
Image scaledImage = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(scaledImage, 0, 0, null);
g2d.dispose();
// 输出为JPEG格式
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", baos);
return baos.toByteArray();
}
}

View File

@ -216,4 +216,5 @@ public class LogAspect {
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|| o instanceof BindingResult;
}
}