当前位置:网站首页>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
边栏推荐
- Student educational administration management system of C # curriculum design
- I want to do large screen data visualization application feature analysis
- Go resolve ID card
- Viz artist advanced script video tutorial -- stringmap use and vertex operation
- go 解析身份证
- Why can void * be a general pointer
- P2622 light off problem II (state compression search)
- Pulitzer Prize in the field of information graphics - malofiej Award
- oracle中的 (+)是什么意思
- Ventuz Foundation Series "one step at the door"
猜你喜欢
Zohocrm deluge function application time verification
[global product discovery 2] the first pure cloud augmented reality (AR) platform - Israel
L'installateur a été installé avec une erreur inattendue
Oracle insert single quotation mark
oracle 插入单引号
the installer has encountered an unexpected error installing this package
Clion toolchains are not configured configure disable profile problem solving
【更新中】微信小程序学习笔记_3
Osgearth target selection
P1596 [USACO10OCT]Lake Counting S
随机推荐
一条通往服务器所有端口的隧道
[audio and video] ijkplayer error code
Golang url的编码和解码
Transplantation of tslib Library
数据库应用技术课程设计之商城管理系统
C语言-入门-精华版-带你走进编程(一)
Open the influence list of "National Meteorological Short Videos (Kwai, Tiktok) in November" in an interactive way“
animation
L'installateur a été installé avec une erreur inattendue
Kunlunbase meetup is waiting for you!
Redis的数据结构
Golang string segmentation, substitution and interception
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
Yolo series --- xml2txt script
String class
796 · 开锁
the installer has encountered an unexpected error installing this package
P2704 [noi2001] artillery position (shape pressure DP)
Osgearth target selection
Storage of data