whycxzp
2025-03-01 e76c58f4c0e9c334a19c9c4418343ca99d5e3d0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.whyc.service;
 
import com.google.zxing.BinaryBitmap;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.whyc.dto.Response;
import com.whyc.pojo.BattRTState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
 
@Service
public class QRCodeService {
 
 
    @Autowired
    private BattRTStateService battRTStateService;
 
    public String extract(byte[] data) {
        try {
            InputStream inputStream = new ByteArrayInputStream(data);
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
            Result result = new MultiFormatReader().decode(binaryBitmap);
            return result.getText();
 
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
 
    public Response extractAndGetData(byte[] data) {
        String battGroupId = extract(data);
        System.out.println("battGroupId:"+battGroupId);
        //测试效果,用静态数据
        /*BattRTState battRTState = new BattRTState();
        battRTState.setBattGroupId(1);
        battRTState.setGroupTmp(27.0f);
        return new Response().set(1,battRTState);*/
        return battRTStateService.getOneByBattGroupId(battGroupId);
    }
 
    public static String test() {
        try {
            //InputStream inputStream = new ByteArrayInputStream(data);
            InputStream inputStream = new FileInputStream("C:\\code\\web\\InspectionSystem\\src\\main\\resources\\lib\\r5.jpg");
            BufferedImage bufferedImage = ImageIO.read(inputStream);
            BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(bufferedImage)));
            Result result = new MultiFormatReader().decode(binaryBitmap);
            return result.getText();
 
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
 
    public static void main(String[] args) {
        System.out.println(test());
    }
 
 
    public Response<BattRTState> uploadVideo(byte[] data) {
        try {
            //将data存储为视频文件
            FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\29550\\Desktop\\当前项目\\2023\\0AR眼镜项目-巡检系统\\项目文件\\upload_test_1.mp4");
            //流式处理,缓冲为2048字节
            InputStream videoFileStream = new ByteArrayInputStream(data);
            byte[] buffer = new byte[2048];
            int bytesRead;
            while ((bytesRead = videoFileStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, bytesRead);
            }
            System.out.println("视频文件上传成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
 
    }
}