当前位置:网站首页>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
边栏推荐
- Advanced OSG collision detection
- C language - Introduction - essence Edition - take you into programming (I)
- Conversion between golang JSON format and structure
- Demonstration of plug-in use of ventuz basic series
- Dotween plug-in
- Golang 字符串分割,替换和截取
- MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
- Unity performance optimization
- Storage of data
- Conversion between string and int types in golang
猜你喜欢

Use of ue5 QRcode plug-in

Are you still watching the weather forecast on TV?

Dotween plug-in

My touch screen production "brief history" 2
![[cloud native] introduction and use of feign of microservices](/img/39/05cf7673155954c90e75a8a2eecd96.jpg)
[cloud native] introduction and use of feign of microservices

Clion toolchains are not configured configure disable profile problem solving

MAE

C#课程设计之员工信息管理系统

Base64和Base64URL

Unity performance optimization
随机推荐
【更新中】微信小程序学习笔记_3
unity2019_ Input management
Are you still watching the weather forecast on TV?
MAE
Conversion between string and int types in golang
animation
Classes and objects
P2622 关灯问题II(状态压缩 搜索)
Swagger document configuration
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
Clip Related Script
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Storage of data
A tunnel to all ports of the server
About Wireshark's unsuccessful installation of npcap
Huawei interview summary during the epidemic
Pulitzer Prize in the field of information graphics - malofiej Award
Sequence of map implementation classes
超限黑客认知
Redis的数据结构