当前位置:网站首页>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
边栏推荐
- Mysql容器化(1)Docker安装MySQL
- How to establish rectangular coordinate system in space
- Osgearth target selection
- Use of ue5 QRcode plug-in
- oracle 插入单引号
- 【云原生】微服务之Feign的介绍与使用
- Student educational administration management system of C # curriculum design
- Pulitzer Prize in the field of information graphics - malofiej Award
- Are you still watching the weather forecast on TV?
- Chain length value
猜你喜欢
梯度下降法求解BP神经网络的简单Demo
Base64和Base64URL
Puhua PLM empowers the whole scene product lifecycle management and helps the enterprise digital transformation of the main line of products
unity2019_ Input management
Basic operation and process control 2
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
P1596 [USACO10OCT]Lake Counting S
Haproxy+kept build 01
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
数据库应用技术课程设计之商城管理系统
随机推荐
P2704 [noi2001] artillery position (shape pressure DP)
Luaframwrok handles resource updates
Golang time format sorting
【音视频】ijkplayer错误码
Kunlunbase meetup is waiting for you!
[updating] wechat applet learning notes_ three
LinkList
Golang中删除字符串的最后一个字符
Introduction to Base64 coding
Multi traveling salesman problem -- overview of formula and solution process
freetype库的移植
String class
Flex flexible box layout
My touch screen production "brief history" 1
oracle 插入单引号
Jupyter remote server configuration and server startup
C#课程设计之学生教务管理系统
图像处理8-CNN图像分类
Mall management system of database application technology course design
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template