当前位置:网站首页>Transmit pictures with Base64 encoding
Transmit pictures with Base64 encoding
2022-07-03 08:21:00 【Yuetun】
Project requirements :
Complete the image upload of the front end , But it's different from the previous uploaded pictures , The upload process is :1 Click to take a picture 》》 Visit the camera program 》》 Return to the picture after taking the picture 》》 Send it to the system program to store pictures .( Photo upload )
Development framework :
The front and rear ends are separated jeecgboot
Solution :
Ideas
First of all, the traditional use of front-end upload components is not feasible , Because the front-end components are uploaded manually , What this demand needs is to take photos and upload them automatically . So I thought of code uploading , To understand the common image coding upload is base64 code .
Reference procedure
The coding content is inconsistent
Then I wrote it and tested it , It is found that the coding content of the front end is inconsistent with that received by the back end
Results found :
In the process of transmission, the encoded content is automatically url Re code , So encode the image before reading and writing on the back end Decode twice url and base decode .
Program realization
front end
takePictures() {
let that=this
axios({
url:"http://127.0.0.1:5189/photo",
headers: {
"Referrer-Policy": "no-referrer-when-downgrade"
},method:'get'}
).then(res=>{
if(res.status == '200'){
let base64= Object.assign({
}, {
}, {
base64:res.data.file,name:res.data.filename.substr(6)})
postAction(window._CONFIG['domianURL']+"/sys/common/uploadBase64",base64).then(res1=>{
// Processing after uploading successfully
this.formData.imgList=res.data.filename;
console.log(this.formData.imgList)
let takePicture = '';
takePicture = window._CONFIG['domianURL']+'/sys/common/static/temps/'+this.formData.imgList.substr(6);
console.log(takePicture)
this.formData.imgList = [takePicture];
this.formData.imgList = res.data.filename;
},err=>{
// Handling of errors
})
}else{
this.tipcontent = ' Photographing error ';
this.visible = true;
}
})
// this.formData.imgList.push('')
},
Back end programs :
@PostMapping(value = "/uploadBase64")
public Result<?> generateImage(@RequestBody Base64 base64) {
// On byte array string Base64 Decode and generate pictures
String imgStr=base64.getBase64();
if (imgStr == null) // Image data is empty
{
Result result = new Result();
// System.out.println(" The picture is empty ");
result.setMessage(" The picture is empty ");
result.setSuccess(false);
return result;
}
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64 decode
imgStr = URLDecoder.decode(imgStr);
imgStr = imgStr.replaceAll(" ", "+").replace("data:image/jpeg;base64,", "");
byte[] bytes = decoder.decodeBuffer(imgStr);
for (int i = 0; i < bytes.length; ++i) {
if (bytes[i] < 0) {
// Adjust abnormal data
bytes[i] += 256;
}
}
// Generate jpg picture
OutputStream out = new FileOutputStream("D://Image//temps//"+base64.getName());
out.write(bytes);
out.flush();
out.close();
System.out.println("success");
Result result = new Result();
result.setSuccess(true);
return result;
} catch (Exception e) {
Result result = new Result();
result.setSuccess(false);
System.out.println("error");
return result;
}
}
Cross-domain problem
Because the camera program and the system program are not on the same machine, there is a cross domain problem
Solution
axios Cross domain is two requests
Reference resources
Solution , The cross domain request server is for options Ask for something to return casually
边栏推荐
- Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
- Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
- [USACO12MAR]Cows in a Skyscraper G(状态压缩dp)
- jupyter远程服务器配置以及服务器开机自启
- Delete the last character of the string in golang
- 方正锐利重磅升级到12.0版本,包装印前处理更加便捷、高效!
- C#课程设计之员工信息管理系统
- Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
- UE4 call DLL
- LinkList
猜你喜欢

Basic operation and process control

the installer has encountered an unexpected error installing this package
![[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December](/img/51/81ceaf8746ec7455ea8abf9f038e81.jpg)
[end of 2021] National Meteorological Short Video (Kwai, Tiktok) influence list in December

jupyter远程服务器配置以及服务器开机自启

Transfinite hacker cognition

oracle 插入单引号

C course design employee information management system

matlab神經網絡所有傳遞函數(激活函數)公式詳解

the installer has encountered an unexpected error installing this package

Transplantation of freetype Library
随机推荐
Use filechannel to copy files
Get to know unity2 for the first time
Clip Related Script
2021-10-19
梯度下降法求解BP神经网络的简单Demo
Editor Extensions
Shader foundation 01
JSON与Object之间转换
Student educational administration management system of C # curriculum design
Mutual call between Lua and C #
L'installateur a été installé avec une erreur inattendue
Jupyter remote server configuration and server startup
P2622 关灯问题II(状态压缩 搜索)
Conversion between golang JSON format and structure
[cloud native] introduction and use of feign of microservices
Xlua task list youyou
[set theory] order relation (the relation between elements of partial order set | comparable | strictly less than | covering | Haas diagram)
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
Mall management system of database application technology course design
My touch screen production "brief history" 1