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;
|
|
}
|
}
|