uniapp
//h5上传图片到服务器解析
//选择图片
uni.chooseImage({
// sourceType:"camera",
success: function(chooseImageRes) {
// console.log(chooseImageRes.tempFilePaths[0]); //临时路径
// console.log(chooseImageRes.tempFiles[0]); //文件数组
//上传文件
uni.uploadFile({
url: "http://192.168.0.115:84/home/ScanCode", //扫码解析图片地址,返回格式为字符串 'Data:{ scanType="", result="value" }'
fileType: "image",
filePath: chooseImageRes.tempFilePaths[0],
header: {
//设置请求头
// 'content-type': 'multipart/form-data'
},
success: (res) => {
// console.log(res);
var data=JSON.parse(res.data);
if (res.statusCode === 200) {
//解析结果
obj.success(data.Data);
console.log('条码类型:' + data.Data.scanType);
console.log('条码内容:' + data.Data.result);
}
}
})
}
});
/// <summary>
/// 解析二维码,引入ThoughtWorks.QRCode包
/// </summary>
/// <returns></returns>
public ActionResult ScanCode()
{
//上传文件流转bute[]
var file=Request.Files[0];
byte[] bytes = new byte[file.InputStream.Length];
file.InputStream.Read(bytes, 0, bytes.Length);
//byte[]转位图Bitmap
MemoryStream ms1 = new MemoryStream(bytes);
Bitmap bm = (Bitmap)Image.FromStream(ms1);
//解析二维码
QRCodeDecoder decoder = new QRCodeDecoder();
string decodedString = decoder.decode(new QRCodeBitmapImage(bm), Encoding.UTF8);//指定utf-8编码, 支持中文
return Json(new JsonResult() {Data= new { scanType="", result=decodedString } });
}