def imagefun(msg,qun,user): #下载图片 respContent = "无法识别你的图片" MediaId = msg.media_id # picname = CreateTime+"_"+fromUserName+"_"+str(index++)+".jpg" #避免多人同时上传或一人同时上传 picname = str(msg.time)+"_"+MediaId+".jpg" datenow = TimeUtil.getCurrentMonth() realdirPath =STATICFILES_DIRS[0] + "/upload/img/"+datenow+"/" if (not os.path.exists(realdirPath)): print("目录不存在正在创建:" + realdirPath) os.makedirs(realdirPath) picpath = realdirPath+picname if (not os.path.exists(picpath)): WeixinUtil.downloadMedia(MediaId,picpath) #下载图片到指定目录 else: respContent = "请再传一次" return respContent #尝试识别二维码 try: allwords = OCR.getocr(picpath) #ocr识别图片中的文字 if (allwords==None): respContent = "今日流量已达上限,请明天再试" else: ocrtext = str(allwords) #全部转化为文字 print(ocrtext) respContent = getinfo(ocrtext,qun,user,picpath) #根据文字生成响应文本 except Exception as e: print('ocr识别出错', e) respContent = "图片识别出错" return respContent
def uploadimgfile(request): if request.method == "POST": userid = request.POST.get("userid", 0) # 读取post数据,None为默认值 qunid = request.POST.get("qunid", 0) # 读取post数据,None为默认值 if request.method == "GET": userid = request.GET.get("userid", 0) # 读取post数据,None为默认值 qunid = request.GET.get("qunid", 0) # 读取post数据,None为默认值 datenow = TimeUtil.getCurrentMonth() realPath = STATICFILES_DIRS[0] + "/upload/img/" + datenow + "/" #realPath = request.getSession().getServletContext().getRealPath("upload/img/"+datenow+"/") print("上传文件的根目录为:" + realPath) if not os.path.exists(realPath): print("目录不存在正在创建:" + realPath) os.makedirs(realPath) imgfile = request.FILES['file'] # geshi = multFile.getOriginalFilename().substring(multFile.getOriginalFilename().lastIndexOf(".")) image = Image.open(imgfile) if (not image): print("文件未上传") else: if image.mode not in ('L', 'RGB'): image = image.convert('RGB') width, height = image.size filename = str(int(time.time())) + "_width" + str( width) + "_height" + str(height) + "_" + "qunid" + str( qunid) + "_" + "userid" + str(userid) + ".jpg" image.save(realPath + "/" + filename, 'JPEG') image.thumbnail((300, 300)) image.save(realPath + "/thumb" + filename, 'JPEG') fileurl = STATIC_URL1 + "upload/img/" + datenow + "/" + filename print(fileurl) return HttpResponse(fileurl)
def getinfo(ocrtext,qun,user,filepath): ocrtext=ocrtext.replace(" ", "") respContent="" if (len(allname)==0): # 主要配置表中有,就都能识别 jsonobj=json.loads(qun.gonggao.replace('\'','\"')) # 获取配置的价格表 for keystr in jsonobj: money = jsonobj[keystr] onerow = keystr.split("|") if (len(onerow)==3): allname.append(keystr) allqiye.append(onerow[0]) allchanpin.append(onerow[1]) allbank.append(onerow[2]) alljiage.append(money) # 分为大杯和中杯饮品,和多少元代金券 bank="" qiye="" chanpin = "" quanma="" money=0 canshibie=False # 先用企业和商品类型识别一遍,这个为必须包含项 for i in range(len(allqiye)): if (allqiye[i] in ocrtext and allchanpin[i] in ocrtext): qiye = allqiye[i] chanpin=allchanpin[i] bank = allbank[i] money= float(alljiage[i]) canshibie=True break # 如果不能识别,就返回不能识别说明 if (not canshibie): respContent = '无法识别此类商品或此类商品已不再收录,您可以回复jg查看收录商品,或通过其他方式上传。\n\n'\ + '<a href="'+urlpath+'piaoquan/mydiary?qunid=999&typeindex=1&userid='+str(user.id)+'">券码管理</a>\n\n'\ + '<a href="'+urlpath+'piaoquan/setting?userid='+str(user.id)+'">个人账户中心</a>\n\n'\ + '回复“jg”查询商品价格!' # print(respContent) return respContent # 再用企业、商品类型、银行识别一遍,作为次选。因为银行并不是必选项 for i in range(len(allqiye)): if (allqiye[i] in ocrtext and allchanpin[i] in ocrtext and allbank[i] in ocrtext): qiye = allqiye[i] chanpin=allchanpin[i] bank = allbank[i] money= float(alljiage[i]) break # print(qiye, chanpin, bank, money) # 如果有图片就使用图片识别券码 if (filepath): try: code = QRcode.ocr_qrcode_zxing(filepath) # 识别图中的二维码 print("第一种方式二维码识别结果:",code) # if(code==None or code=="null" or code==""): # code = QRcode.ocr_qrcode_zxing(filepath) # 识别图中的二维码 # else: # # 判断编码是不是只有字母数字和= # regexstr="^[a-zA-Z=0-9]+$" # pattern = re.compile(regexstr) # result = re.match(pattern,code) # if (not result): # return '无法识别二维码' # print("二维码识别结果:"+code) if (code.find("=")>-1): quanma = code[0:code.find("=")] else: quanma = code except Exception as e: print("二维码识别出错:",e) # 没有图片或者图片匹配不到就正则表达式识别 if (quanma==""): # 识别券码 regexstr="[0-9A-Za-z]{10,30}" # 查找指定的字符串 pattern=re.compile(regexstr) matcher = re.match(pattern, ocrtext) i = 0 while(matcher.groups(i)): # 如果在匹配到的数字前出现的“订单编号”,则放弃 if (matcher.start(i)-8>0): if ('订单编号' in ocrtext[matcher.start(i)-8,matcher.start(i)]): i += 1 continue quanma= matcher.group(i) break # 如果券码为"" if (quanma==""): # 如果没有图片,则放弃 if (filepath==None): respContent = "无法识别券码,请通过其他方式上传,或者联系客服\n\n"\ + "<a href=\""+urlpath+"piaoquan/mydiary.jsp?qunid=999&typeindex=1&userid="+str(user.getId())+"\">券码管理</a>\n\n"\ + "<a href=\""+urlpath+"piaoquan/setting.jsp?userid="+str(user.getId())+"\">个人账户中心</a>\n\n"\ + "回复“jg”查询商品价格!" return respContent else: # 如果有图片,则券号自定义为no+时间+index global index index+=1 quanma="no"+TimeUtil.getCurrentDate1()+TimeUtil.getCurrentTime1()+str(index) # 修改图片名称为券号的名称 picname ="" if(filepath): mulu = filepath[0:filepath.rfind('/') + 1] # 目录(带最后的/) # picname = filepath[filepath.rfind('/') + 1:] # 文件名 geshi =filepath[filepath.index('.')+1:] picname = quanma+"."+geshi newfilepath = mulu+picname if (os.path.exists(newfilepath)): os.remove(newfilepath) os.rename(filepath,newfilepath) print('将图片重命名为新图片名称:',newfilepath) content = qiye + "|" + chanpin + "|" + bank #要存储返回的内容 tip="该票券不是第一次上传了\n" diary = DiaryDAO.selectDiaryforSerial(quanma) if (not diary): diary = Diary() diary.userid= user.id diary.qunid= 999 if (not filepath): diary.diarytype=ResultCode.CONTENT_TYPEWORD # 设置日志只有短信 else: diary.diarytype=ResultCode.CONTENT_TYPEIMG # 设置日志包含图片 diary.type="1" diary.serial=quanma diary.state=0 diary.content=content diary.time=TimeUtil.getCurrentTime() diary.date=TimeUtil.getCurrentDate() diary.money=10*int(money) # 怎么设置价格 if(filepath): datenow = TimeUtil.getCurrentMonth() diary.imgone= STATIC_URL1+ "upload/img/"+datenow+"/"+picname DiaryDAO.addDiary(diary) tip="" # 修改用户的以上传金额 user.tuijiannum = user.tuijiannum+diary.money # 设置用户以上传金额 UserDAO.updateUserInfo(user) respContent = "您的券码已经收到录入,等待橙子权益的汇款即可。\n\n商品:"+content+"\n券码:"+quanma+"\n价格:¥"+str(money) \ + "\n\n<a href=\""+urlpath+"piaoquan/setting.jsp?userid="+str(user.id)+"\">个人账户中心</a>\n\n"\ + "<a href=\""+urlpath+"piaoquan/mydiary.jsp?qunid=999&typeindex=1&userid="+str(user.id)+"\">券码管理</a>\n\n"+tip\ + "回复“jg”查询商品价格!" return respContent